Jetpack CameraX拍照预览同时存在时,绑定异常问题记录

在使用AndroidCameraX库时,如果ImageAnalysis和ImageCapture同时设置为YUV_420_888编码格式,会导致应用程序崩溃。问题源于编码冲突,因为ImageCapture默认是JPEG格式。解决方案是确保ImageCapture保持默认的JPEG格式,避免设置为YUV_420_888。
摘要由CSDN通过智能技术生成

使用CameraX时,ImageAnalysis默认编码格式为ImageFormat.YUV_420_888,此时同时添加ImageCapture,若ImageCapture编码格式也设置为ImageFormat.YUV_420_888格式,会导致崩溃,报如下错误:

java.lang.IllegalArgumentException: No supported surface combination is found for camera device - Id : 0. May be attempting to bind too many use cases.

ImageCapture默认编码格式为ImageFormat.JPEG,保持默认格式即可。

原因未知,猜测可能编码冲突

   // Set up the capture use case to allow users to take photos
        val imageCapture = ImageCapture.Builder()
            .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
            .setBufferFormat(ImageFormat.YUV_420_888)//有ImageAnalysis 时不能使用ImageFormat.YUV_420_888格式,可能抢占同编码一通道 会崩溃
            .setTargetResolution(Size(1280, 720)).build()

        // Attach use cases to the camera with the same lifecycle owner
        var imageAnalysis: ImageAnalysis? = null
        frameListener?.let { frameListener ->
            imageAnalysis = ImageAnalysis.Builder()
                .setTargetResolution(Size(1280, 720))
                .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                .build().apply { setAnalyzer(mCameraExecutor, frameListener) }
        }

附完整示例:

package com.gcsdk.cameraxutils

import androidx.camera.lifecycle.ProcessCameraProvider
import kotlin.jvm.JvmOverloads
import androidx.camera.view.PreviewView
import com.gcsdk.cameraxutils.CameraXUtils
import androidx.core.content.ContextCompat
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.ImageFormat
import android.util.Log
import android.util.Size
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.*
import androidx.camera.core.ImageCapture.O
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值