Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android compute hash of a bitmap

I want to compute a SHA1 hash of different bitmaps (SHA isn't forced). The problem is that there are some bitmaps (captchas) wich are basicly the same, but the name changes often.

I've found this:

Compute SHA256 Hash in Android/Java and C#

But it isn't the soloution i wanted.

The Bitmap.hashCode(), generates only a Integer, and when im right

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

I dont't want a hash code of the object, i want the hashcode of the bitmap content. Thanx!

like image 389
MemLeak Avatar asked Jun 20 '12 20:06

MemLeak


1 Answers

In Android 3.1 or later (API Level 12) there is a method on Bitmap called sameAs() which will compare the pixels and return if the two represent the same image. It does this in native code so it is relatively fast.

If you must target a lower API level, you must write a method that iterates over each pixel of the two objects and see if they match. This will be a very intensive process if done in Java code, so you may consider writing a small routine using the NDK that you can call from your application to do the comparison in native code (there are Bitmap APIs in the NDK so you can easily get at the pixel buffers).

If you opt to do so in Java, getPixels() will assist you in obtaining arrays of the pixel data that you can compare between the two images.

HTH

like image 176
devunwired Avatar answered Oct 07 '22 07:10

devunwired