Google图片search:如何构build反向图片searchurl?

我怎样才能以编程方式通过Java将图像转换为“一些string”,将其作为parameter passing给谷歌图像search。 其实我已经做了一些base64转换的形象,但它不同于谷歌在其图像search引擎。 我做了这样的转换(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(); } 

这个简单的程序的输出不同于在谷歌的谷歌的string。 我谈论那个在tbs=sbi:AMhZZ...之后的stringtbs=sbi:AMhZZ...

这是我对图像search工作的最佳猜测:

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

你应该注意到,当你上传一个图片进行search时,这是一个两步的过程。 第一步是通过url http://images.google.com/searchbyimage/upload上传图片。 Google服务器返回指纹。 然后,浏览器根据指纹redirect到带有查询string的search页面。

除非Google发布生成指纹的algorithm,否则将无法从应用程序中生成search查询string。 在此之前,您可以让应用程序将映像发布到上传URI。 您应该能够parsing响应并构build查询string。

编辑

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

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

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

使用这些信息需要您自担风险。 这是一个无证的API,可以改变和打破你的应用程序。

 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(); } } } 

基于@Ajit的回答,这是相同的,但是使用curl命令(Linux / Cygwin / etc)

 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等graphics浏览器。