gpt4 book ai didi

Android Camera2 API 裁剪视频

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:34 57 4
gpt4 key购买 nike

我正在尝试使用 Android Camera2 API 录制视频。我正在尝试通过在请求生成器中设置 SCALER_CROP_REGION 将视频裁剪为正方形。我正在使用以下代码,但它似乎不起作用

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

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

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,那么您最终会得到

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。

所以录制的mZoom是

Rect(28, 16, 2972, 2972).

它不是居中的,许多设备只支持居中缩放。但它至少是一个正方形区域。

真正的问题是您无法通过裁剪来更改输出的纵横比 - 当您创建捕获 session 时,纵横比由输出表面的分辨率固定。参见 these diagrams了解裁剪的工作原理。

关于Android Camera2 API 裁剪视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42583739/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com