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
}