Skip to content

How can I capture data from a camera (MJPEG/YUY2) and then stream it? I have tried but was unsuccessful. #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
thaituong opened this issue Mar 21, 2025 · 0 comments

Comments

@thaituong
Copy link

@OverRide
public void onDeviceOpen(UsbDevice device, UsbControlBlock ctrlBlock, boolean createNew) {
executor.execute(() -> {
try {

            uvcCamera = new UVCCamera(null);
            List<Size> supportedSizes = uvcCamera.getSupportedSizeList();
            for (Size size : supportedSizes) {
                Log.d(TAG, "Supported size: " + size.width + "x" + size.height);
            }
            int result = uvcCamera.open(ctrlBlock);
            if (result == 0) {
                Log.d(TAG, "UVC camera opened successfully.");
            } else {
                Log.e(TAG, "Failed to open UVC camera. Error code: " + result);
                return;
            }

            uvcCamera.setPreviewSize(previewWidth, previewHeight, UVCCamera.UVC_VS_FORMAT_MJPEG);
            uvcCamera.setPreviewDisplay(svVideoRender.getHolder());
            uvcCamera.startPreview();

            uvcCamera.setFrameCallback(frame -> {
                try {
                    byte[] mjpegData = frame.array();
                    Bitmap bitmap = BitmapFactory.decodeByteArray(mjpegData, 0, mjpegData.length);

                    byte[] nv21Data = convertBitmapToNV21(bitmap, previewWidth, previewHeight);

                    NV21Buffer nv21Buffer = new NV21Buffer(nv21Data, previewWidth, previewHeight, null);
                    VideoFrame videoFrame = new VideoFrame(nv21Buffer, 0, System.nanoTime());

                    capturerObserver.onFrameCaptured(videoFrame);
                    nv21Buffer.release(); 
                } catch (Exception e) {
                    Log.e(TAG, "Failed to process MJPEG frame: " + e.getMessage());
                }
            }, UVCCamera.UVC_VS_FORMAT_MJPEG);

            Log.d(TAG, "UVC camera started successfully.");
        } catch (Exception e) {
            Log.e(TAG, "Failed to start UVC camera: " + e.getMessage());
        }
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant