Android 高通Camera2 Camera Device Close

 1. Many people may feel that there is no reasonable release when they see this log, so they carry out Baidu exploration with this idea

2. At the beginning, I searched for the problem of ImageReader.OnImageAvailableListener

   var afterBitmap: Bitmap? = null
    /**监听拍照的图片 */
    private val imageAvailableListener =
        ImageReader.OnImageAvailableListener { reader ->
            try {
//                ThreadPoolManager.getInstance().execute {
                    // 获取捕获的照片数据
                    val image = reader.acquireNextImage()
                    val buffer = image.planes[0].buffer
                    val data = ByteArray(buffer.remaining())
                    buffer[data]
//                val bitmapByteArray = BitmapUtil.compressInSampleSize(data, 800, 480)
//                afterBitmap =
//                    BitmapFactory.decodeByteArray(bitmapByteArray, 0, bitmapByteArray.size)
                    //显示图片
                    val options =
                        BitmapFactory.Options()
                    options.inSampleSize = 2
                        val bitmap =
                            BitmapFactory.decodeByteArray(data, 0, data.size, options)
                         requireActivity().runOnUiThread(){
                             iv5.isVisible = true
                             iv5.setImageBitmap(bitmap)//  iv.setImageBitmap(adjustTakePhotoRotation(bitmap,0))
                             showButton(fail = true, pass = true)
                         }

                    image.close()//不关闭不能连拍
            } catch (e: Exception) {
            }
        }

3. Search online later because the maximum continuous shooting limit of Camera2 is 2. Many numbers on the Internet, including the source code of Google Camera, need to open a separate thread to process the logic of the picture, and then the ui will be processed in the main thread. Finally, try a wave of continuous shooting on the tablet and your own mobile phone. Clicking to take a picture still shows crash log content java.lang.IllegalStateException: CameraDevice was already closed

4. I got stuck and went to the native Snapdragon camera to find the answer. Many people deal with the logic of continuous shooting in the touch onTouch event and then see the life cycle.

5. So I tried to modify it again. If it is not completely released, then check it directly from the error message, add more logs, and see where the camera device is turned off. On the other hand, look at the complete log, see Is there any other abnormality in the system?

Analyzing the log shows that after the photo is taken, a preview should be requested, and the camera device is turned off.

At this time, release all cameradevice and add logs

6. A surprise appeared in   onPause. This print can already know the cause of the previous exception. That is, the camera.device is released during onpause, and the preview is requested in the photo callback.

 7. The solution is to report the error in the oncapturecompled when the photo is completed, and then go to restore the preview to have a problem, so it should be added in the callback when the photo is completed.

 8. Then click to take pictures continuously and there is no crash for the time being 

Other camera2 specific problems are analyzed in detail, and the basic process is over here. Reprint please indicate the source, thank you!

Android Qualcomm Camera2 Camera Device Close_KdanMin's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/qq_15950325/article/details/129696678