[英]Load image from url in java
我用過Windows 7 OS,chrome / 40.0.2214.93
我嘗試使用Java從網址獲取圖片
我的java代碼是
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedImage img1 = null;
BufferedImage img2 = null;
InputStream inputstream=null;
URLConnection urlcon=null;
try {
URL url1 = new URL("http://rosettacode.org/mw/images/3/3c/Lenna50.jpg");
URL url2 = new URL("http://rosettacode.org/mw/images/b/b6/Lenna100.jpg");
urlcon=url1.openConnection();
urlcon.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36");
img1 = ImageIO.read(url1.openStream());
img2 = ImageIO.read(url2.openStream());
} 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;
System.out.println("diff percent: " + (p * 100.0));
}
}
我運行應用程序時的錯誤
java.io.IOException: Server returned HTTP response code: 403 for URL: http://rosettacode.org/mw/images/3/3c/Lenna50.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at ImgDiffPercent.main(ImgDiffPercent.java:32)
Exception in thread "main" java.lang.NullPointerException
at ImgDiffPercent.main(ImgDiffPercent.java:37)
我已經嘗試了與此相關的堆棧流准則,但是仍然無法解決問題。 幫我解決。
謝謝...
我嘗試了您的代碼:
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new URL("http://rosettacode.org/mw/images/3/3c/Lenna50.jpg"));
}
而且我得到了與您相同的錯誤:
Exception in thread "main" javax.imageio.IIOException: Can't get input stream from URL!
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://rosettacode.org/mw/images/3/3c/Lenna50.jpg
您收到的錯誤是由於服務器的403答復,而不是由於您的代碼。
403禁止 :
服務器理解了該請求,但拒絕執行該請求。 授權將無濟於事,不應重復該請求。 如果請求方法不是HEAD,並且服務器希望公開為什么未滿足請求,則應在實體中描述拒絕原因。
您遇到了此問題,因為此站點使用SSL。 有關更多信息,請檢查此鏈接: 403 Java禁止
測試以下代碼,它可以正常工作並在控制台上打印==>差異百分比:1.6255930981604882
public static void main(String[] args)
{
// TODO Auto-generated method stub
BufferedImage img1 = null;
BufferedImage img2 = null;
try
{
URL url1 = new URL("http://rosettacode.org/mw/images/3/3c/Lenna50.jpg");
URL url2 = new URL("http://rosettacode.org/mw/images/b/b6/Lenna100.jpg");
URLConnection conn1 = url1.openConnection();
conn1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
InputStream in1 = conn1.getInputStream();
URLConnection conn2 = url2.openConnection();
conn2.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
InputStream in2 = conn2.getInputStream();
img1 = ImageIO.read(in1);
img2 = ImageIO.read(in2);
} 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;
System.out.println("diff percent: " + (p * 100.0));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.