我已经实现了一个新示例,这里是一个链接,它描述了来自 Google 代码实验室的新 CameraX api,但 TextureView 没有显示任何内容并引发下一个异常:
OpenGLRenderer:[SurfaceTexture-0-7609-1] dequeueImage:SurfaceTexture 未附加到视图
另一个相机样本作为 Camera2 和本机相机应用程序工作正常我使用了带有 api 级别 Q beta 3 的模拟器
类CameraXFragment:片段(),TextureView.SurfaceTextureListener { 伴随对象{ 有趣的 newInstance(): Fragment = CameraXFragment() } 私人 val REQUEST_CODE_PERMISSIONS = 10 私有 val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE) 覆盖 fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_camera,容器,假) 覆盖乐趣 onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) viewFinder.surfaceTextureListener = 这个 } 私人乐趣 startCamera() { CameraX.unbindAll() val previewConfig = PreviewConfig.Builder().apply { setTargetAspectRatio(Rational(1, 1)) setTargetResolution(大小(320, 320)) }。建造() val 预览 = 预览(previewConfig) preview.setOnPreviewOutputUpdateListener { viewFinder.surfaceTexture = it.surfaceTexture 更新转换() } val imageCaptureConfig = ImageCaptureConfig.Builder() 。申请 { setTargetAspectRatio(Rational(1, 1)) setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY) }。建造() val imageCapture = ImageCapture(imageCaptureConfig) captureButton.setOnClickListener { val 文件 = 文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),“${System.currentTimeMillis()}.jpg”) imageCapture.takePicture(文件, 对象:ImageCapture.OnImageSavedListener { 覆盖 fun onError(error: ImageCapture.UseCaseError, message: String, t: Throwable?) { t?.printStackTrace() } 覆盖乐趣 onImageSaved(file: File) { val msg = "照片拍摄成功:${file.absolutePath}" Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show() } }) } CameraX.bindToLifecycle(this, preview, imageCapture) } 私人乐趣 updateTransform() { val 矩阵 = 矩阵() 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 否则->返回 } matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY) viewFinder.setTransform(矩阵) } 覆盖乐趣 onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) { } 覆盖乐趣 onSurfaceTextureUpdated(surface: SurfaceTexture) { } 覆盖乐趣 onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean { 返回真 } 覆盖乐趣 onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) { if (allPermissionsGranted()) { viewFinder.post { startCamera() } } 别的 { 请求权限(REQUIRED_PERMISSIONS,REQUEST_CODE_PERMISSIONS) } viewFinder.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> 更新转换() } } 覆盖乐趣 onRequestPermissionsResult(requestCode: Int, 权限: Array, grantResults: IntArray) { if (requestCode == REQUEST_CODE_PERMISSIONS) { if (allPermissionsGranted()) { viewFinder.post { startCamera() } } 别的 { Toast.makeText(requireContext(), "权限未授予", Toast.LENGTH_SHORT).show() } } } 私人乐趣 allPermissionsGranted(): Boolean { for(REQUIRED_PERMISSIONS 中的权限){ if (ContextCompat.checkSelfPermission(requireContext(), permission) != PackageManager.PERMISSION_GRANTED) { 返回假 } } 返回真 } }