How to implement pinch-zoom feature for camera preview [closed]
Asked Answered
R

1

9

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

Ricker answered 3/9, 2013 at 14:16 Comment(0)
I
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);
}
Isaak answered 23/6, 2014 at 3:28 Comment(10)
it works but it must be added a line for this member data: private float mDist;Wardell
link is dead for github content !Touch
working perfectly.. nice work.. thnxx !Touch
Above link is brokenProbity
I copy/pasted the relevant partsIsaak
lost clarity after adding thisCastellated
add complete codeCastellated
@UmaAchanta the complete code is available at the github link.Isaak
After adding this in my code I lost click event on Button as well as edittextPulley
works well. only change I ended up making was making the zoom on pinch a little more sensitiveStolid

© 2022 - 2024 — McMap. All rights reserved.