gpt4 book ai didi

Android CameraX 不显示任何内容

转载 作者:太空狗 更新时间:2023-10-29 16:25:37 35 4
gpt4 key购买 nike

我实现了一个新示例,这里是 a link它描述了来自 Google codelabs 的新 CameraX api,但 TextureView 没有显示任何内容并抛出下一个异常:

OpenGLRenderer:[SurfaceTexture-0-7609-1] dequeueImage:SurfaceTexture 未附加到 View

其他相机示例(例如 Camera2 和 native 相机应用程序)工作正常我使用了 API 级别 Q beta 3 的模拟器

class CameraXFragment : Fragment(), TextureView.SurfaceTextureListener {

companion object {
fun newInstance(): Fragment = CameraXFragment()
}

private val REQUEST_CODE_PERMISSIONS = 10
private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_camera, container, false)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewFinder.surfaceTextureListener = this
}

private fun startCamera() {
CameraX.unbindAll()

val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(Rational(1, 1))
setTargetResolution(Size(320, 320))
}.build()

val preview = Preview(previewConfig)
preview.setOnPreviewOutputUpdateListener {
viewFinder.surfaceTexture = it.surfaceTexture
updateTransform()
}

val imageCaptureConfig = ImageCaptureConfig.Builder()
.apply {
setTargetAspectRatio(Rational(1, 1))
setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
}.build()

val imageCapture = ImageCapture(imageCaptureConfig)
captureButton.setOnClickListener {
val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "${System.currentTimeMillis()}.jpg")
imageCapture.takePicture(file,
object : ImageCapture.OnImageSavedListener {
override fun onError(error: ImageCapture.UseCaseError, message: String, t: Throwable?) {
t?.printStackTrace()
}

override fun onImageSaved(file: File) {
val msg = "Photo capture succeeded: ${file.absolutePath}"
Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
}
})
}

CameraX.bindToLifecycle(this, preview, imageCapture)
}

private fun updateTransform() {
val matrix = Matrix()
val centerX = viewFinder.width / 2f
val centerY = viewFinder.height / 2f
val rotationDegrees = when (viewFinder.display.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 270
else -> return
}
matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)
viewFinder.setTransform(matrix)
}

override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {
}

override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {
}

override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
return true
}

override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {
if (allPermissionsGranted()) {
viewFinder.post { startCamera() }
} else {
requestPermissions(REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
}
viewFinder.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
updateTransform()
}
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (allPermissionsGranted()) {
viewFinder.post { startCamera() }
} else {
Toast.makeText(requireContext(), "Permissions are not granted", Toast.LENGTH_SHORT).show()
}
}
}

private fun allPermissionsGranted(): Boolean {
for (permission in REQUIRED_PERMISSIONS) {
if (ContextCompat.checkSelfPermission(requireContext(), permission) != PackageManager.PERMISSION_GRANTED) {
return false
}
}
return true
}
}

最佳答案

需要从父 View 中删除并重新添加 TextureView,以便附加 SurfaceTexture。这是因为一旦 TextureView 附加到 View 层次结构,它就会在内部创建自己的 SurfaceTexture,而只有当父 TextureView 从 View 层次结构中移除时,内部 SurfaceTexture 才会正确分离。您应该将 preview.setOnPreviewOutputUpdateListener 更改为:

preview.setOnPreviewOutputUpdateListener {
val parent = viewFinder.parent as ViewGroup
parent.removeView(viewFinder)
viewFinder.surfaceTexture = it.surfaceTexture
parent.addView(viewFinder, 0)
updateTransform()
}

看起来您可能已经从 Codelab 复制了代码,该代码实验室现已更新以包含 View 重新附加。 official sample还实现了此 View 重新附加。

关于Android CameraX 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56064248/

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