【问题标题】:Google image search: How do I construct a reverse image search URL?Google 图片搜索:如何构建反向图片搜索 URL?
【发布时间】:2011-09-28 14:26:44
【问题描述】:

我如何通过 java 以编程方式将图像转换为“某些字符串”以将其作为参数传递给在谷歌图像搜索中进行搜索。实际上,我已经对图像进行了一些 base64 转换,但它与谷歌在其图像搜索引擎中所做的不同。我做了这样的转换(java 7):

import javax.xml.bind.DatatypeConverter;
...
            Path p = Paths.get("my_photo.JPG");
            try(InputStream in = Files.newInputStream(p); 
                    PrintWriter write = new PrintWriter("base64.txt");
               ) {
                byte [] bytes = new byte[in.available()];
                in.read(bytes);
                String base64 = DatatypeConverter.printBase64Binary(bytes);
                write.println(base64);

            } catch(IOException ex) {
                ex.printStackTrace();
            }

这个简单程序的输出与 url 中的 google 字符串不同。我说的是tbs=sbi:AMhZZ...之后的那个字符串

【问题讨论】:

  • 那么,我可以用其他方式使用这个 google 的服务吗?我只是想获取指定图片页面的代码,比如在谷歌图片搜索中
  • 我不明白你想要完成什么。能举个例子吗?
  • 我想使用code.google.com/intl/uk/apis/imagesearch/v1/… 中的谷歌图像搜索服务,但我想使用图像作为参数而不是文本参数(注意:我不需要使用 json,只是它正在使用例如)
  • 请记住,这是一项实验性服务,您可能不想构建依赖于它的应用程序。还有其他reverse image search engines
  • 抱歉,我一直在考虑将图像 1:1 转换为字符串,而不是搜索引擎内部实际发生的事情。我的新答案应该更有帮助。

标签: java google-image-search


【解决方案1】:

这是我对图像搜索工作原理的最佳猜测:

URL 中的数据不是图像的编码形式。数据是用于模糊匹配的图像指纹。

您应该注意到,当您上传图片进行搜索时,这是一个两步过程。第一步通过urlhttp://images.google.com/searchbyimage/upload上传图片。 Google 服务器返回指纹。然后,浏览器会被重定向到带有基于指纹的查询字符串的搜索页面。

除非 Google 发布用于生成指纹的算法,否则您将无法从您的应用程序中生成搜索查询字符串。在此之前,您可以让您的应用程序将图像发布到上传 URI。您应该能够解析响应并构造查询字符串。

编辑

这些是我上传文件时发送到服务器的键和值。

image_url       =
btnG            = Search
encoded_image   = // the binary image content goes here
image_content   =
filename        =
hl              = en
bih             = 507
biw             = 1920

“bih”和“biw”看起来像尺寸,但与上传的文件不对应。

使用此信息需要您自担风险。这是一个未记录的 API,可能会更改和破坏您的应用程序。

【讨论】:

  • 谢谢,我猜你在说什么。您能否描述或举例说明我如何向该网址发出带有图片的发布请求?
  • @maks,我希望我的编辑更有帮助。您需要将键/值编码为“multipart/form-data”并将其作为 POST 请求正文发送。您应该能够找到大量有关如何进行编码的示例。
  • 能否请您多多指教...我正在尝试在 WindowsPhone7 上做同样的事情
【解决方案2】:
Using google's image search.

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpFileUpload {
  public static void main(String args[]){
    try {
      HttpClient client = new DefaultHttpClient();
      String url="https://www.google.co.in/searchbyimage/upload";
      String imageFile="c:\\temp\\shirt.jpg";
      HttpPost post = new HttpPost(url);

      MultipartEntity entity = new MultipartEntity();
      entity.addPart("encoded_image", new FileBody(new File(imageFile)));
      entity.addPart("image_url",new StringBody(""));
      entity.addPart("image_content",new StringBody(""));
      entity.addPart("filename",new StringBody(""));
      entity.addPart("h1",new StringBody("en"));
      entity.addPart("bih",new StringBody("179"));
      entity.addPart("biw",new StringBody("1600"));

      post.setEntity(entity);
      HttpResponse response = client.execute(post);
      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));         

      String line = "";
      while ((line = rd.readLine()) != null) {
        if (line.indexOf("HREF")>0)
      System.out.println(line.substring(8));
      }

    }catch (ClientProtocolException cpx){
      cpx.printStackTrace();
    }catch (IOException ioex){
      ioex.printStackTrace();
    }
 }
}

【讨论】:

  • 只需要“encode_image”参数,其余参数不需要。
【解决方案3】:

根据@Ajit 的回答,这样做是一样的,但使用curl 命令(Linux / Cygwin / 等)

curl -s -F "image_url=" -F "image_content=" -F "filename=" -F "h1=en"  -F "bih=179" -F "biw=1600" -F "encoded_image=@my_image_file.jpg" https://www.google.co.in/searchbyimage/upload

这将在标准输出上打印一个 URL。您可以使用 curlwget 下载该 URL,但您可能需要将用户代理更改为 Chrome 等图形网络浏览器的用户代理。

【讨论】:

    【解决方案4】:

    这对我有用。实际上不需要任何编码。

    https://www.google.com/searchbyimage?image_url=YOUR_IMAGE_URL
    

    【讨论】:

    • 这是一张在线上传的图片
    【解决方案5】:

    为此使用Google Vision API。 Google 也提供了很多示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多