Joh*_*ang 6 android android-camera2
我用a SurfaceView来显示我捕获的预览.我想使用width = 1080,height = 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");
}
}
Run Code Online (Sandbox Code Playgroud)
如createCaptureSession的引用中所述:
对于绘制到SurfaceView:创建SurfaceView的Surface后,使用setFixedSize(int,int)将Surface的大小设置为getOutputSizes(SurfaceHolder.class)返回的大小之一,然后通过调用getSurface()获取Surface .如果应用程序未设置大小,则摄像机设备将舍入到最接近支持的小于1080p的大小.
看一下 Google 在 GitHub 上提供的 Camera2Basic 示例: https: //github.com/googlesamples/android-Camera2Basic
主片段中有一个方法可以为给定设备选择最佳预览尺寸。如果您想让您的应用程序更加灵活,而不是对大小进行硬编码,这可能是一个更好的方法,但即使您仍然愿意坚持使用设定的大小,您也可以看到它们如何使用结果。
总而言之,您只需将 TextureView 的大小设置为您想要的任何预览大小。
方法名称是“chooseOptimalSize”,它包含以下注释/解释:
/**
* 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
*/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11081 次 |
| 最近记录: |