refactored code for zoom and exposure

pull/488/head
Suneet Agrawal 6 years ago
parent 3107e5e8f1
commit c5ba03b7da
  1. 15
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java
  2. 57
      cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java

@ -56,7 +56,6 @@ public class CameraOptions {
private Set<AspectRatio> supportedVideoAspectRatio = new HashSet<>(3); private Set<AspectRatio> supportedVideoAspectRatio = new HashSet<>(3);
private boolean zoomSupported; private boolean zoomSupported;
private float maxZoomValue;
private boolean exposureCorrectionSupported; private boolean exposureCorrectionSupported;
private float exposureCorrectionMinValue; private float exposureCorrectionMinValue;
private float exposureCorrectionMaxValue; private float exposureCorrectionMaxValue;
@ -204,8 +203,7 @@ public class CameraOptions {
//zoom //zoom
Float maxZoom = cameraCharacteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM); Float maxZoom = cameraCharacteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);
if(maxZoom != null) { if(maxZoom != null) {
maxZoomValue = maxZoom; zoomSupported = maxZoom > 1;
zoomSupported = maxZoomValue > 1;
} }
@ -485,15 +483,4 @@ public class CameraOptions {
public float getExposureCorrectionMaxValue() { public float getExposureCorrectionMaxValue() {
return exposureCorrectionMaxValue; return exposureCorrectionMaxValue;
} }
/**
* The maximum value of digital zoom available by Hardware.
* This is presumably 1 and above than 1 in case zoom is avaible.
*
* @return max zoom value
*/
@SuppressWarnings("WeakerAccess")
public float getMaxZoomValue() {
return maxZoomValue;
}
} }

@ -924,14 +924,22 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
public void run() { public void run() {
if (getEngineState() == STATE_STARTED && mCameraOptions.isZoomSupported()) { if (getEngineState() == STATE_STARTED && mCameraOptions.isZoomSupported()) {
Rect newRect = getZoomRect(zoom * mCameraOptions.getMaxZoomValue());
mRepeatingRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, newRect); Float maxZoom = mCameraCharacteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);
applyRepeatingRequestBuilder();
mZoomValue = zoom;
if (notify) { if (maxZoom != null) {
mCallback.dispatchOnZoomChanged(zoom, points); //converting 0.0f-1.0f zoom scale to the actual camera digital zoom scale
//(which will be for example, 1.0-10.0)
float calculatedZoom = (zoom * (maxZoom - 1.0f)) + 1.0f;
Rect newRect = getZoomRect(calculatedZoom, maxZoom);
mRepeatingRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, newRect);
applyRepeatingRequestBuilder();
mZoomValue = zoom;
if (notify) {
mCallback.dispatchOnZoomChanged(zoom, points);
}
} }
} }
@ -941,19 +949,16 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
} }
@NonNull @NonNull
private Rect getZoomRect(float zoomLevel) { private Rect getZoomRect(float zoomLevel, float maxDigitalZoom) {
float maxZoom = mCameraOptions.getMaxZoomValue();
Rect activeRect = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE); Rect activeRect = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
if ((zoomLevel <= maxZoom) && (zoomLevel > 1) && activeRect != null) { if ((zoomLevel <= maxDigitalZoom) && (zoomLevel > 1) && activeRect != null) {
int minW = (int) (activeRect.width() / maxZoom); int minW = (int) (activeRect.width() / maxDigitalZoom);
int minH = (int) (activeRect.height() / maxZoom); int minH = (int) (activeRect.height() / maxDigitalZoom);
int difW = activeRect.width() - minW; int difW = activeRect.width() - minW;
int difH = activeRect.height() - minH; int difH = activeRect.height() - minH;
int cropW = difW / 100 * (int) zoomLevel; int cropW = difW / 20 * (int) zoomLevel;
int cropH = difH / 100 * (int) zoomLevel; int cropH = difH / 20 * (int) zoomLevel;
cropW -= cropW & 3;
cropH -= cropH & 3;
return new Rect(cropW, cropH, activeRect.width() - cropW, activeRect.height() - cropH); return new Rect(cropW, cropH, activeRect.width() - cropW, activeRect.height() - cropH);
} }
@ -961,8 +966,23 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
} }
@Override @Override
public void setExposureCorrection(float EVvalue, @NonNull float[] bounds, @Nullable PointF[] points, boolean notify) { public void setExposureCorrection(final float EVvalue, @NonNull final float[] bounds, @Nullable final PointF[] points, final boolean notify) {
// TODO mHandler.run(new Runnable() {
@Override
public void run() {
if (getEngineState() == STATE_STARTED && mCameraOptions.isExposureCorrectionSupported()) {
mRepeatingRequestBuilder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, (int) EVvalue);
applyRepeatingRequestBuilder();
mExposureCorrectionValue = EVvalue;
if (notify) {
mCallback.dispatchOnExposureCorrectionChanged(EVvalue, bounds, points);
}
}
mExposureCorrectionOp.end(null);
}
});
} }
//endregion //endregion
@ -1264,5 +1284,4 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
}; };
//endregion //endregion
} }
Loading…
Cancel
Save