I want to capture pictures through Camera2 API, and I need to control the exposure time. But when I set CameraDevice.TEMPLATE_MANUAL or just set the exposure time, I get black picture. When I cancel the setting, I can get the right picture again.
The following is my setting code.
// if the phone's level is full or other which can control mannually
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CONTROL_AE_MODE_OFF);
mCaptureRequestBuilder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, exposureValue);
mCaptureRequestBuilder.set(CaptureRequest.SENSOR_SENSITIVITY, isoValue);
mCaptureRequestBuilder.set(CaptureRequest.SENSOR_FRAME_DURATION,valueFrame);
//exposureValue and isoValue and valueFrame is valid.
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()),
new CameraCaptureSession.StateCallback() {
#Override
public void onConfigured(CameraCaptureSession session) {
Log.e(TAG, "onConfigured: startPreview");
mPreviewCaptureSession = session;
try {
mPreviewCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(),
null, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
#Override
public void onConfigureFailed(CameraCaptureSession session) {
Log.e(TAG, "onConfigureFailed: startPreview");
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
After setting like above, when I capture a picture or save the preview frame, I can just get a black picture. But when I cancel the manual setting, I can save the picture.
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
Related
My app shows a preview and video recording starts with a button press.
What I'm trying to achieve is to automatically turn on flashlight (torch mode) as soon as the video recording starts.
However I couldn't find a way to do so. On Camera2 API we can use FLASH_MODE_AUTO which will use the flashlight when capturing photo when the scene is dark, but that doesn't work for video recording.
There's this FLASH_MODE_TORCH which I could use to turn on the flashlight just like I wanted, but there isn't a FLASH_MODE_TORCH_AUTO to automatically do so when the scene is dark..
There were some answers that uses Ambient light sensor (Sensor.TYPE_LIGHT) of the device to determine whether we are in a dark scene, however that uses the front ambient light sensor instead of the camera itself I think. This is not ideal as the ambient light can be low but the rear camera is able to adjust exposure level to achieve good enough image quality without using flash. So ideally if the camera says 'flash is required' then only the app activates FLASH_MODE_TORCH.
Since the app shows a preview the device already know whether flash is needed before the button press, is there a way to determine whether flash is required during preview?
Please try the below method where you need you can use it
below is for Camera API
public void switchFlashOnMode() {
Camera.Parameters p = getCamera().getParameters();
try {
//p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
p.setFlashMode(Parameters.FLASH_MODE_AUTO);
getCamera().setParameters(p);
getCamera().startPreview();
isFlashTorch = true;
}catch (Exception e){
e.printStackTrace();
}
}
public void switchFlashOffMode() {
Camera.Parameters p = getCamera().getParameters();
try {
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
getCamera().setParameters(p);
Thread.sleep(200);
getCamera().stopPreview();
isFlashTorch = false;
}catch (Exception e){
e.printStackTrace();
}
}
below is for Camera2 API
void switchFlashMode() {
if (!flashSupport) return;
try {
if (isFlashTorch) {
isFlashTorch = false;
requestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
} else {
isFlashTorch = true;
//requestBuilder.set(CaptureRequest.FLASH_MODE,CameraMetadata.FLASH_MODE_TORCH);
requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
}
cameraCaptureSession.setRepeatingRequest(requestBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
hope it will help you
Finally figured this out, gpuser's answer used the right flag but it is not complete - still need to code the callback and turn on the torchlight when needed.
I also found that for video recording, we still use the same Camera2 API init and configuration steps, just that some of the callbacks will be fired multiple times, so I added a flag to perform the flash detection only once.
1)After camera started capturing, run this code
performAutoTorchDetectionOnce = true; // set this flag first, will be used later
captureRequestBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); // CONTROL_AE_MODE_ON_AUTO_FLASH is important here, to enable flash detection
captureSession.setRepeatingRequest(captureRequestBuilder.build(), captureCallback, null);
2)And this is my captureCallback implementation, change it depending on your needs. The gist of it is that eventually the camera capture will fall into one of the two states, CONTROL_AE_STATE_CONVERGED or CONTROL_AE_STATE_FLASH_REQUIRED. These two states mean that auto exposure algorithm has finished running, if it is converged means no flash is needed whereas flash_required will mean that we have to turn on flash. In the latter we will then need to manually turn on the flash in the next step.
private CameraCaptureSession.CaptureCallback captureCallback =
new CameraCaptureSession.CaptureCallback() {
#Override
public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request,
long timestamp, long frameNumber) {
super.onCaptureStarted(session, request, timestamp, frameNumber);
}
#Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState != null) {
if (performAutoTorchDetectionOnce) {
if (aeState == CameraMetadata.CONTROL_AE_STATE_CONVERGED // CONTROL_AE_STATE_CONVERGED means Auto-exposure has finished
|| aeState == CameraMetadata.CONTROL_AE_STATE_FLASH_REQUIRED) { // CONTROL_AE_STATE_FLASH_REQUIRED means Auto-exposure has finished, but flash is required
performAutoTorchDetectionOnce = false;
enableTorch(aeState == CameraMetadata.CONTROL_AE_STATE_FLASH_REQUIRED);
}
}
}
}
};
3)Here's the enableTorch implementation. I tried leaving CONTROL_AE_MODE as CONTROL_AE_MODE_ON_AUTO_FLASH but it didn't work, torch light does not turn on, so I have to change it to CONTROL_AE_MODE_ON.
public synchronized void enableTorch(boolean enable) {
Timber.d("enableTorch(" + enable + ") called");
try {
if (isCaptureStarted()) {
if (enable) {
captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
} else {
captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
}
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
captureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null);
}
} catch (CameraAccessException e) {
Timber.e(e, "enableTorch(" + enable + ") failed: ");
}
}
I need to set up CONTROL_SCENE_MODE_ACTION for my app camera2API.
i tryed to set it captureStillPicture() method then in lockFocus() method then in stateCallback but is doesn't work...
In documentation i found only explanation what it is, but any lines how this mode have to be set up...
There are 2 question:
Where exacly i have to set up this mode
How i can check that it is working
Or maybe you can suggest me how to reduse expose time...
Thanks in advance
You can modify Camera2BasicFragment from Google Camera2Basic sample by add line
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_SCENE_MODE, CaptureRequest.CONTROL_SCENE_MODE_ACTION);
just after line
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
in onConfigured() method of example
#Override
public void onConfigured(#NonNull CameraCaptureSession cameraCaptureSession) {
// The camera is already closed
if (null == mCameraDevice) {
return;
}
// When the session is ready, we start displaying the preview.
mCaptureSession = cameraCaptureSession;
try {
// Auto focus should be continuous for camera preview.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_SCENE_MODE,
CaptureRequest.CONTROL_SCENE_MODE_ACTION);
// Flash is automatically enabled when necessary.
setAutoFlash(mPreviewRequestBuilder);
// Finally, we start displaying the camera preview.
mPreviewRequest = mPreviewRequestBuilder.build();
mCaptureSession.setRepeatingRequest(mPreviewRequest,
mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
I'm developing a camera app using the Android camera2 API, And I need the camera to have always the same configuration, like focus, white balance, exposure, etc.
I already disabled AF, AE and AWB, but it still seems to automatically set the white balance. I was wondering if is there some other automatic configuration I should disable.
I didn't find anything like it on Google.
i just ran into the same issue... In my case it turned out, that there was a function in the template i was using, which reset the control mode to automatic.
protected void updatePreview() {
if(null == cameraDevice) {
Log.e(TAG, "updatePreview error, return");
}
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
try {
cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
I am doing a project related to live wallpaper, in that camera was set as wallpaper, so in my app camera was running continuously, if user set camera as wallpaper, after he opening the camera it will shows the camera failed to load error,
so, i am using the following code to avoid this error:
public void onVisibilityChanged(boolean visible) {
if (visible){
try {
mCamera.reconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
try {
mCamera.unlock();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
But my problem is could not unlock the camera object to other apps use the camera.
can any one give me an idea how can i do this?
You cannot access the object used by the native camera app or other apps to access camera. When you create mCamera, you are creating your own object which is used by your app. At a time, only one object can access a particular camera resource, and you have no control over other objects of other apps accessing that camera resource.
If you want avoid the failed to load error, you need to release your camera object before you open another app which uses camera resource. But then I do not think your main objective will be accomplished.
I am using same method with small modifications to solve the issue, i was doing following changes in the code
public void onVisibilityChanged(boolean visible) {
// TODO Auto-generated method stub
super.onVisibilityChanged(visible);
if (visible) {
try {
"open camera object"
}catch (Exception e) {
e.printStackTrace();
}
}else {
try {
"release the camera object"
}catch (RuntimeException e) {
e.printStackTrace();
}
}
}
I'm testing playing online video using chromecast.
After onRouteSelected(), I create the ApplicationSession and attach a MediaProtocalMessageStream;
Then I called mSession.startSession(); with no APP_ID, so I assume the build-in app inside chromecast play the video for me. This code works perfect and I can play online mp4 videos without writing my own receiver.
But, When I try to leave the video play app, I can't go back anymore, there is always an error message comes from onSessionStartFailed() which says
StartSessionTask failed with error: failed to start application: no
application is running
I don't remember how the first time I got into the video play app, which I don't leave for few day.
But I do know how I leave it, Here is what I did before I can never startSession again:
open Youtube app, get a deviced connected
play some youtube videos
disconnected from a chormecast, then the chromecast return to the starting page
So, doesn't anybody know what's going on here? How to open the build-in video app again?
By the way, My chromecast get a system update just after I return to the starting page, I don't know if google update something cause startSession() fail.
Below is the code I startSession and attach a mediaStream.
mSession = new ApplicationSession(mCastContext, mSelectedDevice);
ApplicationSession.Listener listener = new ApplicationSession.Listener() {
#Override
public void onSessionStarted(ApplicationMetadata appMetadata) {
mChannel = mSession.getChannel();
mStream = new MediaProtocolMessageStream();
mChannel.attachMessageStream(mStream);
if (mStream.getPlayerState() == null) {
ContentMetadata metaData = new ContentMetadata();
metaData.setTitle("Test Video");
String url = "http://www.auby.no/files/video_tests/h264_720p_hp_5.1_6mbps_ac3_planet.mp4";
try {
mCommand = mStream.loadMedia(url, metaData, true);
mCommand.setListener(new MediaProtocolCommand.Listener() {
#Override
public void onCompleted(MediaProtocolCommand arg0) {
onSetVolume(0.5);
}
#Override
public void onCancelled(MediaProtocolCommand arg0) {
}
});
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void onSessionStartFailed(SessionError error) {
Log.d("TEST", "Session Started failed");
}
#Override
public void onSessionEnded(SessionError error) {
Log.d("TEST", "Session Started end");
}
};
mSession.setListener(listener);
try {
mSession.startSession();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You will have to use your own app id and own receiver. Google's default receiver doesn't play video streams anymore (it used to). It only handles Chrome tab mirroring now.