Android Camera2 API裁剪视频 [英] Android Camera2 API Cropping Video

查看:451
本文介绍了Android Camera2 API裁剪视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Android Camera2 API录制视频.我正在尝试通过在请求构建器中设置SCALER_CROP_REGION来将视频裁剪为正方形.我正在使用以下代码,但似乎无法正常工作

I'm trying to record a video using Android Camera2 API. I'm trying to crop video as a square by setting the SCALER_CROP_REGION in the request builder. I'm using the following code but it doesn't seem to work

mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {

    @Override
    public void onConfigured( CameraCaptureSession cameraCaptureSession) {
        mCaptureSession = cameraCaptureSession;
        try {
            mIsRecording = true;

            /////****** this where i'm setting the coping 
            mZoom = getZoomRect();
            mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, mZoom);
            /////////******************
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
            HandlerThread thread = new HandlerThread("CameraPreview");
            thread.start();
            mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, mBackgroundHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConfigureFailed( CameraCaptureSession cameraCaptureSession) {
        Log.d(TAG, "onConfigureFailed");
    }}, mBackgroundHandler);

这是应该使区域裁剪的代码

this is the code that should get the region to crop

public int zoom_level = 1;
public Rect mZoom = null;

public Rect getZoomRect(){
    try {
        CameraManager manager = (CameraManager) getApplicationContext().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 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;
        mZoom = new Rect(cropW, cropH, m.width() - cropW, m.height() - cropH);
        ///// if recording video make it square 
        if (mIsRecording) {
            mZoom = new Rect(cropW, cropH, m.width() - cropW, m.width() - cropW);
        }


    } catch (CameraAccessException e) {
        Log.e(TAG, "can not access camera",e);
        throw new RuntimeException("can not access camera.", e);
    } catch (NullPointerException ex) {
        Log.e(TAG, "touch logic",ex);
    }

    return mZoom;
}

推荐答案

假设zoom_level = 1,并且SCALER_AVAILABLE_MAX_DIGITAL_ZOOM为4,那么您最终得到

Assuming zoom_level=1, and SCALER_AVAILABLE_MAX_DIGITAL_ZOOM is 4, then you end up with

minW = m.width/40
minH = m.height/40
difW = m.width*39/40
difH = m.height*39/40
cropW = m.width*39/40/100 * 1
cropH = m.height*39/40/100 * 1
(floor cropW/cropH down to nearest multiple of 4)

如果m.width = 3000,m.width = 2000,则cropW = 28,cropH = 16.

if m.width = 3000, m.width=2000, then cropW = 28, cropH = 16.

所以录制的mZoom是

so mZoom for recording is

Rect(28, 16, 2972, 2972).

哪个没有居中,许多设备仅支持居中缩放.但这至少是一个正方形区域.

Which isn't centered, and many devices only support center-zoom. But it is a square region at least.

真正的问题是,您无法通过裁剪来更改输出的长宽比-当创建捕获会话时,长宽比由输出Surface的分辨率固定.有关裁剪的方式,请参见这些图.

The real issue is that you can't change the aspect ratio of an output by cropping - the aspect ratio is fixed by the resolution of the output Surface when you create a capture session. See these diagrams for how the cropping works.

这篇关于Android Camera2 API裁剪视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆