简体   繁体   中英

How to convert Byte array into bitmap image?

Here i attach code that convert Bitmap to Byte array now i have to regenerate bitmap to show in android Image-view.

In below code mRawBitmapData is byte Araay. Here code is create byte array for converted image so now i have to regenerate the bitmap from this byte array.

private void convertArgbToGrayscale(Bitmap bmpOriginal, int width, int height){
    int pixel;
    int k = 0;
    int B=0,G=0,R=0;
    try{
        for(int x = 0; x < height; x++) {
            for(int y = 0; y < width; y++, k++) {

                pixel = bmpOriginal.getPixel(y, x);

                if(pixel == -1){
                    mDataArray[k] = 1;
                }
                else {
                    mDataArray[k] = 0;
                }
            }
            if(mDataWidth>width){
                for(int p=width;p<mDataWidth;p++,k++){
                    mDataArray[k]=0;
                }
            }
        }
    }catch (Exception e) {

        Log.e(TAG, e.toString());
    }
}

private void createRawMonochromeData(){
    int length = 0;
    for (int i = 0; i < mDataArray.length; i = i + 8) {

        int k=0;
        for (int j = 7; j >=0; j--, k++) {
            if(mDataArray[i+k]==1)
            {
                mRawBitmapData[length] |= 1 << j;
            }
            else
            {
                mRawBitmapData[length] &= ~(1 << j);
            }

        }

        length++;

    }
}
byte[] bitmapdata; // let this be your byte array
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);

使用Android的BitmapFactory从字节数组中获取位图,如:

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata.length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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