0

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

Sheldon
  • 117
  • 1
  • 1
  • 9
  • 1
    Image.getPixel() does not exists on Android. So where are you talking about ? And why are you talking about or using BitmapFactory? You will not make a Bitmap first is it? Your server will serve an image file i think. So do checksums over the bytes over the image file. But what sense does it make to download the file first and then calculate a checksum? – greenapps Dec 28 '16 at 13:57
  • Image.getPixel() does not exist on PC as well, at least not in java (this is Pseudocode so there is no API really used) I am talking about getting to tell you the method so if there are any mistakes within it it's easier to spot issues. BitmapFactory can read JPEGs as well as other image formats (as far as I have heard from the internet and it decodes perfectly well, the image looks right). Did I ever say that I download the image file before? Nope.. Android and PC use the same input but from different sources. – Sheldon Dec 28 '16 at 14:02
  • Why are you not reaction on determining checksum over the bytes of the image file? That is the whole point. Dont use Bitmap and BitmapFactory for it. – greenapps Dec 28 '16 at 14:05
  • I could do that but at first I want to know why this isn't working instead of directly surrendering to it just 'cause one thing didn't exactly do what I thought it should – Sheldon Dec 28 '16 at 14:06
  • `Android and PC use the same input but from different sources` ??? What do you mean? Android was downloading a file from your PC i understood. – greenapps Dec 28 '16 at 14:07
  • An Image or Bitmap class will behave different on different platforms. Especially if a jpeg has to be translated to a bitmap. An yVal should be the same though. At that moment `Endianess`could have come in play. Check the values in hexadecimal notation. Then you willl see. – greenapps Dec 28 '16 at 14:08
  • PC loads image from its cache, calculates check sum and sends it to android, android loads image from its cache and compares check sums.. if identical: done, if not: retrieve image from pc – Sheldon Dec 28 '16 at 14:09
  • It is unclear what you mean with 'image'. Do you mean an image file? – greenapps Dec 28 '16 at 14:10
  • The file loaded in to a class (Bitmap and BufferedImage) – Sheldon Dec 28 '16 at 14:11
  • Again: 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 convers the bytes of the file. Not at sending side. Not at receiving side. – greenapps Dec 28 '16 at 14:13

2 Answers2

1

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

Community
  • 1
  • 1
Jayfray
  • 415
  • 3
  • 12
1

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.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Yeah it works that way my aim with that question was to find out, why ImageIO and BitmapFactory would read both images differently – Sheldon Dec 28 '16 at 15:28