ImageIO.read() and BitmapFactory.decodeByteArray()

335 views Asked by At

I am working on a little Server-Client application right now with the server being on a computer and the client on an android device. At some point I want the device to check whether an image it has is up to date and using as less data as possible for it so I wrote some code to create a check sum for an image which should make it easier for the client to determine whether it's pic is up to date or not.
(I was going to send the code but that was quite long so I'll try without first) In pseudocode:

long getCheckSumFor(Image i) {
    long xVal;
    long yVal;
    count x from 0 to i.width
         long val = 0;
         count y from 0 to i.height
             val += i.getPixel(x, y);
         yVal += val % Integer.MAX_VALUE;
    count y from 0 to i.height
         long val = 0;
         count x from 0 to i.width
             val += i.getPixel(x, y);
         xVal += val % Integer.MAX_VALUE;
    int tx = xVal % Integer.MAX_VALUE;
    int ty = yVal % Integer.MAX_VALUE;
    return a long with the first 4 bytes taken from ty and the last 4 bytes taken from tx
}

this is basically how it works but it gives different outputs for the same inputs (depending on which platform, PC or Android).
I use ImageIO.read() on PC and BitmapFactory.decodeByteArray() (without BitmapFactory.Options) to read the picture and debugging showed that the yVal already differed from platform to platform so I guess there might be slight differences in how each method reads a JPEG? And if that is true, is there any way how I could read them both in the same way?

Thanks in advance,
Sheldon

2

There are 2 answers

1
greenapps On BEST ANSWER

Do not use Bitmap, BufferedImage or BitmapFactory!

Just take the bytes of the image file to calculate a checksum.

If you send the file then send the bytes of the file and do not use any class that converts the bytes of the file.

Not at sending side. Not at receiving side.

1
Jayfray On

What if you used MD5 instead? You can do this in Java here and for Android here