0

此代码完美运行,但我无法使用 ANDROID,我需要比较两个图像?

 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;
    }



}
4

1 回答 1

0

几件事:

  • 如果您的代码处理选定的菜单选项,您应该返回true,如果没有。onOptionsItemSelected()false

  • 一旦确定图像不同,就应该打破循环。为什么做的工作比你需要的多?

  • 此处无需使用案例/开关。您只处理一个菜单项。

所以:

@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);
}
于 2012-09-12T00:44:34.990 回答