开发者

Android: conerting Image into byteArray

开发者 https://www.devze.com 2023-01-27 08:29 出处:网络
In my project I have an bitmap image. I need to convert this picture to byteArray in order to manipulate some bytes and after that save it as image.

In my project I have an bitmap image. I need to convert this picture to byteArray in order to manipulate some bytes and after that save it as image.

with this code image = BitmapFactory.decodeResource(context.g开发者_开发百科etResources(), R.drawable.tasnim); I have acces to width and height but how can I have access to bytes of this image?

Thanks


AFAIK Most correct way is:

ByteBuffer copyToBuffer(Bitmap bitmap){
    int size = bitmap.getHeight() * bitmap.getRowBytes();
    ByteBuffer buffer = ByteBuffer.allocateDirect(size);
    bitmap.copyPixelsToBuffer(buffer);
    return buffer;
}


I'm assuming the OP wants to manipulate the pixels, not the header information of the Image...

Assuming your image is a Bitmap

int w = image.getWidth(), h = image.getHeight();
int[] rgbStream = new int[w * h];
image.getPixels(rgbStream, 0, w, 0, 0, w, h);

Of course, this gets you Pixel values as Integers...But you can always convert them again.

    int t = w * h;

    for (int i = 0; i < t; i++) {
        pixel = rgbStream[i];  //get pixel value (ARGB)
        int A = (pixel >> 24) & 0xFF; //Isolate Alpha value...
        int R = (pixel >> 16) & 0xFF; //Isolate Red Channel value...
        int G = (pixel >> 8) & 0xFF; //Isolate Green Channel value...
        int B = pixel & 0xFF; //Isolate Blue Channel value...
                       //NOTE, A,R,G,B can be cast as bytes...


    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号