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[]) { bufferedimage img1 = null; bufferedimage img2 = null; try { url url1 = new url("http://www.desktopid.com/wp-content/uploads/2015/05/apple-logo-free-desktop-wallpapers.jpg"); url url2 = new url("http://www.desktopid.com/wp-content/uploads/2015/05/apple-logo-free-desktop-wallpapers.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; (int y = 0; y < height1; y++) { (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; system.out.println("diff percent: " + (p * 100.0)); } } this simple code compares 2 images , returns percent difference, beginner, obstructed following error while running program .
> javax.imageio.iioexception: can't input stream url! @ javax.imageio.imageio.read(unknown source) @ imager.imagecompare.main(imagecompare.java:16) caused by: java.io.ioexception: server returned http response code: 403 url: http://www.desktopid.com/wp-content/uploads/2015/05/apple-logo-free-desktop-wallpapers.jpg @ sun.net.www.protocol.http.httpurlconnection.getinputstream(unknown source) @ java.net.url.openstream(unknown source) ... 2 more exception in thread "main" java.lang.nullpointerexception @ imager.imagecompare.main(imagecompare.java:21) any idea did wrong? getting url , passing ? appreciated.
Comments
Post a Comment