这段代码完美无缺,但我无法使用ANDROID工作,我需要comapre两个图像?
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;
}
}
答案 0 :(得分: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);
}