Google Drive SDK - Uploading Image, OCR, Download Result

后端 未结 1 1852
执笔经年
执笔经年 2021-01-01 04:55

So ultimately I\'m trying to upload images that I want Google to OCR. Then I want to be able to get the results of the OCR back to my Android app. I have my images uploading

相关标签:
1条回答
  • 2021-01-01 05:39

    Well I answered my own question yet again, sort of. Basically since this seems to be a web url and not an API call I can make, then it's not responding with a 401 if it's unauthenticated. So basically the response I was getting is the HTML for the login page. Apparently using DriveRequest does not automatically handle authentication like I thought it would. So I have it working by adding authentication manually to an HttpClient GET call.

    But is there a way to do what I'm trying to do with the actual API? So I can deal with response codes?

    Here's what I did to download the text/plain representation of the file. Here's a caveat: given that the image I was uploading was taken on a cell phone camera using the default camera app, the default dpi and/or jpeg compression caused the OCR to not work very well. Anyway, here's the code I used. Just basic HttpClient stuff

                    String imageAsTextUrl = file.getExportLinks().get("text/plain");
    
                    HttpClient client = new DefaultHttpClient();
                    HttpGet get = new HttpGet(imageAsTextUrl);
                    get.setHeader("Authorization", "Bearer " + token);
                    HttpResponse response = client.execute(get);
    
                    StringBuffer sb = new StringBuffer();
    
                    BufferedReader in = null;
                    try {
                        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                        String str;
                        while ((str = in.readLine()) != null) {
                            sb.append(str);
                        }
                    }
                    finally {
                        if (in != null) {
                            in.close();
                        }
                    }
    
                    // Send data to new Intent to display: 
                    Intent intent = new Intent(UploadImageService.this, VerifyTextActivity.class);
                    intent.putExtra("ocrText", sb.toString());
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
    
    0 讨论(0)
提交回复
热议问题