I need get the bitmap from IFrameCallback which was functioned to deal with the frame from a UVCCamera preview, but there's comes the exception:
java.lang.UnsupportedOperationException at java.nio.ByteBuffer.arrayOffset
private final IFrameCallback callback = new IFrameCallback() {
@override
public void onFrame(final ByteBuffer frame) {
new Thread(new Runnable() {
byte[] bytes = new byte[frame.remaining()];
frame.get(bytes);
if(bytes.length > 0) {
int offset = frame.arrayOffset();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, offset, bytes.length - offset);
......
}
}).start();
}
}
or if I use createBitmap instead of decodeByteArray, I got this exception:
java.lang.RuntimeException: Buffer not large enough for pixels at android.graphics.Bitmap.copyPixelsFromBuffer
private final IFrameCallback callback = new IFrameCallback() {
@override
public void onFrame(final ByteBuffer frame) {
new Thread(new Runnable() {
Bitmap bitmap = Bitmap.createBitmap(UVCCamera.DEFAULT_PREVIEW_WIDTH, UVCCamera.DEFAULT_PREVIEW_HEIGHT, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(frame);
......
}).start();
}
}
Because the preview size is 1280 x 720, so the problem of RuntimeException can be ignored.
Is there any ideas about the first exception to get the arrayOffset from ByteBuffer?
PS: I know
UnsupportedOperationException - If this buffer is not backed by an accessible array
My point is: is there any other ways to get the arrayOffset, or if can't do that and how to solve the RuntimeException?