Hallo zusammen,
ich würde gerne die Funktion von Google "Suche anhand von Bildern" nutzen und ein Bild hochladen. Leider gibt es dafür noch keine API.
Mit folgendem HTML-Code kann man Bilder hochladen:
Jetzt ist die Frage, wie kann man das in Java hinbekommen, da redirects mit drinne sind und FollowRedirects(true) nicht funktioniert.
Ich verwende die Libraries von Apache: commons-logging.jar, commons-codec-1.3.jar, commons-httpclient-3.0-rc4.jar
Hier mein Java-Code:
[Java]
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
public class GoogleWrapper {
public String test (String url,String filename) throws IOException{
HttpClient client = new HttpClient();
String response = url;
client.getParams().setParameter("http.useragent", "Test Client");
client.getParams().setParameter("http.connection.timeout",new Integer(5000));
PostMethod method = new PostMethod();
FileOutputStream fos = null;
BufferedReader br = null;
method = new PostMethod(url);
method.addParameter("image_content","");
method.addParameter("filename","");
method.addParameter("hl","de");
method.addParameter("sa","G");
method.addParameter("bih","373");
method.addParameter("biw","1135");
method.addParameter("btnG", "Suchen");
File file = new File(filename);
Part[] parts = {new FilePart("file", file.getName(), file)};
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
try{
int returnCode = client.executeMethod(method);
int statuscode = method.getStatusCode();
System.out.println("STATUS CODE = "+statuscode);
if ((statuscode == 301) | (statuscode == 302)) {
// feed has moved
Header location = method.getResponseHeader("Location");
if (!location.getValue().equals("")) {
// recursively check URL until it's not redirected any more
String responseBody = method.getResponseBodyAsString();
System.out.println(responseBody);
String redirectLocation = location.getValue();
System.out.println(redirectLocation);
method = new PostMethod(redirectLocation);
method.setRequestHeader(location);
returnCode = client.executeMethod(method);
statuscode = method.getStatusCode();
System.out.println("STATUS CODE = "+statuscode);
}
} else {
response = url;
}
if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
System.err.println("The Post method is not implemented by this URI");
// still consume the response body
method.getResponseBodyAsString();
String responseBody = method.getResponseBodyAsString();
System.out.println(responseBody);
} else {
if(returnCode == HttpStatus.SC_OK)
{
String responseBody = method.getResponseBodyAsString();
System.out.println(responseBody);
}
}
} catch (Exception e) {
System.err.println(e);
} finally {
method.releaseConnection();
if(br != null) try { br.close(); } catch (Exception fe) {}
}
return null;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
GoogleWrapper wrapper = new GoogleWrapper();
String filename = "D:/Photos/026.jpg";
wrapper.test("http://images.google.com/searchbyimage/upload",filename);
}
}
[/Java]
Und hier meine Konsolen-Ausgabe:
Für jeden Ansatz wäre ich dankbar.
Grüße
Mathias
ich würde gerne die Funktion von Google "Suche anhand von Bildern" nutzen und ein Bild hochladen. Leider gibt es dafür noch keine API.
Mit folgendem HTML-Code kann man Bilder hochladen:
HTML:
<html>
<head>
</head>
<body>
<form action="http://images.google.com/searchbyimage/upload" enctype="multipart/form-data" method="post">
<input type="file" name="encoded_image">
<input type="hidden" name ="image_content" value="">
<input type="hidden" name ="filename" value="">
<input type="hidden" name ="hl" value="de">
<input type="hidden" name ="sa" value="G">
<input type="hidden" name ="bih" value="373">
<input type="hidden" name ="biw" value="1135">
<input type="hidden" name ="btnG" value="Suchen">
<input type="hidden" name ="image_content" value="">
<input type="submit">
</form>
</body>
</html>
Jetzt ist die Frage, wie kann man das in Java hinbekommen, da redirects mit drinne sind und FollowRedirects(true) nicht funktioniert.
Ich verwende die Libraries von Apache: commons-logging.jar, commons-codec-1.3.jar, commons-httpclient-3.0-rc4.jar
Hier mein Java-Code:
[Java]
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
public class GoogleWrapper {
public String test (String url,String filename) throws IOException{
HttpClient client = new HttpClient();
String response = url;
client.getParams().setParameter("http.useragent", "Test Client");
client.getParams().setParameter("http.connection.timeout",new Integer(5000));
PostMethod method = new PostMethod();
FileOutputStream fos = null;
BufferedReader br = null;
method = new PostMethod(url);
method.addParameter("image_content","");
method.addParameter("filename","");
method.addParameter("hl","de");
method.addParameter("sa","G");
method.addParameter("bih","373");
method.addParameter("biw","1135");
method.addParameter("btnG", "Suchen");
File file = new File(filename);
Part[] parts = {new FilePart("file", file.getName(), file)};
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
try{
int returnCode = client.executeMethod(method);
int statuscode = method.getStatusCode();
System.out.println("STATUS CODE = "+statuscode);
if ((statuscode == 301) | (statuscode == 302)) {
// feed has moved
Header location = method.getResponseHeader("Location");
if (!location.getValue().equals("")) {
// recursively check URL until it's not redirected any more
String responseBody = method.getResponseBodyAsString();
System.out.println(responseBody);
String redirectLocation = location.getValue();
System.out.println(redirectLocation);
method = new PostMethod(redirectLocation);
method.setRequestHeader(location);
returnCode = client.executeMethod(method);
statuscode = method.getStatusCode();
System.out.println("STATUS CODE = "+statuscode);
}
} else {
response = url;
}
if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
System.err.println("The Post method is not implemented by this URI");
// still consume the response body
method.getResponseBodyAsString();
String responseBody = method.getResponseBodyAsString();
System.out.println(responseBody);
} else {
if(returnCode == HttpStatus.SC_OK)
{
String responseBody = method.getResponseBodyAsString();
System.out.println(responseBody);
}
}
} catch (Exception e) {
System.err.println(e);
} finally {
method.releaseConnection();
if(br != null) try { br.close(); } catch (Exception fe) {}
}
return null;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
GoogleWrapper wrapper = new GoogleWrapper();
String filename = "D:/Photos/026.jpg";
wrapper.test("http://images.google.com/searchbyimage/upload",filename);
}
}
[/Java]
Und hier meine Konsolen-Ausgabe:
Code:
14.12.2011 13:54:11 org.apache.commons.httpclient.HttpMethodDirector isRedirectNeeded
INFO: Redirect requested but followRedirects is disabled
STATUS CODE = 302
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://images.google.com/search?tbs=sbi:AMhZZitnglxc3stDxx0TVFeEEdAdEhEfpaASn3s9r0MkYO_1qELcXh35uYPTTkr48gkv7_1IRkDNoB&file=%FF%D8%FF%E1(~Exif">here</A>.
</BODY></HTML>
http://images.google.com/search?tbs=sbi:AMhZZitnglxc3stDxx0TVFeEEdAdEhEfpaASn3s9r0MkYO_1qELcXh35uYPTTkr48gkv7_1IRkDNoB&file=%FF%D8%FF%E1(~Exif
STATUS CODE = 405
Für jeden Ansatz wäre ich dankbar.
Grüße
Mathias