Google Drive SDK - Uploading Image, OCR, Download

2019-02-07 09:30发布

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 properly. I can loop through all the files in my google drive and I see that there are export links available, one of which is "text/plain". If I use one of these urls in a browser, it downloads the text. Is this the way I should be trying to access it?

I've tried to use the url I get from calling getExportLinks method on the file returned by the insert method

File file = drive.files().insert(body, mediaContent).setOcr(true).execute();
String imageAsTextUrl = getExportLinks.get("text/plain") 

I end up getting HTML back that appears to be the Google Drive home page. To get the exported url document, I used google drive instance so it should have properly authenticated like the insert method I would think.

DriveRequest request = new DriveRequest(drive, HttpMethod.GET, imageAsTextUrl, null);

Has anyone tried to do this before? What am I doing wrong?

1条回答
我想做一个坏孩纸
2楼-- · 2019-02-07 09:32

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);
查看更多
登录 后发表回答