mak*_*aks 10 java google-image-search
如何通过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();
}
Run Code Online (Sandbox Code Playgroud)
这个简单程序的输出与url中google的字符串不同.我谈到了之后的那个字符串tbs=sbi:AMhZZ...
mik*_*obi 11
这是我对图像搜索工作原理的最佳猜测:
URL中的数据不是图像的编码形式.数据是用于模糊匹配的图像指纹.
您应该注意到,当您上传图像进行搜索时,这是一个两步过程.第一步是通过网址上传图片http://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
Run Code Online (Sandbox Code Playgroud)
"bih"和"biw"看起来像尺寸,但不会对上传的文件产生任何影响.
使用此信息需要您自担风险.它是一个无证的api,可以改变和破坏你的应用程序.
小智 7
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)