简体   繁体   English

如何将int数组转换为字节数组

[英]How convert int array to byte array

int w = width ;
int h =height  ;

Log.i("SopCast", "w:"+w+"-----h:"+h);

int b[]=new int[(int) (w*h)];
int bt[]=new int[(int) (w*h)];
IntBuffer buffer=IntBuffer.wrap(b);
buffer.position(0);
GLES20.glReadPixels(0, 0, w, h,GLES20.GL_RGBA,GLES20.GL_UNSIGNED_BYTE, buffer);
for(int i=0; i<h; i++)
{
      //remember, that OpenGL bitmap is incompatible with Android bitmap
      //and so, some correction need.
      for(int j=0; j<w; j++)
      {
          int pix=b[i*w+j];
          int pb=(pix>>16)&0xff;
          int pr=(pix<<16)&0x00ff0000;
          pix1=(pix&0xff00ff00) | pr | pb;
          bt[(h-i-1)*w+j]=pix1;
          }
      }
      Bitmap inBitmap = null ;
     if (inBitmap == null || !inBitmap.isMutable()
                    || inBitmap.getWidth() != w || inBitmap.getHeight() != h) {
                inBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            }
            //Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            inBitmap.copyPixelsFromBuffer(buffer);
            //return inBitmap ;
            // return Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
            inBitmap = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);

I used above code to get a bitmap,and i want to get the original byte form the 我使用上面的代码来获取位图,并且我想从

          IntBuffer buffer=IntBuffer.wrap(b);
          int[] = buffer.array();

how to convert int[] to byte[] 如何将int []转换为byte []

or change the code 或更改代码

                int b[]=new int[(int) (w*h)];
                int bt[]=new int[(int) (w*h)];
                IntBuffer buffer=IntBuffer.wrap(b);

to byte[] 到字节[]

can you give me some help? 你能给我些帮助吗?

Try this 尝试这个

byte[] ba= new byte[bt.length * 4];

for(int i = 0, k = 0; i < bt.length; i++) {
     int temp= bt[i];
       for(int j = 0; j < 4; j++, k++) {
         ba[k] = (byte)((temp>> (8 * j)) & 0xFF);
     }
}  

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM