0

Hello I am new to android. I am working on app which uses camera2 api and I am trying to implement Zoom. I figured out a method but there is no affect on the zoom while testing the camera app on my android phone (means, there is no zoom when I pinch to zoom). Please tell me what am I missing or anything that I am not doing correctly. thank you.

I am implementing the following 2 functions to implement the camera zoom.

public boolean onTouchEvent(MotionEvent event) {
        try {
            CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
            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);
                    captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom);
                }
                finger_spacing = current_finger_spacing;
            }
            else{
                if (action == MotionEvent.ACTION_UP) {
                    //single touch logic
                }
            }

            try {
                mCameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), captureCallbackListener,
                        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 (float)Math.sqrt(x * x + y * y);
    }
shah lamaan
  • 35
  • 2
  • 6

0 Answers0