Pong
November 12th 2014, Android JNI Torch Revisited
← November 12th 2014 Android JNI Revisited | ● | Decemver 14th 2014 SQLite Efficient searching →
With the previous AndroidExtras procedure that allows us to add Java code to a Qt Android C++ app, we implement a torch light by turning the mobile flashlight on or off:
// copyright by Stefan Roettger, licensed under the "New BSD License"
package org.openterrain.example;
import org.openterrain.example.Main;
import android.hardware.Camera;
import java.lang.String;
import java.util.List;
import android.graphics.SurfaceTexture;
import android.util.Log;
public class Torch extends Main
{
protected Camera cam;
protected Camera.Parameters params;
public void on()
{
try
{
cam = Camera.open();
params = cam.getParameters();
List<String> flashModes = params.getSupportedFlashModes();
if (flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH))
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
else
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
cam.setParameters(params);
cam.setPreviewTexture(new SurfaceTexture(0));
cam.startPreview();
cam.autoFocus(new Camera.AutoFocusCallback() { public void onAutoFocus(boolean success, Camera cam) {} });
}
catch (Exception e)
{
Log.d("Example", "Torch.on: could not release camera");
}
}
public void off()
{
try
{
params = cam.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
cam.setParameters(params);
cam.stopPreview();
cam.release();
}
catch (Exception e)
{
Log.d("Example", "Torch.off: could not acquire camera");
}
}
}
package org.openterrain.example;
import org.openterrain.example.Main;
import android.hardware.Camera;
import java.lang.String;
import java.util.List;
import android.graphics.SurfaceTexture;
import android.util.Log;
public class Torch extends Main
{
protected Camera cam;
protected Camera.Parameters params;
public void on()
{
try
{
cam = Camera.open();
params = cam.getParameters();
List<String> flashModes = params.getSupportedFlashModes();
if (flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH))
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
else
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
cam.setParameters(params);
cam.setPreviewTexture(new SurfaceTexture(0));
cam.startPreview();
cam.autoFocus(new Camera.AutoFocusCallback() { public void onAutoFocus(boolean success, Camera cam) {} });
}
catch (Exception e)
{
Log.d("Example", "Torch.on: could not release camera");
}
}
public void off()
{
try
{
params = cam.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
cam.setParameters(params);
cam.stopPreview();
cam.release();
}
catch (Exception e)
{
Log.d("Example", "Torch.off: could not acquire camera");
}
}
}
← November 12th 2014 Android JNI Revisited | ● | Decemver 14th 2014 SQLite Efficient searching →