Comparing Bitmaps problem

All your problems with Audio, Video and Images.

Comparing Bitmaps problem

Postby danizmax » Wed Oct 20, 2010 1:36 pm

Hi,

I never seem to get same result when taking picture from some source (as resource or downloaded) and comparing it with that same picture that was saved before.

Here is my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. picUrl = new URL(hailDataImage);
  2. URLConnection conn = picUrl.openConnection();
  3. conn.connect();
  4. bis = new BufferedInputStream(conn.getInputStream());
  5. hailPic = BitmapFactory.decodeStream(bis);
  6.        
  7. fis = context.openFileInput(hailPicFile);
  8. bis = new BufferedInputStream(fis);
  9. localHailPic = BitmapFactory.decodeStream(bis);
  10.                
  11. // also tried without that
  12. baos = new ByteArrayOutputStream();
  13. hailPic.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, baos);
  14. hailPic = BitmapFactory.decodeByteArray(baos.toByteArray(),0, baos.size());
  15.                
  16. //hash code isn't same either
  17. int h1 = hailPic.hashCode();
  18. int h2 = localHailPic.hashCode();
  19.                
  20. //comparing bitmaps
  21. if(!hailPic.equals(localHailPic)){
  22.       hailPic = localHailPic;
  23.       update = false;
  24.                    
  25. }else{
  26.       update = true;
  27.       Log.e("diff in data", "update on");
  28. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


and there is the code to save Bitmap onto the device:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. fos = context.openFileOutput(hailPicFile, Context.MODE_PRIVATE);
  2. baos = new ByteArrayOutputStream();
  3.  
  4. hailPic.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, baos);
  5. fos.write(baos.toByteArray());
  6. fos.close();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


I also tried with raw byte data, but this seemed not to work either.

Any idea what a I doing wrong or how is the done the right way?

Thanks for help :P
danizmax
Freshman
Freshman
 
Posts: 5
Joined: Sun Oct 03, 2010 1:16 pm

Top

Re: Comparing Bitmaps problem

Postby Tage Nielsen » Wed Oct 20, 2010 9:03 pm

Your are comparing two separate bitmap objects that happen to have the same pixel data. You could write a new class extending the Bitmap class to override the equals and hashcode methods as desired, or write a custom compare method.

The following snippet should do the trick:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int w = bmp1.getWidth();
  2. int h = bmp1.getHeight();
  3. int[] pixels1 = new int[w * h];
  4. bmp1.getPixels(pixels1, 0, w, 0, 0, w, h);
  5.  
  6. w = bmp2.getWidth();
  7. h = bmp2.getHeight();
  8. int[] pixels2 = new int[w * h];
  9. bmp2.getPixels(pixels2, 0, w, 0, 0, w, h);
  10.  
  11. boolean isEqual = Arrays.equals(pixels1, pixels2);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Tage Nielsen
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Oct 13, 2010 8:02 pm

Re: Comparing Bitmaps problem

Postby danizmax » Thu Oct 21, 2010 11:06 am

That works well... thanks!

I don't see much use for equals() and hashCode() methods then, since most of the time programmers would be interested in content of the object and not the whole.
danizmax
Freshman
Freshman
 
Posts: 5
Joined: Sun Oct 03, 2010 1:16 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 14 guests