Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android camera2 handle zoom

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

And this:

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.

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

like image 202
Eli Elezra Avatar asked Mar 13 '16 08:03

Eli Elezra


People also ask

What is Camera2 in Android?

Camera2 is the latest low-level Android camera package and replaces the deprecated Camera class. Camera2 provides in-depth controls for complex use cases, but requires you to manage device-specific configurations.

What is Level 3 Camera 2 API?

Level-3 – The OEM of the device has added some additional features to the camera hardware including YUV reprocessing, RAW image capture, and more. Full – The smartphone fully supports all major capabilities of the Camera2 API. Limited – The phone supports only some of the features of Camera2 API.

How do I get Camera2 API?

prop in the /system partition of your Android device, you can enable the Camera2 API functionality. First you'll need a rooted phone, and a method of editing your build. prop file. You can either use a root file explorer app (like ES Explorer) to navigate to the /system partition on your phone and open build.


2 Answers

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

like image 188
Eli Elezra Avatar answered Sep 27 '22 17:09

Eli Elezra


Global:

companion object {
    private const val ZOOM_GESTURE_SENSITIVITY = .0005f
    private const val MAX_ZOOM_FACTOR = 3f
}

private var fingerDistance: Float? = null
private var zoom = 1f

Listener:

textureView.setOnTouchListener({ _, event ->
    if (event.action != MotionEvent.ACTION_MOVE || event.pointerCount <= 1) {
        fingerDistance = null
        return@setOnTouchListener true
    }
    val newFingerDistance = with(event) {
        val x = getX(0) - getX(1)
        val y = getY(0) - getY(1)
        sqrt(x * x + y * y) * resources.displayMetrics.density
    }
    if (fingerDistance != null) {
        val manager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
        val characteristics = manager.getCameraCharacteristics(cameraId)
        val maxZoom = min(MAX_ZOOM_FACTOR, characteristics.get(
                CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM))
        val zoomChange = (newFingerDistance - fingerDistance!!) * ZOOM_GESTURE_SENSITIVITY
        zoom = min(maxZoom, max(1f, zoom + zoomChange))
        val sensorSize = characteristics.get(
                CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE)!!
        val cropW = (sensorSize.width() * (1 - 1 / zoom) / 2).toInt()
        val cropH = (sensorSize.width() * (1 - 1 / zoom) / 2).toInt()
        val zoomRect = Rect(cropW, cropH,
                sensorSize.width() - cropW,
                sensorSize.height() - cropH)
        previewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomRect)
    }
    fingerDistance = newFingerDistance
    try {
        captureSession
                ?.setRepeatingRequest(previewRequestBuilder.build(), captureCallback, null)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    true
})
like image 32
Hsingchien Cheng Avatar answered Sep 27 '22 18:09

Hsingchien Cheng