简体   繁体   English

java.nio.ByteBuffer.arrayOffset 处的 java.lang.UnsupportedOperationException?

[英]java.lang.UnsupportedOperationException at java.nio.ByteBuffer.arrayOffset?

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:我需要从IFrameCallback获取位图,该位图用于处理来自UVCCamera预览的,但有一个例外:

java.lang.UnsupportedOperationException at java.nio.ByteBuffer.arrayOffset java.nio.ByteBuffer.arrayOffset 处的 java.lang.UnsupportedOperationException

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:或者,如果我使用createBitmap而不是decodeByteArray则会出现此异常:

java.lang.RuntimeException: Buffer not large enough for pixels at android.graphics.Bitmap.copyPixelsFromBuffer java.lang.RuntimeException: 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.因为预览尺寸是1280 x 720 ,所以RuntimeException的问题可以忽略。

Is there any ideas about the first exception to get the arrayOffset from ByteBuffer ?关于从ByteBuffer获取arrayOffset的第一个异常有什么想法吗?

PS: I know PS:我知道

UnsupportedOperationException - If this buffer is not backed by an accessible array UnsupportedOperationException - 如果此缓冲区不受可访问数组的支持

My point is: is there any other ways to get the arrayOffset , or if can't do that and how to solve the RuntimeException?我的观点是:有没有其他方法可以获得arrayOffset ,或者如果不能这样做以及如何解决 RuntimeException ?

Well, since there's no a good solution, I had changed the way to get the bitmap from IFrameCallback .好吧,由于没有好的解决方案,我改变了从IFrameCallback获取位图的方式。

Because I had applied the UVCCamera.PIXEL_FORMAT_NV21 format to the UVCCamera :因为我已经将UVCCamera.PIXEL_FORMAT_NV21格式应用于UVCCamera

uvcCamera.setFrameCallback(frameCallback, UVCCamera.PIXEL_FORMAT_NV21); uvcCamera.setFrameCallback(frameCallback, UVCCamera.PIXEL_FORMAT_NV21);

So, I can get the bitmap just transform YuvImage :所以,我可以得到位图只是变换YuvImage

private ChannelBufferOutputStream stream = new ChannelBufferOutputStream(MessageBuffers.dynamicBuffer());
......
private final IFrameCallback callback = new IFrameCallback() {
    @override
    public void onFrame(final ByteBuffer frame) {
        new Thread(new Runnable() {
            @override
            public void run() {
                byte[] bytes = new byte[frame.remaining()];
                frame.get(bytes);

                if(bytes.length > 0) {
                    YuvImage yuvImage = new YuvImage(bytes, ImageFormat.NV21, UVCCamera.DEFAULT_PREVIEW_WIDTH, UVCCamera.DEFAULT_PREVIEW_HEIGHT, null);
                    bytes = null;

                    if(yuvImage.getYuvData().length > 0) {
                        yuvImage.compressToJpeg(new Rect(0, 0, yuvImage.getWidth(), yuvImage.getHeight()), 80, stream);
                        Bitmap bitmap = BitmapFactory.decodeByteArray(stream.buffer().array(), 0, stream.buffer().array().length);
                        ......
                    }
                }
            }
        }).start();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.nio.ByteBuffer.array上的java.lang.UnsupportedOperationException(ByteBuffer.java:959) - java.lang.UnsupportedOperationException at java.nio.ByteBuffer.array(ByteBuffer.java:959) akka Serialization ByteBuffer - java.lang.UnsupportedOperationException - akka Serialization ByteBuffer - java.lang.UnsupportedOperationException 模拟java.nio.ByteBuffer类时的java.lang.UnsupportedOperationException - java.lang.UnsupportedOperationException when mocking java.nio.ByteBuffer class 获取“java.lang.UnsupportedOperationException:” - Getting “java.lang.UnsupportedOperationException:” java.lang.UnsupportedOperationException:null - java.lang.UnsupportedOperationException: null java.lang.UnsupportedOperationException:删除 - java.lang.UnsupportedOperationException: remove java.lang.UnsupportedOperationException 与 ArrayList() - java.lang.UnsupportedOperationException with ArrayList() java.lang.UnsupportedOperationException: JsonObject - java.lang.UnsupportedOperationException: JsonObject 该程序在Java中抛出java.lang.UnsupportedOperationException - The program throws java.lang.UnsupportedOperationException in Java java.lang.UnsupportedOperationException 在 io.netty.channel.socket.nio.NioServerSocketChannel.doConnect(NioServerSocketChannel.java:178) - java.lang.UnsupportedOperationException at io.netty.channel.socket.nio.NioServerSocketChannel.doConnect(NioServerSocketChannel.java:178)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM