我正在将Camera2 API集成到我的Android应用中,并在Samsung Galaxy Tab S2上对其进行测试。我需要使用实时预览框架。 这是我的代码:
private val onPreviewAvailableListener = ImageReader.OnImageAvailableListener { reader ->
if (reader != null) {
val image = reader.acquireNextImage()
if (image != null) {
// do something with image
image.close()
}
}
}
private var mPreviewRequestBuilder: CaptureRequest.Builder? = null
private lateinit var mPreviewSize: Size
private fun configurePreview() {
// some code here
// ...
previewImageReader = ImageReader.newInstance(
mPreviewSize.width,
mPreviewSize.height,
ImageFormat.YUV_420_888,
2
)
previewImageReader?.setOnImageAvailableListener(onPreviewAvailableListener, backgroundHandler)
// some code here
// ...
}
// other code
private fun createCameraPreviewSession() {
mPreviewRequestBuilder = mCameraDevice?.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
mPreviewRequestBuilder?.addTarget(previewImageReader?.surface)
mPreviewRequestBuilder?.addTarget(surface) // surfaceView
mCameraDevice?.createCaptureSession(
Arrays.asList(previewImageReader?.surface, surface),
object : CameraCaptureSession.StateCallback() { },
null /* null handler */
}
我需要onPreviewAvailableListener
中的图像具有高分辨率。他们是。分辨率不太高。约1280 * 720。问题是我的预览TextureView
滞后了,好像设置了15 FPS。
也许我可以减少进入onPreviewAvailableListener
的图像的频率。我尝试设置一个标志以仅处理onPreviewAvailableListener
的{{1}}中每10到20幅图像,但没有任何变化:
onImageAvailable
我还尝试专门为private val onPreviewAvailableListener = object: ImageReader.OnImageAvailableListener {
private var counter = 10
fun onImageAvailable(reader : ImageReader) {
if (counter % 10 == 0) {
if (reader != null) {
val image = reader.acquireNextImage()
if (image != null) {
// do something with image
image.close()
}
}
}
if (counter == Int.MAX_VALUE) {
counter = 0
} else {
counter++
}
}
}
创建一个新的后台线程,以便在该线程上调用其回调方法。均未更改:
onPreviewAvailableListener