我正在使用SurfaceView
来显示我捕获的预览。我想使用宽度= 1080,高度= 1920进行预览。在哪里可以设置预览的大小?
我用谷歌搜索了答案,但它们都是相机版本一。我正在使用android.hardware.camera2。
private void takePreview() {
try {
final CaptureRequest.Builder previewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
previewRequestBuilder.addTarget(mSurfaceHolder.getSurface());
mCameraDevice.createCaptureSession(Arrays.asList(mSurfaceHolder.getSurface(), mImageReader.getSurface()), new CameraCaptureSession.StateCallback() // ③
{
@Override
public void onConfigured(CameraCaptureSession cameraCaptureSession) {
if (null == mCameraDevice) return;
mCameraCaptureSession = cameraCaptureSession;
try {
previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
previewRequestBuilder.set(CaptureRequest.JPEG_THUMBNAIL_SIZE, new Size(1080,1920));
CaptureRequest previewRequest = previewRequestBuilder.build();
mCameraCaptureSession.setRepeatingRequest(previewRequest, null, childHandler);
} catch (CameraAccessException e) {
Log.e("takePreview","onConfigured(CameraCaptureSession cameraCaptureSession)",e);
}
}
@Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
Log.e("takePreview","onConfigureFailed");
}
}, childHandler);
} catch (CameraAccessException e) {
Log.e("takePreview","CameraAccessException");
}
}
如 createCaptureSession 的引用中所述:
对于绘制到 SurfaceView:创建 SurfaceView 的 Surface 后,使用 setFixedSize(int, int( 将 Surface 的大小设置为 getOutputSizes(SurfaceHolder.class( 返回的大小之一,然后通过调用 getSurface(( 获取 Surface((。如果应用程序未设置大小,则相机设备将将其舍入到最接近的受支持大小小于 1080p。
看看 Google 在 GitHub 上提供的 Camera2Basic 示例: https://github.com/googlesamples/android-Camera2Basic
主片段中有一种方法可以为给定设备选择最佳预览大小。如果你想让你的应用更灵活,而不是硬编码大小,这可能是一个更好的方法,但即使你仍然宁愿坚持设定的大小,你也可以看到他们如何使用结果。
总结一下,您只需将TextureView的大小设置为您想要的任何大小预览即可。
方法名称是"选择最优大小",它包括以下注释/解释:
/**
* Given {@code choices} of {@code Size}s supported by a camera, choose the smallest one that
* is at least as large as the respective texture view size, and that is at most as large as the
* respective max size, and whose aspect ratio matches with the specified value. If such size
* doesn't exist, choose the largest one that is at most as large as the respective max size,
* and whose aspect ratio matches with the specified value.
*
* @param choices The list of sizes that the camera supports for the intended output
* class
* @param textureViewWidth The width of the texture view relative to sensor coordinate
* @param textureViewHeight The height of the texture view relative to sensor coordinate
* @param maxWidth The maximum width that can be chosen
* @param maxHeight The maximum height that can be chosen
* @param aspectRatio The aspect ratio
* @return The optimal {@code Size}, or an arbitrary one if none were big enough
*/
设置预览大小:mSurfaceTexture.setDefaultBufferSize(width, height);
onSurfaceTextureAvailable
函数。
要设置拍摄(图片(大小:ImageReader.newInstance(width, height, format, maxSize);