Я пытаюсь создать новую сессию камеры для моего приложения Android.
Я пытался использовать примеры кодов Googles из https://github.com/googlesamples/android-Camera2Basic.
Но коды не работают на моем устройстве:
/**
* 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.