我一直在使用TextureView进行相机预览,直到我找不到设置背景颜色的方法,所以我切换到SurfaceView但预览不再有效。除了预览之外,其他一切工作正常。
这是我使用TextureView时的代码:
public void previewCamera(){
try {
SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(previewSize.getWidth(),previewSize.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
captureRequestBuilder = device.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(previewSurface);
device.createCaptureSession(Arrays.asList(previewSurface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
updatePreview(session);
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
Toast.makeText(getApplicationContext(),"unable to setup cam preview",Toast.LENGTH_SHORT).show();
}
},null);
} catch (CameraAccessException e) {
e.printStackTrace();
}}
..这是我使用SurfaceView的代码
public void previewCamera(){
try {
Surface previewSurface = surfaceView.getHolder().getSurface();
captureRequestBuilder = device.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(previewSurface);
device.createCaptureSession(Arrays.asList(previewSurface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
updatePreview(session);
}
...我正在添加此代码以防它与它有关
private void updatePreview(CameraCaptureSession session) {
previewSession = session;
if (null == device) {
return;
}
try {
handlerThread = new HandlerThread("CameraPreview");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
previewSession.setRepeatingRequest(captureRequestBuilder.build(), null, ((isRecording)? null:handler));
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
由于
答案 0 :(得分:0)
您是否尝试在检索Surface对象之前设置固定大小?
surfaceView.setFixedSize(previewSize.getWidth(),previewSize.getHeight());
答案 1 :(得分:0)
如果您使用 SurfaceView 进行预览,则不应使用处理程序。如下所示:
manager.openCamera("0", mStateCallback, null);
顺便说一下,SurfaceView的大小不会让预览无效,所以你不必设置修正大小。