如何创建相机会话?

时间:2019-04-28 06:57:57

标签: android-camera2

我正在尝试为我的Android应用程序创建一个新的相机会话。

我尝试使用https://github.com/googlesamples/android-Camera2Basic中的Google示例代码。

但是代码在我的设备上不起作用

    /**
     * Creates a new [CameraCaptureSession] for camera preview.
     */
    private fun createCameraPreviewSession() {
        try {
            val texture = textureView.surfaceTexture

            // We configure the size of default buffer to be the size of camera preview we want.
            texture.setDefaultBufferSize(previewSize.width, previewSize.height)

            // This is the output Surface we need to start preview.
            val surface = Surface(texture)

            // We set up a CaptureRequest.Builder with the output Surface.
            previewRequestBuilder = cameraDevice!!.createCaptureRequest(
                    CameraDevice.TEMPLATE_PREVIEW
            )
            previewRequestBuilder.addTarget(surface)

            // Here, we create a CameraCaptureSession for camera preview.
            cameraDevice?.createCaptureSession(Arrays.asList(surface, imageReader?.surface),
                    object : CameraCaptureSession.StateCallback() {

                        override fun onConfigured(cameraCaptureSession: CameraCaptureSession) {
                            // The camera is already closed
                            if (cameraDevice == null) return

                            // When the session is ready, we start displaying the preview.
                            captureSession = cameraCaptureSession
                            try {
                                // Auto focus should be continuous for camera preview.
                                previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                                        CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE)
                                // Flash is automatically enabled when necessary.
                                setAutoFlash(previewRequestBuilder)

                                // Finally, we start displaying the camera preview.
                                previewRequest = previewRequestBuilder.build()
                                captureSession?.setRepeatingRequest(previewRequest,
                                        captureCallback, backgroundHandler)
                            } catch (e: CameraAccessException) {
                                Log.e(TAG, e.toString())
                            }

                        }

                        override fun onConfigureFailed(session: CameraCaptureSession) {
                            activity.showToast("Failed")
                        }
                    }, null)
        } catch (e: CameraAccessException) {
            Log.e(TAG, e.toString())
        }

    }

运行我的应用程序时,出现以下错误:

E/Legacy-CameraDevice-JNI: LegacyCameraDevice_nativeGetSurfaceId: Could not retrieve native Surface from surface.

E/CameraDeviceGLThread-1: Received exception on GL render thread: java.lang.IllegalArgumentException: Surface had no valid native Surface.

1 个答案:

答案 0 :(得分:0)

听起来您在将Surface传递到会话中之前并没有等待Surface就绪:

cameraDevice?.createCaptureSession(Arrays.asList(surface, imageReader?.surface)

如果回溯,您会看到Surface来自TextureView:

val texture = textureView.surfaceTexture

您需要等待,直到Surface准备就绪。为此,应附加一个SurfaceTextureListener并等待调用onSurfaceTextureAvailable。然后,可以安全地检索和使用Surface进行相机会话。