相机以编程方式缩放

时间:2018-06-06 08:03:49

标签: android android-camera

如何在Android中实现捏合缩放功能?

我有以下代码,但我收到错误,

  

java.lang.NullPointerException:尝试调用虚方法

     

' android.hardware.Camera $参数   android.hardware.Camera.getParameters()'在null对象引用上                                                                             在com.roadlike.app.Camera.CameraImageMain.onTouchEvent

以下代码:

@Override
public boolean onTouchEvent(MotionEvent event) {
    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) {
            }
        });
    }

如何修复错误以实现捏拉缩放效果?

0 个答案:

没有答案