I do following:
CaptureRequest captureRequest;
captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
captureRequest = captureRequestBuilder.build();
cameraCaptureSessions.setRepeatingRequest(captureRequest, captureCallBackListener, backgroundHandler);
...but the flash turns off before the picture was taken.
Maybe this might help:
CONTROL_AE_MODE
added in API level 21
public static final Key CONTROL_AE_MODE
The desired mode for the camera device's auto-exposure routine.
This control is only effective if android.control.mode is AUTO.
When set to any of the ON modes, the camera device's auto-exposure routine is enabled, overriding the application's selected exposure time, sensor sensitivity, and frame duration (android.sensor.exposureTime, android.sensor.sensitivity, and android.sensor.frameDuration). If one of the FLASH modes is selected, the camera device's flash unit controls are also overridden.
The FLASH modes are only available if the camera device has a flash unit (android.flash.info.available is true).
If flash TORCH mode is desired, this field must be set to ON or OFF, and android.flash.mode set to TORCH.
When set to any of the ON modes, the values chosen by the camera device auto-exposure routine for the overridden fields for a given capture will be available in its CaptureResult.
this is from here: https://developer.android.com/reference/android/hardware/camera2/CaptureRequest
Related
There are a select few camera features that are called 'extensions' (Auto, Bokeh, HDR, Night and Face Retouch).
But those aren't ALL the features a camera could have (and naturally not all you can do with the Camera/Camera2/CameraX API), such as setting white balance mode and auto focus mode, which are set in CaptureRequest options.
Why are some features 'categorised' under a separate 'thing' called 'extensions'?
My android application captures preview frames. It is necessary that the frames be not blurred.
This requires to limit the exposure time of the sensor. For example, I want the exposure time be less than 10ms, and the white-balance adjuster uses ISO only.
The only solution I found is the fixing of SENSOR_EXPOSURE_TIME and SENSOR_SENSITIVITY:
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result)
{
// measure ISO and exposure
mExposure = result.get(CaptureResult.SENSOR_EXPOSURE_TIME);
mSensitivity = result.get(CaptureResult.SENSOR_SENSITIVITY);
...
}
void prepareCapturing()
{
// setting the necessary values of ISO and exposure
if (mExposure > 10.0 * 1e+6)
{
double exposure = 10.0 * 1e+6;
double sens = mExposure * mSensitivity / exposure;
mPreviewRequestBuilder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, (long)exposure);
mPreviewRequestBuilder.set(CaptureRequest.SENSOR_SENSITIVITY, (int)sens);
}
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);
setRepeatingRequest();
}
I call the method prepareCapturing before running my algorithm of preview frame analyzing.
This approach works, but it requires to disable android.control.aeMode, thus the white balance will be off.
Also I tried to use standard scene modes like CONTROL_SCENE_MODE_ACTION, and CONTROL_SCENE_MODE_SPORTS, but exposure time anyway is about 40ms.
The question: is it possible using camera2 intereace to limit exposure time of the sensor, s.t. the white balance be active?
The primary API to require auto-exposure to remain below some maximum exposure time is the CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE control. For example, if you set that to (30,30), then the camera device may not use exposure times longer than 1/30th of a second.
The list of available ranges for a device is provided by CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES.
There's no direct control for setting max/min exposure values by themselves, and if no available target FPS range limits exposure times enough, your only other option is to use manual exposure control (if supported).
Whether that disables AWB is device-dependent, unfortunately - on some devices, the output of the auto-exposure routine is essential for the white balance algorithm to work.
You can know the lower and upper values supported by your phone using the key SENSOR_INFO_EXPOSURE_TIME_RANGE
Check out the next method:
/**
* Get exposure time range.
*
* #param cameraCharacteristics The properties of the camera.
* #return Long exposure compensation range.
* #see <a href="https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics.html#SENSOR_INFO_EXPOSURE_TIME_RANGE">
* CameraCharacteristics.SENSOR_INFO_EXPOSURE_TIME_RANGE</a>
*/
private static Range<Long> getExposureTimeRange(CameraCharacteristics cameraCharacteristics) {
return cameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_EXPOSURE_TIME_RANGE);
}
Check out Camera parameters' setExposureCompensation() method.
Here is the URL https://developer.android.com/reference/android/hardware/Camera.Parameters.html#setExposureCompensation(int)
I used camera2 to capture picture, and set the flash mode to auto. I set the request parameters as following.
CaptureRequest.Builder stillCaptureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
stillCaptureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
In the code above, the flash should be fired in low-light conditions. But it didn't work as expected. The flash didn't get fired in low-light conditions.
How can I set camera2 to auto flash mode?
Let's see if I can help you!
Try to set Flash_mode to OFF if you are using AE_MODE:
builder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON);
builder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
and later update your preview setting again a repeatingRequest to your builder and callback:
mCaptureSession.setRepeatingRequest(builder.build(),
callback, backgroundHandler);
Also remember that if you want to check if everything is set correctly you can recover the state of your flash using result.get(CaptureResult.FLASH_STATE) from your captureCalback return value after or before take a picture.
In other way, don't forget to don't update the preview when the flash is being shoot or you will put off your flash before the photo as been shoot.
try this for auto flash
// Use the same AE and AF modes as the preview.
captureStillBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
captureStillBuilder.set(CaptureRequest.CONTROL_AE_MODE,
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
I have a camera in my app and I want to make it auto focus continuously in the same way that the phone's camera does it. I found the modes FOCUS_MODE_CONTINUOUS_VIDEO and FOCUS_MODE_CONTINUOUS_PICTURE, but they are not supported by some of the HTC Gingerbread phones I'm testing on.
This is what I'm doing to determine whether I can use these modes:
Camera.Parameters parameters = mCamera.getParameters();
List<String> supportedFocusModes = parameters.getSupportedFocusModes();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}
else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
// auto focus on request only
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
Running on several different Gingerbread HTC phones I don't get the continuous modes back, but I get "auto". This lets me auto focus on demand (when I call mCamera.autoFocus(null), but the camera will not refocus if the user moves the camera.
I cannot set the focus mode to anything the camera does not support, and if I do it shows up blank.
One solution that I tried is to call mCamera.autoFocus(null) on a timer. This causes the camera to refocus continuously, even if it is already in focus.
Is there a way to implement a continuous auto focus experience on these phones? When I look at HTCs camera app on these phones it does have continuous auto focus - as you move around the camera refocuses and does not keep refocusing once the picture is in focus.
We had a requirement to support a very wide range of phones. My solution in the end was to handle each case differently.
For the scenario of phones without continuous auto-focus support I implemented a utility to listen to the accelerometer using SensorManager and trigger mCamera.autoFocus(...) whenever the user moves the camera.
There were also some older tablets that only supported fixed focus (who would use such a thing!), in that case the picture needed to be taken immediately - not on the focus callback.
For most modern phones the code snippet above was fine to use FOCUS_MODE_CONTINUOUS_PICTURE.
I got a similar pb on my samsung S4 and I solved it with:
camera.setParameters(parameters);
camera.autoFocus(null);
This is suggest in the Google doc here.
To make the camera parameters take effect, your application has to call setParameters(Camera.Parameters).
It is any way to turn off auto focus in camera in code my application. I want to check how my scanner work if phone has no auto focus, but in my phone I have that function.
Use FOCUS_MODE_INFINITY or FOCUS_MODE_FIXED. You can also use FOCUS_MODE_MACRO, but that will require holding your phone quite close to the object you're scanning.
On a second thought, the word 'scanner' evokes thoughts of barcodes and QR codes, so unless you print them as full-size page, you actually might be better off with FOCUS_MODE_MACRO.
You can set the desired focus mode with Camera.Parameters.setFocusMode() when opening your camera.
You Can use mCamera.cancelAutoFocus();
Also if you want to set Macro or another Focus Mode, you shall write:
Camera.Parameters mParam = mCamera.getParameters();
mParam.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
mCamera.setParameters(mParam);
All the focus mode and Camera parameters are availble here: