How to search image by image based search (reverse image search Google) in android java

191 views Asked by At

I coded the app a year ago. Its works and easily search images by providing the image in the Google search bar, latter in 2023 when I upload an image for searching, it says the image search is unavailable.

my code is

 String hash = GooglePhotoHash.hashFromBitmap(bitmap); 
 Intent openHashURLinBrowser = (new Intent(SingleFrmGall.this, WebActivity.class));
 openHashURLinBrowser.setData(Uri.parse(hash));

GooglePhotoHash code is

public class GooglePhotoHash {
    private static final String GOOGLE_IMAGE_UPLOAD_URL = "https://www.google.com/searchbyimage/upload";

    private static HttpEntity buildRequestForBitmap(Bitmap bitmap) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
        byte[] data = bos.toByteArray();
        @SuppressLint("DefaultLocale") String fileName = String.format("File_%d.png", new Date().getTime());
        ByteArrayBody bab = new ByteArrayBody(data, fileName);
        return MultipartEntityBuilder.create().addPart("encoded_image", bab)
                .addPart("image_url", new StringBody("", ContentType.TEXT_PLAIN))
                .addPart("image_content", new StringBody("", ContentType.TEXT_PLAIN))
                .addPart("filename", new StringBody("", ContentType.TEXT_PLAIN))
                .addPart("h1", new StringBody("en", ContentType.TEXT_PLAIN))
                .addPart("bih", new StringBody("179", ContentType.TEXT_PLAIN))
                .addPart("biw", new StringBody("1600", ContentType.TEXT_PLAIN)).build();
    }

    // Post request and get response
    private static BufferedReader sendRequest(CloseableHttpClient client, HttpPost post, HttpEntity entity)
            throws IOException {
        post.setEntity(entity);
        CloseableHttpResponse response = client.execute(post);
        InputStream content = response.getEntity().getContent();
        return new BufferedReader(new InputStreamReader(content));
    }

    // Parse response
    private static String parseResponse(BufferedReader reader) throws IOException {
        String line;
        String hashURL = "";
        while ((line = reader.readLine()) != null)
            if (line.indexOf("HREF") > 0)
                hashURL = line.substring(9, line.length() - 11);
        if (hashURL.equals(""))
            throw new IOException();
        return hashURL;
    }

    // Accepts file object of image and returns hash URL
    public static String hashFromBitmap(Bitmap bitmap) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(GOOGLE_IMAGE_UPLOAD_URL);
        HttpEntity entity = buildRequestForBitmap(bitmap);
        BufferedReader reader = sendRequest(client, post, entity);
        return parseResponse(reader);
    }}

and show the following result enter image description here

0

There are 0 answers