如何使用谷歌API从谷歌图像保存图像?

编程入门 行业动态 更新时间:2024-10-09 14:18:53
本文介绍了如何使用谷歌API从谷歌图像保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在Google图片中搜索一些不同的图片并使用java Google API保存每个查询的第一个结果。

i'm trying to search in Google-images some different and save the first result for every query with java Google API.

我设法在Google中搜索获取包含搜索结果的json对象。该对象包含包含图像的网站,而不是图像地址

I managed to search in Google and get the json object which contains the search results. the object contains the web sites which contains the images,and not the image address

代码:

URL url = new URL("ajax.googleapis/ajax/services/search/images?" + "v=1.0&q="+properties.getProperty(Integer.toString(i))+"&userip=IP"); URLConnection connection = url.openConnection(); connection.addRequestProperty("Referer", "images.google"); 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())

如果我有图像链接,我也知道如何保存图像。

I'm also know how to save image if i had the image link.

我的问题是如何获得第一个(或第二个或其他)图像正确的地址而不是网址(例如www.yadayadayada/image.png)

my problem is how to get the first (or second or whatever) image right address and not the web address (example www.yadayadayada/image.png)

10x

推荐答案

JSON界面在 JSON开发人员指南。特别是, JSON参考部分概述了响应格式和保证字段。您可以使用值 url 属性。

JSON interface is described at JSON Developer's Guide. In particular, JSON reference section outlines response format and guaranteed fields. You can use a value of url property.

根据URL,您可以使用 ImageIO 。以下是相关的教程。

Given the URL, you can read the image and write it to the disk using ImageIO. Here is the relevant tutorial.

如果不需要图像处理和显示,那么你可以使用 HttpURLConnection 只需下载文件。

If the image manipulation and presentation is not required, then you could use HttpURLConnection to simply download the file.

编辑:示例

以下是基于问题中包含的代码的简单示例。它执行搜索并显示第一个图像。

Below is a simple example based on the code included in the question. It performs a search and displays the first image.

import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.InputStreamReader; import java.URL; import java.URLConnection; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class TestImage { public static void main(String[] args) { try{ URL url = new URL("ajax.googleapis/ajax/services/search/images?v=1.0&q=Godfather"); 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()); String imageUrl = json.getJSONObject("responseData").getJSONArray("results").getJSONObject(0).getString("url"); BufferedImage image = ImageIO.read(new URL(imageUrl)); JOptionPane.showMessageDialog(null, "", "", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(image)); } catch(Exception e){ JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } } }

更多推荐

如何使用谷歌API从谷歌图像保存图像?

本文发布于:2023-11-13 03:10:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   如何使用   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!