How can I programmatically through java convert an image to "some string" to pass it as a parameter for searching in google image search. Actually I have made some base64 convertion of image but it differs from that that google does in its image search engine. I've made such a convertion(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();
            }

the output of this simple program differs from the google's string in url. I talk about that string that goes after tbs=sbi:AMhZZ...

share|improve this question

so, can I use this google's service in another way. I just simply want to get the code of a page of specified image, like in google image search – maks Sep 28 '11 at 14:53
I don't understand what you are trying to accomplish. Can you give an example? – mikerobi Sep 28 '11 at 15:00
i want to use a google image search service as in code.google.com/intl/uk/apis/imagesearch/v1/… but instead of text parameters I want to use image as a parameter(note: I needn't to use json, simply it is using in example) – maks Sep 28 '11 at 15:09
Keep in mind that this is an experimental service, you might not want to build an application that depends on it. There are other reverse image search engines – mikerobi Sep 28 '11 at 15:23
Sorry, I was stuck thinking about a 1:1 conversion of an image to a string, not what was actually going on inside the search engine. My new answer should be more helpful. – mikerobi Sep 28 '11 at 16:28
feedback

1 Answer

up vote 4 down vote accepted

This is my best guess for how the image search works:

The data in the URL is not an encoded form of the image. The data is an image fingerprint used for fuzzy matching.

You should notice that when you upload an image for searching, it is a 2 step process. The first step uploads the image via the url http://images.google.com/searchbyimage/upload. The Google server returns the fingerprint. The browser is then redirected to a search page with a query string based on the fingerprint.

Unless Google publishes the algorithm for generating the fingerprint, you will be unable to generate the search query string from within your application. Until then, you can have your application post the image to the upload URI. You should be able to parse the response and construct the query string.

EDIT

These are the keys and values sent to the server when I uploaded a file.

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

"bih" and "biw" look like dimensions, but do not corrispond to the uploaded file.

Use this information at your own risk. It is an undocumented api that could change and break your application.

share|improve this answer
Thanks, I guess what you are talking about. Can you describe or give an example how I can make a post request with image to that url? – maks Sep 28 '11 at 18:12
@maks, I hope my edit is more helpful. You need to encode the key/values as "multipart/form-data" and send it as the POST request body. You should be able to find plenty of examples on how to do the encoding. – mikerobi Sep 28 '11 at 19:04
Can you please enlighten more on this...I'm trying to do the same on WindowsPhone7 – SirwaniMayur Nov 29 '11 at 9:40
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.