3

I'm trying to implement Spot Metering using an android phone. parameters.set("auto-exposure", "spot-metering");

However, I'm a bit confused as to how the metering works. If I understand correctly, Spot Metering works by selecting a region that it uses to set exposure values.

However, when flattening the Camera.Parameters class and displaying the results, metering area was (0, 0, 0, 0, 0) [meaning it has zero width, height, and weight] while the auto-exposure setting was correctly "spot-metering". In the API, however, "getMeteringAreas()" documentation says that the metering area CANNOT have zero width and height.

So how can I be using "spot-metering" while metering-area is size 0? I tried setting the metering-area to the upper right hand corner but it didn't have any effect on exposure.

This leads me to believe that auto-exposure=spot-metering doesn't have anything to do with the metering areas....which is strange.

If that's not the case, can someone please help me understand what I'm doing wrong? If you need more information, I'll be happy to include code.

If that is the case (as in, metering areas has nothing to do with spot metering), can someone help me figure out how to CHOOSE the spot that auto-exposure meters from? I think it defaults to the center.

Thanks!

3 Answers 3

2

MeteringAreas parameter is for auto-focus metering.

There is no Android api for the exposure metering modes. You need to find proper undocumented parameter name and values to set it (for example, by dumping all parameters via native_getParameters).

For example, for the Samsung Galaxy Note 3 parameter name is metering and accepted values are center, matrix and spot. So you just call set("metering", "spot"); and it magically works.

1

Metering is a device-dependant matter.

Prior to API level 14 there wasn't a standard API for managing metering, I think that some brands chose their own way to manage it (in your case: "auto-exposure" keyword), but not through the standard APIs. This is why you cannot obtain a valid area. The metering area (0, 0, 0, 0, 0) in your parameters.flatten() means that metering areas are not supported (weight=0), i guess that getMaxNumMeteringAreas() returns 0:

apps should call getMaxNumMeteringAreas() to know the maximum number of metering areas first. If the value is 0, metering area is not supported.

By now there are few devices that support metering areas (in my experience 3/20).

1

This answer is for Camera API 1

First check if setting metering areas is supported on your device, using:

Camera.Parameters params = mCamera.getParameters();
if (params.getMaxNumMeteringAreas() > 0) {
    // Supported!
} else {
    // Not supported
}

Then, if supported, create a Rect object with your ROI (region of interest).
You need to account for camera rotation, since on different devices cameras can be oriented differently. Finding out the camera rotation is out of scope of this answer, sorry :)

private Rect getRotatedRect(Rect rect, int previewWidth, int previewHeight, int cameraRotation) {

    int resultLeft = rect.left;
    int resultTop = rect.top;
    int resultRight = rect.right;
    int resultBottom = rect.bottom;

    switch (cameraRotation) {
        case 90:
            resultLeft = top;
            resultTop = previewHeight - right;
            resultRight = bottom;
            resultBottom = previewHeight - left;
            break;
        case 180:
            resultLeft = previewWidth - right;
            resultTop = previewHeight - bottom;
            resultRight = previewWidth - left;
            resultBottom = previewHeight - top;
            break;
        case 270:
            resultLeft = previewWidth - bottom;
            resultTop = left;
            resultRight = previewWidth - top;
            resultBottom = right;
            break;
    }

    return new Rect(resultLeft, resultTop, resultRight, resultBottom);
}

Note that instead of using absolute values for X and Y coordinates,
the camera API uses coordinates from (-1000, -1000) to (1000, 1000).

So you have to scale your Rect so that (-1000, -1000) represents the top-left corner
and (1000, 1000) represents the bottom-right corner of the camera.

Example: if your camera preview size is 800x480 and your ROI is Rect(200, 120, 600, 360),
you should create a new Rect(-500, -500, 500, 500).

private Rect getScaledRect(Rect rect, int previewWidth, int previewHeight) {
    float widthUnit = previewWidth / 2000f;
    float heightUnit = previewHeight / 2000f;

    int adjustedLeft = Math.max((int) (rect.left / widthUnit - 1000), -1000);
    int adjustedTop = Math.max((int) (rect.top / heightUnit - 1000), -1000);
    int adjustedRight = Math.max((int) (rect.right / widthUnit - 1000), -1000);
    int adjustedBottom = Math.max((int) (rect.bottom / heightUnit - 1000), -1000);

    return new Rect(adjustedLeft, adjustedTop, adjustedRight, adjustedBottom);
}

Then finally set the new Rect as the metering area.
You can set the weight for your metering area (int from 0 to 1000) which determines how much your defined area influences the final exposure calculation.

To sum it all up:

Camera.Parameters params = mCamera.getParameters();
if (params.getMaxNumMeteringAreas() > 0) {
    // Supported!

    // Create rotated Rect
    Rect rotatedRect = getRotatedRect(yourAbsoluteCoordinatesROIRect, mPreviewSize.width, mPreviewSize.height, mCameraOrientation);

    // Scale Rect to make it appropriate for Camera API
    Rect scaledRect = getScaledRect(rotatedRect, mPreviewSize.width, mPreviewSize.height);

    // Create metering area with maximum weight
    Camera.Area meteringArea = new Camera.Area(scaledRect, 1000);

    // Create a list because setMeteringAreas() expects a List
    List<Camera.Area> meteringAreaList = new ArrayList<>();
    meteringAreaList.add(meteringArea);

    // Set metering area
    params.setMeteringAreas(meteringAreaList);
    mCamera.setParameters(params);
} else {
    // Not supported
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.