Google Drive SDK - Image Download, OCR, Download Result

So, in the end, I'm trying to upload the images that I want to Google OCR. Then I want the OCR results back to my Android application. I have uploaded my images correctly. I can iterate over all the files on my Google drive, and I see that there are available export links, one of which is text / plain. If I use one of these URLs in a browser, it loads the text. So should I try to access it?

I tried using the url that I get from calling the getExportLinks method in the file returned by the insert method

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

I get the HTML back, which appears to be the main page of Google Drive. In order to get the exported url document, I used an instance of the Google drive, so it must be authenticated correctly, as the insertion 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?

+4
source share
1 answer

Well, I answered my question again, sort of like. Basically, since this is similar to a web URL, not an API call that I can make, then it does not respond 401 if it has not been authenticated. So basically the answer I got is the HTML for the login page. Apparently using DriveRequest doesn't automatically handle authentication as I thought. So I work by adding authentication manually to call the HttpClient GET.

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

Here is what I did to download a text / plain view of the file. Here's a caveat: given that the image I uploaded was taken to the cell phone camera using the default camera app, the default value for dpi and / or jpeg compression made OCR not work very well. Anyway, here is the code I used. Just the base material of HttpClient

  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); 
+3
source

Source: https://habr.com/ru/post/1434897/


All Articles