Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to zoom cameraX or Camera2 beyond the Api max zoom limit

So I'm trying to make a binoculars/Telescope kind of app but the problem is I want to zoom to the max. I have seen there are apps on the store that does that but not sure how. I have tried cameraX linearZoom method but it only works in range between 0f - 1f Or is it possible to do this with Camera2 api? Any help would be highly appreciate. Please note I have only posted this queston after trying or researching different things for the whole day.

Thanks

like image 290
eC Droid Avatar asked Oct 15 '22 04:10

eC Droid


1 Answers

For the curiosity of this question I decided to throw myself and try, the solution I found will not be the most conventional but it currently works.

After my first attempt I saw that it was not possible to increase the camera max_zoom so I thought of another way to enlarge what I was sending on the screen anyway.

My solution is based on the main layout in which you place the textureView in my case I decided to use a LinearLayout, that following some other guide i was able to zoom in by pinching it.

    public class ZoomLinearLayout extends LinearLayout implements ScaleGestureDetector.OnScaleGestureListener {

        private enum Mode {
            NONE,
            DRAG,
            ZOOM
        }

        private static final float MIN_ZOOM = 1.0f;
        private static final float MAX_ZOOM = 10.0f;

        private Mode mode = Mode.NONE;
        private float scale = 1.0f;
        private float lastScaleFactor = 0f;

        private float startX = 0f;
        private float startY = 0f;

        private float dx = 0f;
        private float dy = 0f;
        private float prevDx = 0f;
        private float prevDy = 0f;

        public ZoomLinearLayout(Context context) {
            super(context);
            init(context);
        }

        public ZoomLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }

        public ZoomLinearLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context);
        }

        @SuppressLint("ClickableViewAccessibility")
        public void init(Context context) {
            final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(context, this);
            this.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_DOWN:
                            if (scale > MIN_ZOOM) {
                                mode = Mode.DRAG;
                                startX = motionEvent.getX() - prevDx;
                                startY = motionEvent.getY() - prevDy;
                            }
                            break;
                        case MotionEvent.ACTION_MOVE:
                            if (mode == Mode.DRAG) {
                                dx = motionEvent.getX() - startX;
                                dy = motionEvent.getY() - startY;
                            }
                            break;
                        case MotionEvent.ACTION_POINTER_DOWN:
                            mode = Mode.ZOOM;
                            break;
                        case MotionEvent.ACTION_POINTER_UP:
                            mode = Mode.DRAG;
                            break;
                        case MotionEvent.ACTION_UP:
                            mode = Mode.NONE;
                            prevDx = dx;
                            prevDy = dy;
                            break;
                    }
                    scaleDetector.onTouchEvent(motionEvent);

                    if ((mode == Mode.DRAG && scale >= MIN_ZOOM) || mode == Mode.ZOOM) {
                        getParent().requestDisallowInterceptTouchEvent(true);
                        float maxDx = (child().getWidth() - (child().getWidth() / scale)) / 2 * scale;
                        float maxDy = (child().getHeight() - (child().getHeight() / scale)) / 2 * scale;
                        dx = Math.min(Math.max(dx, -maxDx), maxDx);
                        dy = Math.min(Math.max(dy, -maxDy), maxDy);
                        applyScaleAndTranslation();
                    }

                    return true;
                }
            });
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector scaleDetector) {
            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector scaleDetector) {
            float scaleFactor = scaleDetector.getScaleFactor();
            if (lastScaleFactor == 0 || (Math.signum(scaleFactor) == Math.signum(lastScaleFactor))) {
                scale *= scaleFactor;
                scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));
                lastScaleFactor = scaleFactor;
            } else {
                lastScaleFactor = 0;
            }
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector scaleDetector) {
        }

        private void applyScaleAndTranslation() {
            child().setScaleX(scale);
            child().setScaleY(scale);
            child().setTranslationX(dx);
            child().setTranslationY(dy);
        }

        private View child() {
            return getChildAt(0);
        }

    }

What manages the maximum of our zoom is no longer the power of the camera but our variable MAX_ZOOM witch in our case is set to 10.0f (but you can actually change it).

The next step is to enclose the TextureView used in camera2 between the ZoomLinearLayout:

    <com.giacomociardini.zoom.ZoomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000" />

    </com.giacomociardini.zoom.ZoomLinearLayout>

The FrameLayout is only a container for the fragment (the TextureView) that is in charge of all the hard work for the camera2, this is my final result:

video

photo

Notice: the results are made by only zooming inside the view, if you combine it with the zoom of the camera you are gonna get a result way much smoother!

like image 119
G. Ciardini Avatar answered Nov 02 '22 03:11

G. Ciardini