chenyeju295 / flutter_uvc_camera

Flutter package
https://pub.dev/packages/flutter_uvc_camera
MIT License
8 stars 3 forks source link

Preview error - Frame format / Color space error - could not negotiate with camera:err=-51 #7

Closed Joao-Peterson closed 1 month ago

Joao-Peterson commented 1 month ago

While trying to make this package work with a boroscope camera on my phone, I figured that the preview wasn't opening because of a frame format error.

Error: could not negotiate with camera:err=-51.

Related issues in jiangdongguo AndroidUSBCamera:

So my fix was to alter line 154 in the CustomCameraUVC.kt source. The try catch block wasn't working in my wrong frame format case.

CustomCameraUVC.kt:

try {
    Logger.i(TAG, "getSuitableSize: $previewSize")
    if (! isPreviewSizeSupported(previewSize)) {
        closeCamera()
        postStateEvent(ICameraStateCallBack.State.ERROR, "unsupported preview size")
        Logger.e(TAG, "open camera failed, preview size($previewSize) unsupported-> ${mUvcCamera?.supportedSizeList}")
        return
    }
    initEncodeProcessor(previewSize.width, previewSize.height)
    // if give custom minFps or maxFps or unsupported preview size
    // this method will fail
    mUvcCamera?.setPreviewSize(
        previewSize.width,
        previewSize.height,
        MIN_FS,
        MAX_FPS,
        UVCCamera.FRAME_FORMAT_YUYV,                        // ALTERED HERE !!!
        UVCCamera.DEFAULT_BANDWIDTH
    )
} catch (e: Exception) {
    try {
        previewSize = getSuitableSize(request.previewWidth, request.previewHeight).apply {
            mCameraRequest!!.previewWidth = width
            mCameraRequest!!.previewHeight = height
        }
        if (! isPreviewSizeSupported(previewSize)) {
            postStateEvent(ICameraStateCallBack.State.ERROR, "unsupported preview size")
            closeCamera()
            Logger.e(TAG, "open camera failed, preview size($previewSize) unsupported-> ${mUvcCamera?.supportedSizeList}")
            return
        }
        Logger.e(TAG, " setPreviewSize failed, try to use yuv format...")
        mUvcCamera?.setPreviewSize(
            previewSize.width,
            previewSize.height,
            MIN_FS,
            MAX_FPS,
            UVCCamera.FRAME_FORMAT_YUYV,
            UVCCamera.DEFAULT_BANDWIDTH
        )
    } catch (e: Exception) {
        closeCamera()
        postStateEvent(ICameraStateCallBack.State.ERROR, "err: ${e.localizedMessage}")
        Logger.e(TAG, " setPreviewSize failed, even using yuv format", e)
        return
    }
}

It appears that some devices need more configuration options: Frame format, min/max Fps and Bandwidth. I think it would be great to pass these parameters from flutter to android and using them to instantiate the camera.

chenyeju295 commented 1 month ago

Thank you very much for the suggestion, I don't have time to adjust the testing at the moment, so I'll consider adding it to subsequent versions.😁

chenyeju295 commented 1 month ago

Updated to version 0.0.3, thanks!

Joao-Peterson commented 1 month ago

Thanks!