Skip to content Skip to sidebar Skip to footer

Pictures With Camera2 Api Are Really Dark

I'm working on Android and I'm trying to capture a picture without displaying any preview. I tried to simplify the process by making a class. It's working but all the pictures are

Solution 1:

If the only capture request you send to the camera is the one for the final picture, this is not surprising.

The camera automatic exposure, focus, and white balance routines generally need a second or two of streaming buffers before they converge to good results.

While you don't need to draw preview on screen, the simplest method here is to first run a repeating request targeting a dummy SurfaceTexture for a second or two, and then fire off the JPEG capture. You could just stream the JPEG capture, but JPEG capture is slow, so you'll need a longer time for convergence (plus it's more likely a camera implementation has a bug with repeated JPEG capture and getting good exposure, than with a typical preview).

So, create a dummy SurfaceTexture with a random texture ID argument:

privateSurfaceTexturemDummyPreview=newSurfaceTexture(1);
privateSurfacemDummySurface=newSurface(mDummyPreview);

and include the Surface in your session configuration. Once the session is configured, create a preview request that targets the dummy preview, and after N capture results have come in, submit the capture request for the JPEG you want. You'll want to experiment with N, but probably ~30 frames is enough.

Note that you're still not dealing with:

  • Locking AF to ensure a sharp image for your JPEG
  • Running AE precapture to allow for flash metering, if you want to allow for flash use
  • Having some way for the user to know what they'll be capturing, since there's no preview, they can't aim the device at anything very well.

The AF lock and precapture trigger sequences are included in Camera2Basic sample here: https://github.com/googlesamples/android-Camera2Basic, so you can take a look at what those do.

Solution 2:

Maybe you can try to turn on the automatic exposure mode and automatic white balance:

request.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
request.set(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_AUTO);

I hope it will help :)

Solution 3:

In my case just configuration FPS helps me. And don't forget to put it to CaptureRequest.Builder for preview and ALSO to CaptureRequest.Builder capture builder. As usual FPS 10 or 15 frames quite enough for photo and preview.

Capture builder

// This is the CaptureRequest.Builder that we use to take a picture.final CaptureRequest.BuildercaptureBuilder= mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
...
setupFPS(captureBuilder);

Preview builder:

// We set up a CaptureRequest.Builder with the output Surface.
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(surface);
                ...
// set FPS ratesetupFPS(mPreviewRequestBuilder);

Where setupFPS:

privatevoidsetupFPS(CaptureRequest.Builder builder){
        if(fpsRange != null) {
            builder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, fpsRange);
        }
}

And initialization of FPS with:

CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
try {
                    Range<Integer>[] ranges = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
                    if(ranges != null) {
                        for (Range<Integer> range : ranges) {
                            int upper = range.getUpper();
                            Log.i(TAG, "[FPS Range Available]:" + range);
                            if (upper >= 10) {
                                if (fpsRange == null || upper < fpsRange.getUpper()) {
                                    fpsRange = range;
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "[FPS Range] is:" + fpsRange);

Solution 4:

I know this is old, but I ran into a similar issue. Sharing what worked for me for anyone else who stumbles upon this. Tried all sorts of answers here with no success.

Setting CONTROL_AE_MODE to CONTROL_AE_MODE_ON (as some suggested) also did not fix it (you think it would).

What fixed it for me was setting the CONTROL_CAPTURE_INTENT to CONTROL_CAPTURE_INTENT_VIDEO_RECORD.

request.set(CaptureRequest.CONTROL_CAPTURE_INTENT, CaptureRequest.CONTROL_CAPTURE_INTENT_VIDEO_RECORD);

Once adding this line and building, auto exposure was enabled on the camera as expected and the camera adjusted automatically.

Refer to https://developer.android.com/reference/android/hardware/camera2/CameraMetadata for more information. I used this as a guide to discover what options were available.

Post a Comment for "Pictures With Camera2 Api Are Really Dark"