You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio(Java):如何向Google反向图片搜索发送HTTP POST请求?

Android通过POST请求将Bitmap传入Google反向图片搜索

要实现将本地Bitmap通过POST请求传入Google反向图片搜索,你需要把Bitmap转为二进制数据,再通过multipart/form-data格式的POST请求上传,以下是具体实现步骤和代码:

1. 把Bitmap转为字节数组

先将拍摄得到的Bitmap转换成JPEG/PNG格式的字节数组,方便后续上传:

private byte[] bitmapToByteArray(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    // 压缩Bitmap为JPEG格式,质量设为80(可根据需求调整)
    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    return stream.toByteArray();
}

2. 构建Volley的Multipart POST请求

Google反向图片搜索的上传接口接受multipart/form-data类型的请求,我们需要自定义Volley的Request来实现:

private void postRequest(Bitmap bitmap) {
    String url = "https://images.google.com/searchbyimage/upload";
    
    // 转换Bitmap为字节数组
    byte[] imageBytes = bitmapToByteArray(bitmap);
    
    // 自定义MultipartRequest
    StringRequest multipartRequest = new StringRequest(Request.Method.POST, url,
            response -> {
                // 处理搜索结果响应,返回的是搜索页面的HTML内容,可自行解析提取信息
                Log.d("GoogleSearch", "搜索结果响应: " + response);
            },
            error -> {
                Log.e("GoogleSearch", "请求错误: " + error.getMessage());
            }) {
        @Override
        public String getBodyContentType() {
            return "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW";
        }

        @Override
        public byte[] getBody() throws AuthFailureError {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(bos);
            try {
                // 添加图片字段
                dos.writeBytes("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
                dos.writeBytes("Content-Disposition: form-data; name=\"encoded_image\"; filename=\"image.jpg\"\r\n");
                dos.writeBytes("Content-Type: image/jpeg\r\n\r\n");
                dos.write(imageBytes);
                dos.writeBytes("\r\n");
                // 添加Google搜索要求的必要参数
                dos.writeBytes("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
                dos.writeBytes("Content-Disposition: form-data; name=\"image_content\"\r\n\r\n");
                dos.writeBytes("");
                dos.writeBytes("\r\n");
                dos.writeBytes("------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n");
                return bos.toByteArray();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    };

    requestQueue.add(multipartRequest);
}

3. 在onActivityResult中调用postRequest

修改你的onActivityResult方法,获取Bitmap后调用上传请求:

public void onActivityResult(ActivityResult result) {
    if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
        Bundle bundle = result.getData().getExtras();
        Bitmap bitmap = (Bitmap) bundle.get("data");
        
        requestQueue = Volley.newRequestQueue(context);
        postRequest(bitmap); // 执行图片上传搜索
    }
}

注意事项

  • Google反向图片搜索的上传接口属于非公开API,参数或地址可能随时变动,需做好兼容性适配
  • 确保你的App已声明网络权限(android.permission.INTERNET)
  • 如果拍摄的Bitmap尺寸过大,建议先进行缩放压缩,避免请求超时或失败

内容的提问来源于stack exchange,提问作者SKAE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 04:30:22