我一直在使用 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();
}
}
谢谢