EGL 错误:0x3003,相机设备遇到严重错误(Camera2 Api)

Posted

技术标签:

【中文标题】EGL 错误:0x3003,相机设备遇到严重错误(Camera2 Api)【英文标题】:EGL error: 0x3003, The camera device has encountered a serious error (Camera2 Api) 【发布时间】:2019-01-05 00:47:39 【问题描述】:

我正在编写一个应用程序控制闪光灯相机手机,我可以控制闪光灯的开或关。但它只工作了几次,然后就崩溃了。

我不明白它崩溃的原因。我很累。

这是我的代码:

class FlashActivity : AppCompatActivity() 

private val PERMISSIONS_REQUEST_CAMERA = 1

private var sX = 0f
private var sY = 0f
private lateinit  var button: Button
private lateinit var camManager: CameraManager
private lateinit var camDevice: CameraDevice
private lateinit var request: CaptureRequest.Builder
private lateinit var camSession: CameraCaptureSession

override fun onCreate(savedInstanceState: Bundle?) 
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_flash)
    getScreen()
    setUI()
    init()


private fun openCamera()
    if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), PERMISSIONS_REQUEST_CAMERA)
    else
        if(getListCamera().lastIndex > 0)
            camManager.openCamera(getListCamera()[0], OpenCallback(), null)
        else
            Toast.makeText(this, "this device is not support", Toast.LENGTH_SHORT).show()
    


private fun init()
    camManager = this.getSystemService(android.content.Context.CAMERA_SERVICE) as CameraManager
    openCamera()


private fun getListCamera(): List<String> 
    var list = listOf<String>()
    for (i in camManager.cameraIdList)
        list += i
    
    return list


private fun event()
    var i = 0
    button.setOnClickListener
        if(i%2 == 0) 
            button.text = "On"
            request.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH)
        
        else
            button.text = "Off"
            request.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF)
        
        camSession.setRepeatingRequest(request.build(), null, null)
        i++
    


private fun setUI()
    button = Button(this)
    button.isActivated = false
    button.text = "Off"
    button.measure(0, 0)
    rl_flash_activity.addView(button)
    button.x = sX*50 - button.measuredWidth/2
    button.y = sY*50



private fun getScreen()
    val manager = windowManager.defaultDisplay
    val point  = Point()
    manager.getSize(point)
    sX = point.x/100f
    sY = point.y/100f


inner class OpenCallback: CameraDevice.StateCallback()
    override fun onOpened(camera: CameraDevice) 
        Log.d("camera", "opened")
        camDevice = camera
        request = camDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE)

        val texture = SurfaceTexture(1)
        texture.setDefaultBufferSize(1, 1)
        request.addTarget(Surface(texture))

        val outputSurface = ArrayList<Surface>(1)
        outputSurface.add(Surface(texture))
        try 
            camDevice.createCaptureSession(outputSurface, CameraCaptureSessionCallBack(), null)
        catch (e: Exception)
        event()
    

    override fun onDisconnected(camera: CameraDevice) 
    

    override fun onError(camera: CameraDevice, error: Int) 
        Log.d("camera", "err")
    


open inner class CameraCaptureSessionCallBack: CameraCaptureSession.StateCallback()
    override fun onConfigureFailed(session: CameraCaptureSession) 
        Log.d("CaptureSession", "false")
    

    override fun onConfigured(session: CameraCaptureSession) 
        camSession = session
        camSession.setRepeatingRequest(request.build(), null, null)
    


override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) 
    if (requestCode == PERMISSIONS_REQUEST_CAMERA) 
        // Request for camera permission.
        if (grantResults.size == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
            // Permission has been granted. Start camera preview Activity.
            openCamera()
         else 
            // Permission request was denied.
            Toast.makeText(this, "permission is not granded", Toast.LENGTH_SHORT).show()
        
    


Logcat:

07-28 03:59:14.834 7479-7525/com.example.root.flash E/CameraDeviceGLThread-0:在 GL 渲染线程上收到异常: java.lang.IllegalStateException:swapBuffers:EGL 错误:0x3003 在 android.hardware.camera2.legacy.SurfaceTextureRenderer.swapBuffers(SurfaceTextureRenderer.java:535) 在 android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:751) 在 android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:105) 在 android.os.Handler.dispatchMessage(Handler.java:98) 在 android.os.Looper.loop(Looper.java:154) 在 android.os.HandlerThread.run(HandlerThread.java:61) 07-28 03:59:14.969 7479-7479/com.example.root.flash E/AndroidRuntime: FATAL 例外:主要 进程:com.example.root.flash,PID:7479 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 引起:java.lang.reflect.InvocationTargetException 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) Caused by: android.hardware.camera2.CameraAccessException: CAMERA_ERROR (3): 摄像头设备遇到严重错误 在 android.hardware.camera2.impl.CameraDeviceImpl.checkIfCameraClosedOrInError(CameraDeviceImpl.java:2023) 在 android.hardware.camera2.impl.CameraDeviceImpl.submitCaptureRequest(CameraDeviceImpl.java:891) 在 android.hardware.camera2.impl.CameraDeviceImpl.setRepeatingRequest(CameraDeviceImpl.java:938) 在 android.hardware.camera2.impl.CameraCaptureSessionImpl.setRepeatingRequest(CameraCaptureSessionImpl.java:243) 在 com.example.root.flash.FlashActivity$event$1.onClick(FlashActivity.kt:80) 在 android.view.View.performClick(View.java:5637) 在 android.view.View$PerformClick.run(View.java:22433) 在 android.os.Handler.handleCallback(Handler.java:751) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6186) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

【问题讨论】:

【参考方案1】:

解决了。我将val texture = SurfaceTexture(1) 移至onCreate(),它工作正常

【讨论】:

以上是关于EGL 错误:0x3003,相机设备遇到严重错误(Camera2 Api)的主要内容,如果未能解决你的问题,请参考以下文章

egl 表面属性未实现?

EGL错误:“纹理资源为NULL,未指定级别”

opencv-python 相机读取错误

尝试在 Android 模拟器上运行 mapsActivity 时出现 EGL 错误

不幸的是,相机已停止错误 android 6.0.0

Nexus设备上的Android相机捕获活动错误