如何在Java中将byte []转换为int []?

时间:2019-03-30 12:29:20

标签: java c++ arrays bitmap rgb

我有一个按字节B-G-R-B-G-R从C ++返回的字节[] ...大小应为800 * 600 * 3 在Java中,我需要按照Bitmap.createBitmap

的要求将其转换为int []

代码将是这样

byte[] JNI_output,mJpeg;
int[]  bitmap_Input;
Bitmap mBitmap; 
mJpeg = ...........   
JNI_output=processInJNI(mJpeg);

SOME CODE HERE CONVERT byte[] to int[]

mBitmap=Bitmap.createBitmap( bitmap_Input,800,600,Bitmap.Config.ARGB_8888);

我该如何实现?

首先,在JNI中,我们需要将BGR字节[]转换为BGRA btye []

JNIEXPORT jbyteArray JNICALL 
Java_Project_Activity_processInJNI(JNIEnv *env,jobject classObject,jbyteArray jpeg){
Mat result;
result = ......
......
cvtColor(result,result,CV_BGR2BGRA);
return result;}

在Java中,由于位图的顺序与RGBA的顺序完全相反,因此我们需要按little endian的顺序扭曲byte []。

JNI_output=processInJNI(mJpeg);
IntBuffer intBuf = ByteBuffer.wrap(JNI_output).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
int[] bitmap_input = new int[intBuf.remaining()];
intBuf.get(bitmap_input);
mBitmap=Bitmap.createBitmap( bitmap_input,800,600,Bitmap.Config.ARGB_8888);

问题已结束

0 个答案:

没有答案