How to turn on and off camera outside the UI thread? - java

I am trying to turn on and off camera flashlight using the following code, but every time I call those functions it slows down my whole app. Which is kinda annoying. Is there any way I can do it in separate thread or perhaps in a different way?
Thanks in advance.
private void turnOnFlash() {
if (!isFlashOn && hasFlash) {
if (camera == null || params == null) {
return;
}
isFlashOn = true;
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
}
}
private void turnOffFlash() {
if (isFlashOn && hasFlash) {
if (camera == null || params == null) {
return;
}
isFlashOn = false;
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
}
}
and im calling it from another function like
if (intensity[3] < 0.5f) {
turnOnFlash();
} else {
turnOffFlash();
}

setParameters and getParameters may take noticeable time.
But what really slows the app is that UI thread becomes the handler for all camera callbacks. Here in an old answer I showed how easy it is to handle the camera in a background HandlerThread.
Note that the camera API you use here was deprecated some years ago, while the current camera2 API handles this threading issue correctly, and don't need these tricks anymore.
You can post turnOffFlash() and turnOnFlash() to the same HandlerThread.

Related

Set a fixed focus value in the main camera (Android Studio)

I am developing an app for my project and I don't want my main camera to have autofocus. I want my camera to have a specific focus value that can focus on objects that are about 5-10 cm from the main camera.
Here is the code on how the CameraManager is implemented:
private String chooseCamera() {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
for (final String cameraId : manager.getCameraIdList()) {
final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
// We don't use a front facing camera in this sample.
final Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
continue;
}
final StreamConfigurationMap map =
characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (map == null) {
continue;
}
// Fallback to camera1 API for internal cameras that don't have full support.
// This should help with legacy situations where using the camera2 API causes
// distorted or otherwise broken previews.
useCamera2API =
(facing == CameraCharacteristics.LENS_FACING_FRONT)
|| isHardwareLevelSupported(
characteristics);
LOGGER.i("Camera API lv2?: %s", useCamera2API);
// manager.setTorchMode(cameraId, true);
return cameraId;
}
} catch (CameraAccessException e) {
LOGGER.e(e, "Not allowed to access camera");
}
return null;
}
protected void setFragment() {
String cameraId = chooseCamera();
Fragment fragment;
if (useCamera2API) {
CameraConnectionFragment camera2Fragment =
CameraConnectionFragment.newInstance(
(size, rotation) -> {
previewHeight = size.getHeight();
previewWidth = size.getWidth();
CameraActivity.this.onPreviewSizeChosen(size, rotation);
},
this,
getLayoutId(),
getDesiredPreviewFrameSize());
camera2Fragment.setCamera(cameraId);
fragment = camera2Fragment;
} else {
fragment =
new LegacyCameraConnectionFragment(this, getLayoutId(), getDesiredPreviewFrameSize());
}
getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
}
You need to first turn off autofocus by using CaptureRequest.CONTROL_AF_MODE and then set desired focus distance value using this request key, CaptureRequest.LENS_FOCUS_DISTANCE.
To know if a device supports manual sensor or not, query characteristics and check for support.
https://developer.android.com/reference/android/hardware/camera2/CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR

Android Studio Emulator bug? Alarm sound keeps looping even if I set mediaPlayer.setLooping(false)

I am not sure if this is just an emulator bug or did I miss something. I am writing an alarm app. My app gets a ringtone Uri with RingToneManager, then use MediaPlayer to play the ringtone Uri. I want to control whether the ringtone loops or not. It works fine on my real smartphone, as well as with the Android Studio emulator using Uri gets with RingtoneManager.TYPE_NOTIFICATION and RingtoneManager.TYPE_RINGTONE. However, if I use Uri gets with RingtoneManager.TYPE_ALARM with the emulator, it just keeps looping even if I set mediaPlayer.setLooping(false).
This seems like an emulator bug to me. However, I want to make sure I do not miss something that I need to set. I do not want to find out after the app is released and it turned out that other people's smartphones have the same problem as the emulator. Is there anything I can do or check?
Below is the related code:
activeAlarmSound = new MediaPlayer();
activeAlarmSound.setAudioAttributes(
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
);
try {
activeAlarmSound.setDataSource(this, Uri.parse(ringToneString));
if (!hasBGMusic && !timeUpItem.sayDate && !timeUpItem.sayDay && !timeUpItem.sayTime) {
activeAlarmSound.setLooping(true);
} else {
activeAlarmSound.setLooping(false);
}
activeAlarmSound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (playedBGMusic && timeUpItem.getDismissMethod() == 0) {
stopSelf();
} else {
playedAlarmSound = true;
if (activeBGMusic == null) {
playBGMusic();
} else {
activeBGMusic.start();
}
if (timeUpItem.getRepeatAlarm() > 0) {
endAlarmHandler = new Handler();
endAlarmHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (activeBGMusic != null) {
activeBGMusic.pause();
}
playAlarmSoundAndTime();
}
}, timeUpItem.getRepeatAlarm() * 60000);
}
}
}
});
activeAlarmSound.prepare();
activeAlarmSound.start();
} catch (IOException e) {
e.printStackTrace();
}```

onFaceDetection called only once or twice while running but works perfectly when debugging with breakpoints

This is the code I am using for face detection, the problem is when I debug this code with android studio the onFaceDetection method is called multiple times and face is detected perfectly(When i put a break point inside the method). But when I run it without any break points the method is called only 2-3 times and face detection doesn't take place. Any help regarding this would be much appreciated, as you can see from the code I've tried stopping and starting face detection.
void setFaceDetectionListener() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mFaceDetectionListener = new Camera.FaceDetectionListener() {
Handler faceDetectionHandler;
#Override
public void onFaceDetection(final Camera.Face[] faces, final Camera camera) {
if(faceDetectionHandler == null){//Initialize
faceDetectionHandler = new Handler();
Toast.makeText(HWTestActivity.this,
UiMessages.MSG_SHOW_YOUR_FACE.toString(),
Toast.LENGTH_SHORT).show();
}
faceDetectionHandler.post(new Runnable() {
#Override
public void run() {
Log.e("faceDetect", "No of faces = " + faces.length);
if (!is_face_detected) {
Toast.makeText(HWTestActivity.this,
UiMessages.MSG_DETECTING_YOUR_FACE.toString(),
Toast.LENGTH_SHORT).show();
is_face_detected = faces.length > 0;
}
if (faces.length > 0) {
Toast.makeText(HWTestActivity.this,
UiMessages.MSG_FACE_DETECTED.toString(),
Toast.LENGTH_SHORT).show();
camera.stopFaceDetection();
} else {
camera.stopFaceDetection();
camera.startFaceDetection();
}
}
});
}
};
}
}
This was ignorance on my part, apparently you can't have face detection running while the media recorder is running, So guys don't try to run face detection while you are recording with the camera simultaneously.
If you really wanted to detect faces while recording then you should use the
onPreviewFrame(byte[] pixelData, Camera camera)
method in
Camera.PreviewCallback()
convert the pixelData to RGB_565 bitmap and supply it to the FaceDetector.findfaces method. But in my experience I find this method to be very unreliable.

Force close for Android Custom Camera app

I am developing an android custom camera application without using the intent (to avoid getting android's built in camera features). I have enabled auto focus feature in my app. I am taking the picture on press of a keyVolume Button. And I am using the below code for setting the parameters.
Camera.Parameters p = camera.getParameters();
camera.autoFocus(autoFocusCallback);
p.setFocusMode(Parameters.FOCUS_MODE_AUTO);
camera.setParameters(p1);
camera.takePicture(shutterCallback, rawCallback, jpgCallback);
void setHandler(Handler autoFocusHandler, int autoFocusMessage)
{
this.autoFocusHandler = autoFocusHandler;
this.autoFocusMessage = autoFocusMessage;
}
private AutoFocusCallback autoFocusCallback = new AutoFocusCallback()
{
private Object success;
#Override
public void onAutoFocus(boolean autoFocusSuccess, Camera camera)
{
if (autoFocusHandler != null)
{
Message message = autoFocusHandler.obtainMessage(autoFocusMessage, success);
autoFocusHandler.sendMessageDelayed(message, AUTOFOCUS_INTERVAL_MS);
autoFocusHandler = null;
}
else
{
}
}
};
But the problem is that, this code works fine only for LG phone. and i am getting force close on all other phones after running it.
And the Error Log looks like this
http://textuploader.com/?p=6&id=kOc9G
Not getting where i am going wrong. Please Help! Thanks!
Different phones have different camera params. Check if mode available befire actually setting it.
For example, in your case there is public List<String> getSupportedFocusModes () function of
Camera.Parameters class.
Afaik, cheap phones like acer or zte or some others, have very weak programming support for their cameras.
UPD: code sample
Camera.Parameters p = camera.getParameters();
List<String> modes = p.getSupportedFocusModes();
if(modes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
{
p.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setParameters(p);
camera.autoFocus(autoFocusCallback);
}
else
{
// this is default focus mode if autofocus unsupported.
// also, we should not call camera.autoFocus(autoFocusCallback) here
p.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
camera.setParameters(p);
}
you are using
Camera.Parameters p = camera.getParameters();
so replace
camera.setParameters(p1);
with
camera.setParameters(p);
I think this should help you....
Camera.Parameters p = camera.getParameters();
List<Size> sizes = p.getSupportedPictureSizes();
// Choose any one you want among sizes
size = sizes.get(0);
p.setPictureSize(size.width, size.height);
camera.setParameters(p);
Don't use "p.setFocusMode(Parameters.FOCUS_MODE_AUTO);" line.
By default focus mode will be FOCUS_MODE_AUTO.

Enabling Camera Flash While Recording Video

I need a way to control the camera flash on an Android device while it is recording video. I'm making a strobe light app, and taking videos with a flashing strobe light would result in the ability to record objects that are moving at high speeds, like a fan blade.
The flash can only be enabled by starting a video preview and setting FLASH_MODE_TORCH in the camera's parameters. That would look like this:
Camera c = Camera.open();
Camera.Parameters p = c.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
c.setParameters(p);
c.startPreview();
Once the preview has started, I can flip that parameter back and forth to turn the light on and off. This works well until I try to record a video. The trouble is that in order to give the camera to the MediaRecorder, I first have to unlock it.
MediaRecorder m = new MediaRecorder();
c.unlock(); // the killer
m.setCamera(c);
After that unlock, I can no longer change the camera parameters and therefore have no way to change the flash state.
I do not know if it is actually possible to do this since I'm not the best at java-hacking, but here is what I do know:
Camera.unlock() is a native method, so I can't really see the mechanism behind the way it locks me out
Camera.Parameter has a HashMap that contains all of its parameters
Camera.setParameters(Parameters) takes the HashMap, converts it to a string, and passes it to a native method
I can eliminate all the parameters but TORCH-MODE from the HashMap and the Camera will still accept it
So, I can still access the Camera, but it won't listen to anything I tell it. (Which is kind of the purpose of Camera.unlock())
Edit:
After examining the native code, I can see that in CameraService.cpp my calls to Camera.setParameters(Parameters) get rejected because my Process ID does not match the Process ID the camera service has on record. So it would appear that that is my hurdle.
Edit2:
It would appear that the MediaPlayerService is the primary service that takes control of the camera when a video is recording. I do not know if it is possible, but if I could somehow start that service in my own process, I should be able to skip the Camera.unlock() call.
Edit3:
One last option would be if I could somehow get a pointer to the CameraHardwareInterface. From the looks of it, this is a device specific interface and probably does not include the PID checks. The main problem with this though is that the only place that I can find a pointer to it is in CameraService, and CameraService isn't talking.
Edit4: (several months later)
At this point, I don't think it is possible to do what I originally wanted. I don't want to delete the question on the off chance that someone does answer it, but I'm not actively seeking an answer. (Though, receiving a valid answer would be awesome.)
I encountered a similar issue. The user should be able to change the flash mode during recording to meet their needs depending on the light situation. After some investigative research i came to the following solution:
I assume, that you've already set up a proper SurfaceView and a SurfaceHolder with its necessary callbacks. The first thing i did was providing this code (not declared variables are globals):
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
camera.startPreview();
recorder = new MediaRecorder();
} catch (IOException e) {
e.printStackTrace();
}
}
My next step was initializing and preparing the recorder:
private void initialize() {
camera.unlock();
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setVideoFrameRate(20);
recorder.setOutputFile(filePath);
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
It's important to note, that camera.unlock() has to be called BEFORE the whole initialization process of the media recorder. That said also be aware of the proper order of each set property, otherwise you'll get an IllegalStateException when calling prepare() or start(). When it comes to recording, i do this. This will usually be triggered by a view element:
public void record(View view) {
if (recording) {
recorder.stop();
//TODO: do stuff....
recording = false;
} else {
recording = true;
initialize();
recorder.start();
}
}
So now, i finally can record properly. But what's with that flash? Last but not least, here comes the magic behind the scenes:
public void flash(View view) {
if(!recording) {
camera.lock();
}
parameters.setFlashMode(parameters.getFlashMode().equals(Parameters.FLASH_MODE_TORCH) ? Parameters.FLASH_MODE_OFF : Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
if(!recording) {
camera.unlock();
}
}
Everytime i call that method via an onClick action i can change the flash mode, even during recording. Just take care of properly locking the camera. Once the lock is aquired by the media recorder during recording, you don't have to lock/unlock the camera again. It doesn't even work. This was tested on a Samsung Galaxy S3 with Android-Version 4.1.2. Hope this approach helps.
After preparing media recorder, use camera.lock(), and then set whatever parameters you want to set to camera.
But before starting recording you need to call camera.unlock(), and after you stop media recorder you need to call camera.lock() to start preview.
Enjoy!!!
Try this.. hopefully it will work.. :)
private static Torch torch;
public Torch() {
super();
torch = this;
}
public static Torch getTorch() {
return torch;
}
private void getCamera() {
if (mCamera == null) {
try {
mCamera = Camera.open();
} catch (RuntimeException e) {
Log.e(TAG, "Camera.open() failed: " + e.getMessage());
}
}
}
public void toggleLight(View view) {
toggleLight();
}
private void toggleLight() {
if (lightOn) {
turnLightOff();
} else {
turnLightOn();
}
}
private void turnLightOn() {
if (!eulaAgreed) {
return;
}
if (mCamera == null) {
Toast.makeText(this, "Camera not found", Toast.LENGTH_LONG);
button.setBackgroundColor(COLOR_WHITE);
return;
}
lightOn = true;
Parameters parameters = mCamera.getParameters();
if (parameters == null) {
button.setBackgroundColor(COLOR_WHITE);
return;
}
List<String> flashModes = parameters.getSupportedFlashModes();
if (flashModes == null) {
button.setBackgroundColor(COLOR_WHITE);
return;
}
String flashMode = parameters.getFlashMode();
Log.i(TAG, "Flash mode: " + flashMode);
Log.i(TAG, "Flash modes: " + flashModes);
if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameters);
button.setBackgroundColor(COLOR_LIGHT);
startWakeLock();
} else {
Toast.makeText(this, "Flash mode (torch) not supported",
Toast.LENGTH_LONG);
button.setBackgroundColor(COLOR_WHITE);
Log.e(TAG, "FLASH_MODE_TORCH not supported");
}
}
}
private void turnLightOff() {
if (lightOn) {
button.setBackgroundColor(COLOR_DARK);
lightOn = false;
if (mCamera == null) {
return;
}
Parameters parameters = mCamera.getParameters();
if (parameters == null) {
return;
}
List<String> flashModes = parameters.getSupportedFlashModes();
String flashMode = parameters.getFlashMode();
if (flashModes == null) {
return;
}
Log.i(TAG, "Flash mode: " + flashMode);
Log.i(TAG, "Flash modes: " + flashModes);
if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) {
if (flashModes.contains(Parameters.FLASH_MODE_OFF)) {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
stopWakeLock();
} else {
Log.e(TAG, "FLASH_MODE_OFF not supported");
}
}
}
}
private void startPreview() {
if (!previewOn && mCamera != null) {
mCamera.startPreview();
previewOn = true;
}
}
private void stopPreview() {
if (previewOn && mCamera != null) {
mCamera.stopPreview();
previewOn = false;
}
}
private void startWakeLock() {
if (wakeLock == null) {
Log.d(TAG, "wakeLock is null, getting a new WakeLock");
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
Log.d(TAG, "PowerManager acquired");
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);
Log.d(TAG, "WakeLock set");
}
wakeLock.acquire();
Log.d(TAG, "WakeLock acquired");
}
private void stopWakeLock() {
if (wakeLock != null) {
wakeLock.release();
Log.d(TAG, "WakeLock released");
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Eula.show(this)) {
eulaAgreed = true;
}
setContentView(R.layout.main);
button = findViewById(R.id.button);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
disablePhoneSleep();
Log.i(TAG, "onCreate");
}
To access the device camera, you must declare the CAMERA permission in your Android Manifest. Also be sure to include the <uses-feature> manifest element to declare camera features used by your application. For example, if you use the camera and auto-focus feature, your Manifest should include the following:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
A sample that checks for torch support might look something like this:
//Create camera and parameter objects
private Camera mCamera;
private Camera.Parameters mParameters;
private boolean mbTorchEnabled = false;
//... later in a click handler or other location, assuming that the mCamera object has already been instantiated with Camera.open()
mParameters = mCamera.getParameters();
//Get supported flash modes
List flashModes = mParameters.getSupportedFlashModes ();
//Make sure that torch mode is supported
//EDIT - wrong and dangerous to check for torch support this way
//if(flashModes != null && flashModes.contains("torch")){
if(flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)){
if(mbTorchEnabled){
//Set the flash parameter to off
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
else{
//Set the flash parameter to use the torch
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
//Commit the camera parameters
mCamera.setParameters(mParameters);
mbTorchEnabled = !mbTorchEnabled;
}
To turn the torch on, you simply set the camera parameter Camera.Parameters.FLASH_MODE_TORCH
Camera mCamera;
Camera.Parameters mParameters;
//Get a reference to the camera/parameters
mCamera = Camera.open();
mParameters = mCamera.getParameters();
//Set the torch parameter
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
//Comit camera parameters
mCamera.setParameters(mParameters);
To turn the torch off, set Camera.Parameters.FLASH_MODE_OFF

Categories

Resources