I'm new in Android Camera2 API. I just move my all project to the new Camera2 API. I have used the Camera2Basic example as a starting point.
I'm now trying handle zoom by adding this:
public boolean onTouchEvent(MotionEvent event) {
try {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId);
float maxZoom = (characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM))*10;
Rect m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
int action = event.getAction();
float current_finger_spacing;
if (event.getPointerCount() > 1) {
// Multi touch logic
current_finger_spacing = getFingerSpacing(event);
if(finger_spacing != 0){
if(current_finger_spacing > finger_spacing && maxZoom > zoom_level){
zoom_level++;
}
else if (current_finger_spacing < finger_spacing && zoom_level > 1){
zoom_level--;
}
int minW = (int) (m.width() / maxZoom);
int minH = (int) (m.height() / maxZoom);
int difW = m.width() - minW;
int difH = m.height() - minH;
int cropW = difW /100 *(int)zoom_level;
int cropH = difH /100 *(int)zoom_level;
cropW -= cropW & 3;
cropH -= cropH & 3;
Rect zoom = new Rect(cropW, cropH, m.width() - cropW, m.height() - cropH);
mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
}
finger_spacing = current_finger_spacing;
}
else{
if (action == MotionEvent.ACTION_UP) {
//single touch logic
}
}
try {
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback,
null);
}
catch (CameraAccessException e) {
e.printStackTrace();
}
catch (NullPointerException ex)
{
ex.printStackTrace();
}
}
catch (CameraAccessException e)
{
throw new RuntimeException("can not access camera.", e);
}
return true;
}
And this:
private float getFingerSpacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
But after I captured, the picture result is without the zoom. How can I make it happen? Thanks all.
Update
Need to add captureBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
to captureStillPicture()
method.
Need to add
captureBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
to thecaptureStillPicture()
method.Global:
Listener:
Just set
SCALER.CROP.REGION
to thecaptureBuilder
. You can do it in this way:In other way, if you want to keep the Zoom in a preference. I suggest you do something like this: Save the Rect in a preference as a String and later on recover it to use it, or call it every time you open the camera:
Later call this method to set the zoom dynamically:
You've only set the
SCALER_CROP_REGION
on theCaptureRequestBuilder
for the recurring preview camera output. You just need to add the same crop region property to theCaptureRequestBuilder
that uses theImageSaver
's JPEGSurface
as the output, and you should be all set.