Android的Java应用程序。有一段xml:
<TableRow>
<ImageView
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_margin = "10dp"
android: src = "@ drawable / icon_security" />
<TextView
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
style = "@ style / DesignerTextStyle2"
android: layout_gravity = "center_vertical"
android: text = "@ string / welcome_security_calls" />
</ TableRow>
我需要比较屏幕上某个图像的像素图像处理(例如,它是屏幕上的第三个图像)和着名图像(即此图像 - “@ drawable / icon_security”)。
你能告诉我一个解决这个问题的例子吗?
答案 0 :(得分:2)
这是Java
代码,用于使用URL
package imager;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.net.URL;
public class Imagecompare
{
public static void main(String args[])
{
String s1;
BufferedImage img1 = null;
BufferedImage img2 = null;
try {
URL url1 = new URL("http://www.lac.inpe.br/JIPCookbook/Resources/ImageSimilarity/d02.jpg");
URL url2 = new URL("http://www.lac.inpe.br/JIPCookbook/Resources/ImageSimilarity/s02.jpg");
img1 = ImageIO.read(url1);
img2 = ImageIO.read(url2);
} catch (IOException e) {
e.printStackTrace();
}
int width1 = img1.getWidth(null);
int width2 = img2.getWidth(null);
int height1 = img1.getHeight(null);
int height2 = img2.getHeight(null);
if ((width1 != width2) || (height1 != height2)) {
System.err.println("Error: Images dimensions mismatch");
System.exit(1);
}
long diff = 0;
for (int y = 0; y < height1; y++) {
for (int x = 0; x < width1; x++) {
int rgb1 = img1.getRGB(x, y);
int rgb2 = img2.getRGB(x, y);
int r1 = (rgb1 >> 16) & 0xff;
int g1 = (rgb1 >> 8) & 0xff;
int b1 = (rgb1 ) & 0xff;
int r2 = (rgb2 >> 16) & 0xff;
int g2 = (rgb2 >> 8) & 0xff;
int b2 = (rgb2 ) & 0xff;
diff += Math.abs(r1 - r2);
diff += Math.abs(g1 - g2);
diff += Math.abs(b1 - b2);
}
}
double n = width1 * height1 * 3;
double p = diff / n / 255.0;
double percnt = 100.0-(p*100.0);
System.out.println("PERCENT: " +percnt);
}
}