How to implement pinch-zoom feature for camera preview

13,947

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);
}
Share:
13,947
ultimate
Author by

ultimate

Updated on July 26, 2022

Comments

  • ultimate
    ultimate over 1 year

    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...

  • user3290180
    user3290180 almost 9 years
    it works but it must be added a line for this member data: private float mDist;
  • Mayur R. Amipara
    Mayur R. Amipara almost 9 years
    link is dead for github content !
  • Mayur R. Amipara
    Mayur R. Amipara almost 9 years
    working perfectly.. nice work.. thnxx !
  • Nininea
    Nininea over 7 years
    thanks :) works for me
  • Dharmendra
    Dharmendra over 7 years
    Above link is broken
  • mtbomb
    mtbomb over 7 years
    I copy/pasted the relevant parts
  • Uma Achanta
    Uma Achanta almost 7 years
    lost clarity after adding this
  • Uma Achanta
    Uma Achanta almost 7 years
    add complete code
  • mtbomb
    mtbomb almost 7 years
    @UmaAchanta the complete code is available at the github link.
  • Vishal Vaishnav
    Vishal Vaishnav over 6 years
    After adding this in my code I lost click event on Button as well as edittext
  • dollardime
    dollardime about 4 years
    works well. only change I ended up making was making the zoom on pinch a little more sensitive