You are on page 1of 3

Image Matching:

The code: One final note: the code assumes ImageViews are under a LinearLayout. If not, change to appropriate layout. You could also give the layout as a parameter.
private void scaleImage(ImageView view, int boundBoxInDp) { // Get the ImageView and its bitmap Drawable drawing = view.getDrawable(); Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap(); // Get current dimensions int width = bitmap.getWidth(); int height = bitmap.getHeight(); // Determine how much to scale: the dimension requiring less scaling is // closer to the its side. This way the image always stays inside your // bounding box AND either x/y axis touches it. float xScale = ((float) boundBoxInDp) / width; float yScale = ((float) boundBoxInDp) / height; float scale = (xScale <= yScale) ? xScale : yScale; // Create a matrix for the scaling and add the scaling data Matrix matrix = new Matrix(); matrix.postScale(scale, scale); // Create a new bitmap and convert it to a format understood by the ImageView Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); BitmapDrawable result = new BitmapDrawable(scaledBitmap); width = scaledBitmap.getWidth(); height = scaledBitmap.getHeight(); // Apply the scaled bitmap view.setImageDrawable(result); // Now change ImageView's dimensions to match the scaled image LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); params.width = width; params.height = height; view.setLayoutParams(params); } private int dpToPx(int dp) { float density = getApplicationContext().getResources().getDisplayMetrics().density; return Math.round((float)dp * density); }

(the code tuned a little bit from the StackOverflow version) And sample code for testing and faster adoptation:
public class TestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); // ImageViews must be under LinearLayout in the xml or the code crashes into scaleImage(). Tune scaleImage() into your needs. ImageView view1 = (ImageView) findViewById(R.id.test1); ImageView view2 = (ImageView) findViewById(R.id.test2); scaleImage(view1, 250); // in dp scaleImage(view2, 100); // in dp } }

1. /// <summary> 2. /// method for comparing 2 images to see if they are the same. First 3. /// we convert both images to a byte array, we then get their hash (their 4. /// hash should match if the images are the same), we then loop through 5. /// each item in the hash comparing with the 2nd Bitmap 6. /// </summary> 7. /// <param name="bmp1"></param> 8. /// <param name="bmp2"></param> 9. /// <returns></returns> 10. public bool doImagesMatch(ref Bitmap bmp1, ref Bitmap bmp2) 11. { 12. try 13. { 14. //create instance or System.Drawing.ImageConverter to convert 15. //each image to a byte array 16. ImageConverter converter = new ImageConverter();

17. //create 2 byte arrays, one for each image 18. byte[] imgBytes1 = new byte[1]; 19. byte[] imgBytes2 = new byte[1]; 20. 21. //convert images to byte array 22. imgBytes1 = (byte[])converter.ConvertTo(bmp1, imgBytes2.GetType()); 23. imgBytes2 = (byte[])converter.ConvertTo(bmp2, imgBytes1.GetType()); 24. 25. //now compute a hash for each image from the byte arrays 26. SHA256Managed sha = new SHA256Managed(); 27. byte[] imgHash1 = sha.ComputeHash(imgBytes1); 28. byte[] imgHash2 = sha.ComputeHash(imgBytes2); 29. 30. //now let's compare the hashes 31. for (int i = 0; i < imgHash1.Length && i < imgHash2.Length; i++) 32. { 33. //whoops, found a non-match, exit the loop 34. //with a false value 35. if (!(imgHash1[i] == imgHash2[i])) 36. return false; 37. } 38. } 39. catch (Exception ex) 40. { 41. MessageBox.Show(ex.Message); 42. return false; 43. } 44. //we made it this far so the images must match 45. return true; 46. }

You might also like