是否有任何 API 可以从 Java 中的 Google 图像下载图像以及如何使用它。
我使用了https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=boy&rsz=8但它给出的结果不超过 2 个,我想在JDesktopPane
我试过的时候显示所有结果。提前致谢
public static void main(String[] args) {
try{
URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=boy&rsz=8");
URLConnection connection = url.openConnection();
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
System.out.println(builder.toString());
// for(int i=0;i<8;i++)
// {
JSONArray js = json.getJSONObject("responseData").getJSONArray("results");
for(int j=0;j<js.length();j++)
{
// }
JSONObject json1 = js.getJSONObject(j);
String imageUrl=json1.getString("url");
// String imageUrl = json.getJSONObject("responseData").getJSONArray("results").getJSONObject(i).getString("url");
BufferedImage image = ImageIO.read(new URL(imageUrl));
// for downloading the image
// connection.setRequestProperty("User-Agent","Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)");
File outputfile = new File("D:\\saved"+j+".jpg");
ImageIO.write(image, "jpg", outputfile);
System.out.println("downloded"+j);
}
//JOptionPane.showMessageDialog(null, "", "", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(image));
} catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}