Reputation: 723
I have implemented the Camera2 api. It works well on most devices, but I've got a few reports from users that it won't let them take pictures. I got logs from those users. All of them are getting a ERROR_CAMERA_DEVICE error in the onError method of the CameraDevice.StateCallback I pass in when opening the front facing camera. This error states that a fatal error has happened with the camera and it needs to be reopened to be used. https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DEVICE
I wrote code that reopens the camera, but every time, the error happens again.
Does anyone know why this might be happening and how I can fix it?
Here is some of the relevant implementation:
override fun open(): Boolean {
if (hasCamera) {
try {
val id = manager.cameraIdList[cameraId]
val characteristics = manager.getCameraCharacteristics(id)
val map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
map?.let {
imageDimension = map.getOutputSizes(SurfaceTexture::class.java)[0]
}
if (!isOpen) {
manager.openCamera(id, object : CameraDevice.StateCallback() {
override fun onOpened(camera: CameraDevice) {
isOpen = true
openCount++
this.cameraDevice = camera
startPreview(null)
}
override fun onDisconnected(camera: CameraDevice) {
this.cameraDevice = camera
close()
}
override fun onError(camera: CameraDevice, error: Int) {
this.cameraDevice = camera
close()
this.cameraDevice = null
if (openCount < 10) {
openCount++
open()
}
}
}, null)
}
} catch (e: Exception) {
Timber.e("open: $e")
} catch (e: SecurityException) {
Timber.e("open: $e")
}
}
return hasCamera
}
override fun startPreview(startPreviewFailCallback: (() -> Unit)?) {
cameraDevice?.let { cameraDevice ->
try {
val texture = textureView.surfaceTexture ?: return
imageDimension?.let {
texture.setDefaultBufferSize(it.width, it.height)
} ?: kotlin.run {
texture.setDefaultBufferSize(640, 480)
}
surface = Surface(texture)
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
captureRequestBuilder.addTarget(surface)
val range = getRange()
range?.let {
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, getRange())
}
cameraDevice.createCaptureSession(Arrays.asList(surface), object : CameraCaptureSession.StateCallback() {
override fun onConfigured(@NonNull cameraCaptureSession: CameraCaptureSession) {
//The camera is already closed
if (null == cameraDevice) {
return
}
// When the session is ready, we start displaying the preview.
[email protected] = cameraCaptureSession
updatePreview()
}
override fun onConfigureFailed(@NonNull cameraCaptureSession: CameraCaptureSession) {
this.cameraCaptureSession = cameraCaptureSession
}
}, null)
} catch (e: CameraAccessException) {
e.printStackTrace()
Timber.e("startPreview: $e")
} catch (e: SecurityException) {
Timber.e("startPreview: $e")
}
} ?: kotlin.run {
startPreviewFailCallback?.let {
it.invoke()
}
}
}
private fun updatePreview() {
if (null == cameraDevice) {
return
}
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO)
try {
cameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, backgroundHandler)
} catch (e: CameraAccessException) {
e.printStackTrace()
Timber.e("updatePreview: $e")
} catch (e: IllegalStateException) {
Timber.e("updatePreview: $e")
}
}
Upvotes: 5
Views: 4806
Reputation: 723
I finally figured it out. The texture view default buffer size was using too large of a size. I fixed it by iterating over the array of output sizes from camera characteristics map and used the largest size that was under 960 x 1200
val map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
map?.let {
val sizesArray = map.getOutputSizes(SurfaceTexture::class.java)
var smallest: Size = Size(0, 0)
for (item in sizesArray) {
Timber.i("jpegSize: width: ${item.width}, height: ${item.height}")
if (item.height > smallest.height && item.height < 960 && item.width < 1200) {
smallest = item
}
}
imageDimension = smallest
}
and here's where I used the imageDimension when starting the preview
imageDimension?.let {
texture.setDefaultBufferSize(it.width, it.height)
}
Upvotes: 3