3

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

我找到了一些像下面这样的例子,结果会给我一个执行反向图像搜索的 url。
虽然我想知道如何从结果站点下载其中一个图像。

要补充的是,我已经阅读了新的 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();
    }
 }
}
4

0 回答 0