Pictures with Camera2 API are really dark

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:

private SurfaceTexture mDummyPreview = new SurfaceTexture(1);
private Surface mDummySurface = new Surface(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 :)