0

I am new to android development and i am creating app that deals with camera but i am having problem with the zoom in function. I am using camera2 code sample from google, i modified bit using solutions from here but still it is not working.

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

I have added this in the end of Camera2BasicFragment class but it changes nothing.

Lion King
  • 11
  • 1
  • 2
  • isn't this duplicate of https://stackoverflow.com/questions/35968315/android-camera2-handle-zoom? – Alex Cohn Sep 10 '17 at 09:35
  • Does the onTouch method get called? What values are you getting for the Rect? Add some logging and see if it all looks sane. – Eddy Talvala Sep 10 '17 at 22:49
  • @EddyTalvala that is the problem it doesn't get called at all. – Lion King Sep 12 '17 at 23:26
  • Did you link your onTouch method to the SurfaceView via https://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener) ? – Eddy Talvala Sep 13 '17 at 20:10

0 Answers0