使用Google API进行反向图片搜索

时间:2014-04-24 12:33:12

标签: google-image-search

我正在尝试编写一个java程序,用于循环文件夹中的图像文件和
在谷歌上进行反向图像搜索(只是尝试在这个问题中使用一个图像)

我找到了一些如下所示的例子,结果会给我一个进行反向图像搜索的网址 虽然我想知道如何从结果网站下载其中一张图片。

要添加的一些内容是我已经阅读了新的google api(?)并且我发现它只允许我每天进行100次搜索,因此我选择使用旧版本(?)虽然它警告我方法已弃用。

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 ReverseImageSearch{
  public static void main(String args[]){
    try {
      HttpClient client = new DefaultHttpClient();
      String url="https://www.google.co.in/searchbyimage/upload";
      String imageFile="C:\\Users\\Chan\\Desktop\\pixiv29706591.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));
           //Problem:
           // get one of the result image from the sites
           // and store it in my PC


      }

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

0 个答案:

没有答案