jiangdongguo / AndroidUSBCamera

🔥🔥🔥Flexible and useful UVC camera engine on Android platform, supporting multi-road cameras!

Home Page:https://juejin.cn/post/7115229806844706847

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

updateResolution in some devices causes blank screen

cityvoice opened this issue · comments

I have several cameras made by different vendors, if I config resolutin by getCameraRequest() method with a solid width and height, all cameras can work. But if I change resolution using resolution which required by getAllPreviewSizes() method, most of these cameras don't work,  the camera open successful, but the screen is blank. No any errors 
        override fun getCameraRequest(): CameraRequest {
        return CameraRequest.Builder()
            .setPreviewWidth(640)
            .setPreviewHeight(480)
            .setRenderMode(CameraRequest.RenderMode.OPENGL) // camera render mode
            .setDefaultRotateType(RotateType.ANGLE_0) // rotate camera image when opengl mode
            .setAudioSource(CameraRequest.AudioSource.NONE) // set audio source
            .setPreviewFormat(CameraRequest.PreviewFormat.FORMAT_MJPEG) // set preview format, MJPEG recommended
            .setAspectRatioShow(false) // asp    ect render,default is true
            .setCaptureRawImage(true) // capture raw image picture when opengl mode
            .setRawPreviewData(true)  // preview raw image when opengl mode
            .create()
    }
    private fun handleCameraOpened() {
        val previewSize =
            getCurrentCamera()?.getAllPreviewSizes()!!.sortedByDescending { it.width }
        if (previewSize.isNotEmpty()) {
            val bigPreviewSize = previewSize[0]
            getCurrentCamera()?.updateResolution(bigPreviewSize.width, bigPreviewSize.height)
        }
        getCurrentCamera()?.addPreviewDataCallBack(previewDataCallback())
    }

I'm using version3

commented

Same issue here, I can see on logs that the EGL is rendering showing the FPS but the layout is blank, after the update resolution

EDIT: I figure it out a workaround, the updateResolution closes the camera and open it again, but in your current code (and it was on mine too), you are updating the resolution as soon as the camera is opened, so the issue is that the camere itself has not finished to open and you are already closing it. It was hard to debug as there is no error message, the native library will just crash.

I manually close and open the camera with some delay and that worked, of course, you need to update your getCameraRequest with the updated resolution for it to work

Handler main = new Handler(Looper.getMainLooper());
main.postDelayed(this::closeCamera, 1000);
main.postDelayed(() -> openCamera(aspectRatioTextureView), 2000);