i tried pinch-zoom feature for image from this.it works fine but how can i implement pinch-zoom feature for camera preview. Please help me. Thanks in advance...
Asked
Active
Viewed 1.5k times
1 Answers
50
This is what I used to add pinch to zoom. The link to the code is here: https://github.com/maxtower/AndroidDocumentScanner/blob/master/app/src/main/java/com/martin/opencv4android/CameraPreview.java
private SurfaceHolder mHolder;
private Camera mCamera;
float mDist = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
// Get the pointer ID
Camera.Parameters params = mCamera.getParameters();
int action = event.getAction();
if (event.getPointerCount() > 1) {
// handle multi-touch events
if (action == MotionEvent.ACTION_POINTER_DOWN) {
mDist = getFingerSpacing(event);
} else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
mCamera.cancelAutoFocus();
handleZoom(event, params);
}
} else {
// handle single touch events
if (action == MotionEvent.ACTION_UP) {
handleFocus(event, params);
}
}
return true;
}
private void handleZoom(MotionEvent event, Camera.Parameters params) {
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
float newDist = getFingerSpacing(event);
if (newDist > mDist) {
//zoom in
if (zoom < maxZoom)
zoom++;
} else if (newDist < mDist) {
//zoom out
if (zoom > 0)
zoom--;
}
mDist = newDist;
params.setZoom(zoom);
mCamera.setParameters(params);
}
public void handleFocus(MotionEvent event, Camera.Parameters params) {
int pointerId = event.getPointerId(0);
int pointerIndex = event.findPointerIndex(pointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
List<String> supportedFocusModes = params.getSupportedFocusModes();
if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean b, Camera camera) {
// currently set to auto-focus on single touch
}
});
}
}
/** Determine the space between the first two fingers */
private float getFingerSpacing(MotionEvent event) {
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float)Math.sqrt(x * x + y * y);
}

mtbomb
- 1,107
- 1
- 13
- 16
-
6it works but it must be added a line for this member data: private float mDist; – user3290180 May 18 '15 at 14:40
-
1link is dead for github content ! – Mayur R. Amipara Jul 21 '15 at 05:59
-
1working perfectly.. nice work.. thnxx ! – Mayur R. Amipara Jul 21 '15 at 06:15
-
thanks :) works for me – Nininea Sep 13 '16 at 12:57
-
Above link is broken – Dharmendra Oct 05 '16 at 05:57
-
1I copy/pasted the relevant parts – mtbomb Oct 05 '16 at 14:33
-
lost clarity after adding this – Uma Achanta Jun 14 '17 at 13:04
-
add complete code – Uma Achanta Jun 14 '17 at 13:10
-
@UmaAchanta the complete code is available at the github link. – mtbomb Jun 15 '17 at 17:26
-
After adding this in my code I lost click event on Button as well as edittext – Vishal Vaishnav Aug 24 '17 at 05:24
-
works well. only change I ended up making was making the zoom on pinch a little more sensitive – dollardime Feb 07 '20 at 16:47