gpt4 book ai didi

android - 为什么我在 CameraDevice.StateCallback 的 onError 方法中得到 ERROR_CAMERA_DEVICE

转载 作者:行者123 更新时间:2023-12-04 15:33:22 32 4
gpt4 key购买 nike

我已经实现了 Camera2 api。它在大多数设备上运行良好,但我收到了一些用户的报告,称它不允许他们拍照。我从那些用户那里得到了日志。在我打开前置摄像头时传入的 CameraDevice.StateCallback 的 onError 方法中,所有这些都出现了 ERROR_CAMERA_DEVICE 错误。此错误表明相机发生了 fatal error ,需要重新打开才能使用。 https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DEVICE

我编写了重新打开相机的代码,但每次都会再次发生错误。

有谁知道为什么会发生这种情况以及我该如何解决?

下面是一些相关的实现:

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.
this@CameraNew.cameraCaptureSession = 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")
}

}

最佳答案

我终于明白了。纹理 View 默认缓冲区大小 使用的大小过大。我通过迭代相机特性图中的输出尺寸数组来修复它,并使用了 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
}

这是我在开始预览时使用 imageDimension 的地方

imageDimension?.let {
texture.setDefaultBufferSize(it.width, it.height)
}

关于android - 为什么我在 CameraDevice.StateCallback 的 onError 方法中得到 ERROR_CAMERA_DEVICE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60621345/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com