So a few days ago I asked this question here on SO about generating a md5 sum from a android.graphics.Bitmap object. User Leonidos was a big help and his suggested method works. But the md5 sum I generated beforehand, using the same picture, gives a different sum.
The Android code I'm using is as follows:
public String md5ForBitmap(Bitmap bitmap)
{
String hash = "";
try
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapBytes = stream.toByteArray();
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(bitmapBytes);
byte[] digestedBytes = messageDigest.digest();
BigInteger intRep = new BigInteger(1, digestedBytes);
hash = intRep.toString(16);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return hash;
}
While the python script looks like this:
def begin(path):
os.chdir(path)
files = glob.glob("*")
for file in files:
processFile(file, path)
def processFile(file, folder):
with open(file, "r") as picture:
fileContents = picture.read()
md5 = hashlib.md5()
md5.update(fileContents)
hash = md5.hexdigest()
print file + " : " + hash
The Android app receives a json string from a server that contains the url to the image and the md5 sum. The md5 sum having been calculated with the python script beforehand. After the image has been downloaded I'm left with a Bitmap object that I then use in the app.
Leonidos suggested that the Bitmap object isn't treated the same way as python treats the image data and that I'll need to find the md5 sum of the raw image data in Android. This sound, to me, to be a perfectly plausible explanation. It's just that I'm really really lost when it comes to all of this.
So what is the correct way to do this?