我怎样才能从ByteBuffer短[] [英] How can I get short[] from a ByteBuffer

查看:325
本文介绍了我怎样才能从ByteBuffer短[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用在Android项目,其中JNI本机功能需要很短的[]参数JNI code。但是,原来的数据被存储作为ByteBuffer的。我想转换的数据格式如下:

I am using JNI code in an Android project in which the JNI native function requires a short[] argument. However, the original data is stored as a ByteBuffer. I'm trying the convert the data format as follows.

ByteBuffer rgbBuf = ByteBuffer.allocate(size);
...    
short[] shortArray = (short[]) rgbBuf.asShortBuffer().array().clone();

但运行上面显示的code的第二行,当我遇到以下问题:

But I encounter the following problem when running the second line of code shown above:

E/AndroidRuntime(23923): Caused by: java.lang.UnsupportedOperationException
E/AndroidRuntime(23923): at Java.nio.ShortToByteBufferAdapter.protectedArray(ShortToByteBufferAdapter.java:169)

任何人都可以提出实施转化的一种手段?

Could anyone suggest a means to implement the conversion?

推荐答案

的方法做,这是一个有点古怪,其实。你可以做到如下;订货重要的是将其转换为一个阵列。

The method do this is a bit odd, actually. You can do it as below; ordering it is important to convert it to a short array.

short[] shortArray = new short[size/2];
rgbBuf.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortArray);

此外,您可能需要使用 allocateDirect 而不是分配

Additionally, you may have to use allocateDirect instead of allocate.

这篇关于我怎样才能从ByteBuffer短[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆