saki4510t / UVCCamera

library and sample to access to UVC web camera on non-rooted Android device
3.02k stars 1.21k forks source link

Getting frames with IFrameCallback #595

Closed emejoti closed 3 years ago

emejoti commented 3 years ago

Hello,

I need to get the frames from the camera as a ByteBuffer, so I'm using IFrameCallback like this:

mUVCCamera.setFrameCallback(mIFrameCallback1, UVCCamera.PIXEL_FORMAT_YUV420SP);

private final IFrameCallback mIFrameCallback1 = new IFrameCallback() {
            @Override
            public void onFrame(final ByteBuffer frame) {
                             // Saving the frame on a ConcurrentLinkedDeque 
            }
        };

The thing is, when I get the frame from that cocurrentLinkedDeque, because I need to use it on another function, the frame looks wrong. I tried all the formats on UVCCamera and YUV420SP is the one that gave me less problems.

Here are some of the frames I get:

0 1 100

I'll appreciate any help, thanks.

emejoti commented 3 years ago

Solved! Here is the code:

     private int buf_sz = 4096;
     private byte[] buf = new byte[buf_sz];

    private final IFrameCallback mIFrameCallback1 = new IFrameCallback() {
        @Override
        public void onFrame(final ByteBuffer frame) {
            synchronized (mSync) {
                final int n = frame.limit();
                if (buf_sz < n) {
                    buf_sz = n;
                    buf = new byte[n];
                }
                frame.get(buf, 0, n);

                //Save buf on my ConcurrentLinkedDeque 
            }
        }
    };

And then just transform the byte array into ByteBuffer with ByteBuffer.wrap(buf );

I found the solution here: https://github.com/saki4510t/UVCCamera/issues/241#issuecomment-336334886

rgm-22 commented 3 years ago

Hi @emejoti!

How did you succeed with this on the MainActivity?

"And then just transform the byte array into ByteBuffer with ByteBuffer.wrap(buf );"

I would like to record what I am displaying on IFrameCallback.

Thank you very much in advance!