Android:如何使用camera2 API进行缩放

时间:2017-09-09 17:19:41

标签: java android camera android-camera2

我是Android开发的新手,我正在创建处理相机的应用程序,但我有放大功能的问题。我正在使用谷歌的camera2代码示例,我使用此处的解决方案进行了修改,但仍无效。

public float finger_spacing = 0;
public int zoom_level = 1;
.
.
.
public boolean onTouch(View v, MotionEvent event) {
            try { 
                Activity activity = getActivity();
                CameraManager manager = (CameraManager) activity.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; 
            } 


//Determine the space between the first two fingers 
@SuppressWarnings("deprecation") 
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);
}

我在Camera2BasicFragment类的末尾添加了它,但它什么都没改变。

0 个答案:

没有答案