0

I'm trying to convert the byte array into a bitmap, and then again I'm converting back from bitmap to byte array, but there seems to be some issue, because the images do not match.

Below is the function that I'm using to convert byte array to bitmap.

public Bitmap getBitmapFromRawImage(byte[] rawImage,int width,int height) {
    byte[] Bits = new byte[rawImage.length * 4]; // That's where the RGBA
    // array
    // goes.
    int i;
    for (i = 0; i < rawImage.length; i++) {
        Bits[i * 4] = Bits[i * 4 + 1] = Bits[i * 4 + 2] = rawImage[i];

        // Invert the source bits
        Bits[i * 4 + 3] = -1;// 0xff, that's the alpha.
    }

    // Now put these nice RGBA pixels into a Bitmap object
    Bitmap bm = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
    bm.copyPixelsFromBuffer(ByteBuffer.wrap(Bits));

    return bm;
}

It seems to be fine, but I'm not sure of byte array, how it converts to byte array again based on above format.

Kindly help me to make the below method perfect based on above bitmap method

public byte[] getByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] flippedImageByteArray = stream.toByteArray();

    return flippedImageByteArray;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
angelina
  • 59
  • 2
  • 11
  • Does this answer your question? [Access to raw data in ARGB\_8888 Android Bitmap](https://stackoverflow.com/questions/5010545/access-to-raw-data-in-argb-8888-android-bitmap) – emandt Mar 16 '21 at 13:12
  • No...Before conversion to bitmap Byte array receved is [B@1b53928c and after i convert to bitmap and than again convert back to byte array the output is [B@3d843dd5... please help me – angelina Mar 16 '21 at 14:10
  • You have to compare the CONTENT (in this specific case: array of bytes) not the "hash code of the object". Each different object, even if its content is the same of another one, has a different Hash Code......... (example: https://stackoverflow.com/questions/52357354/typecast-string-to-bytearray-in-kotlin) – emandt Mar 16 '21 at 14:21
  • i tried and the contents are also different . is there way to rotate bytearray directly? – angelina Mar 17 '21 at 06:34
  • If you're trying to compare your argument "bmp" (and it's extracted pixels array) with resulting "flippedImageByteArray" I think it's wrong. Your compressed output "stream" contains COMPRESSED data usable to write (for example) a PNG file and not to be compared against the uncompressed/original source bitmap. To do a comparison you have to re-decode the "stream" in a REAL Bitmap and then compare both array of raw pixels. Moreover: PNG stream could contains an HEADER (because the methos is usually used to directly write a file that needs an Image Header) – emandt Mar 17 '21 at 07:24
  • how do i do that . acutally i am using morpho sdk to capture fingerprint – angelina Mar 17 '21 at 07:25
  • You can convert "stream" to a Bitmap using "BitmapFactory.decodeStream(inputStream)". Oblivioustly you have to create "inputStream" as an InputStream/BufferedInputStream based on your "stream". (it's easy but I'm writing you from a smartphone and cannot test any code or quickly read any Reference/Guide to write you right methods to use) – emandt Mar 17 '21 at 07:30
  • but i have byte[] initially – angelina Mar 17 '21 at 07:32

0 Answers0