0

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

谢谢

4

2 回答 2

0

您是否尝试在检索 Surface 对象之前设置固定大小?

surfaceView.setFixedSize(previewSize.getWidth(),previewSize.getHeight());
于 2016-11-24T23:17:42.573 回答
0

使用SurfaceView进行预览时不宜使用Handler。如下所示:

 manager.openCamera("0", mStateCallback, null);

顺便说一句,SurfaceView 的大小不会让预览不起作用,因此您不必设置固定大小。

于 2017-01-06T01:45:37.027 回答