0

This code work perfectly but i can't make work with ANDROID, i need to comapre two image ?

 import java.awt.image.BufferedImage;
    import java.io.FileInputStream;
    import java.util.Vector;

     import javax.imageio.ImageIO;

    public class Untitled1 {
    public static void main(String[] args) {
        Vector original = testImg("b.jpg");
        Vector clonde   = testImg("a.jpg");
        System.out.println(original.equals(clonde));

    }

    public static Vector testImg(String file) {
        Vector all = new Vector();
        try {
            BufferedImage im = ImageIO.read(new FileInputStream(file));
            int w = im.getWidth(null);
            int h = im.getHeight(null);
            int[] rgbs = new int[w * h];
            int x = 0;
            im.getRGB(0, 0, w, h, rgbs, 0, w);

            for (int i = 0; i < w; i+=100) {
                Vector line = new Vector();
                for (int j = 0; j < h; j+=100) {
                    line.add(new Integer(rgbs[x]));
//                    System.out.println("Pixel " + i + "," + j + "has " + "RGB values of " + rgbs[x]);
                    x++;
                }
                all.add(line);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return all;
    }



}
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
hamzarh
  • 330
  • 2
  • 6
  • 20
  • possible duplicate of [Image comparison - fast algorithm](http://stackoverflow.com/questions/843972/image-comparison-fast-algorithm) – fresskoma Sep 12 '12 at 00:44

1 Answers1

0

a couple of things:

  • you should be returning true from onOptionsItemSelected() if your code handles the selected menu option, and false if it doesn't.

  • you should break your loop as soon as you determine the images are different. Why do more work than you need to?

  • there is no need to use a case/switch here. You only handle a single menu item.

so:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() != 1000)
        return (super.onOptionsItemSelected(item);

    for(int i=0;i<tabA.length;i++)
    {
        if(tabA[i] != tabB[i])
        {
            Toast.makeText(this, "Image are not equal", Toast.LENGTH_LONG).show();  

            return (true);
        }
    }

    Toast.makeText(this, "Image are equal", Toast.LENGTH_LONG).show();  

    return (true);
}
Dan O
  • 6,022
  • 2
  • 32
  • 50
  • also, as you have found out, comparing images pixel by pixel takes a really long time. You might want to use additional checks in your comparison method. See here for more: http://stackoverflow.com/questions/4409282/compare-two-images-is-same-or-not?rq=1 – Dan O Sep 12 '12 at 00:46