Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera2 API - Set AE-regions not working

In my Camera2 API project for Android, I want to set a region for my Exposure Calculation. Unfortunately it doesn't work. On the other side the Focus region works without any problems.

Device: Samsung S7 / Nexus 5

1.) Initial values for CONTROL_AF_MODE & CONTROL_AE_MODE

mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);

2.) Create the MeteringRectangle List

meteringFocusRectangleList = new MeteringRectangle[]{new MeteringRectangle(0,0,500,500,1000)};

3.) Check if it is supported by the device and set the CONTROL_AE_REGIONS (same for CONTROL_AF_REGIONS)

if (camera2SupportHandler.cameraCharacteristics.get(CameraCharacteristics.CONTROL_MAX_REGIONS_AE) > 0) {
      camera2SupportHandler.mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS, meteringFocusRectangleList);
}

4.) Tell the camera to start Exposure control

camera2SupportHandler.mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_START);

The CONTROL_AE_STATE is always in CONTROL_AE_STATE_SEARCHING, but doesn't use the configured regions...

like image 396
Manuel Schmitzberger Avatar asked Feb 06 '23 01:02

Manuel Schmitzberger


2 Answers

After long testing & development I've found an answer.

  1. The coordinate system - Camera 1 API VS Camera 2 API

RED = CAM1; GREEN = CAM2; As shown in the image below, the blue rect are the coordinates for a possible focus/exposure area for the Cam1. By using the Cam2 API, there must be firstly queried the max of the height and the width. Please find more info here.

enter image description here

  1. Initial values for CONTROL_AF_MODE & CONTROL_AE_MODE: See in the question above.

  2. Set the CONTROL_AE_REGIONS: See in the question above.

  3. Set the CONTROL_AE_PRECAPTURE_TRIGGER.

// This is how to tell the camera to start AE control

                CaptureRequest captureRequest = camera2SupportHandler.mPreviewRequestBuilder.build();
              camera2SupportHandler.mCaptureSession.setRepeatingRequest(captureRequest, captureCallbackListener, camera2SupportHandler.mBackgroundHandler);
                camera2SupportHandler.mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
                camera2SupportHandler.mCaptureSession.capture(captureRequest, captureCallbackListener, camera2SupportHandler.mBackgroundHandler);
  1. The ''captureCallbackListener'' gives feedback of the AE control (of course also for AF control)

So this configuration works for the most Android phones. Unfortunately it doesn't work for the Samsung S6/7. For this reason I've tested their Camera SDK, which can be found here.

After deep investigations I've found the config field ''SCaptureRequest.METERING_MODE''. By setting this to the value of ''SCaptureRequest.METERING_MODE_MANUAL'', the AE area works also the Samsung phones.

I'll add an example to github asap.

like image 65
Manuel Schmitzberger Avatar answered Feb 15 '23 04:02

Manuel Schmitzberger


Recently I had the same problem and finally found a solution that helped me.

All I needed to do was to step 1 pixel from the edges of the active sensor rectangle. In your example instead of this rectangle:

meteringRectangleList = new MeteringRectangle[]{new MeteringRectangle(0,0,500,500,1000)};

I would use this:

meteringRectangleList = new MeteringRectangle[]{new MeteringRectangle(1,1,500,500,1000)};

and it started working as magic on both Samsung and Nexus 5! (note that you should also step 1 pixel from right/bottom edges if you use maximum values there)

It seems that many vendors have poorly implemented this part of documentation

If the metering region is outside the used android.scaler.cropRegion returned in capture result metadata, the camera device will ignore the sections outside the crop region and output only the intersection rectangle as the metering region in the result metadata. If the region is entirely outside the crop region, it will be ignored and not reported in the result metadata.

like image 43
Mikhail Avatar answered Feb 15 '23 02:02

Mikhail