本文介绍了Android Camera2手柄缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android Camera2 API的新手.我将所有项目移至新的Camera2 API.我已经使用了"Camera2基本"示例作为起点.

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;
}

这:

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.

更新需要将captureBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);添加到captureStillPicture()方法.

UpdateNeed to add captureBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom); to captureStillPicture() method.

推荐答案

需要添加captureBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);captureStillPicture()方法.

这篇关于Android Camera2手柄缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 20:57