android - Google Drive SDK - 上传图片、OCR、下载结果

标签 android google-drive-api ocr

所以最终我会尝试将我希望谷歌上传到 OCR 的图像上传。然后我希望能够将 OCR 的结果返回到我的 Android 应用程序。我的图片已正确上传。我可以遍历我的谷歌驱动器中的所有文件,我看到有可用的导出链接,其中之一是“text/plain”。如果我在浏览器中使用其中一个 url,它会下载文本。这是我应该尝试访问它的方式吗?

我尝试使用通过对插入方法返回的文件调用 getExportLinks 方法获得的 url

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

我最终得到了似乎是 Google Drive 主页的 HTML。为了获取导出的 url 文档,我使用了 google drive instance,因此它应该像我认为的插入方法一样经过正确验证。

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

有没有人试过这样做?我究竟做错了什么?

最佳答案

好吧,我又一次回答了我自己的问题。基本上,因为这似乎是一个 web url 而不是我可以进行的 API 调用,所以如果它未经身份验证,它不会以 401 响应。所以基本上我得到的响应是登录页面的 HTML。显然,使用 DriveRequest 不会像我想象的那样自动处理身份验证。因此,我通过手动向 HttpClient GET 调用添加身份验证来使其工作。

但是有没有办法用实际的 API 来做我想做的事情呢?所以我可以处理响应代码?

这是我下载文件的文本/纯文本表示所做的。这里有一个警告:鉴于我上传的图像是使用默认相机应用程序在手机相机上拍摄的,默认 dpi 和/或 jpeg 压缩导致 OCR 无法正常工作。无论如何,这是我使用的代码。只是基本的 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);

关于android - Google Drive SDK - 上传图片、OCR、下载结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12481333/

相关文章:

android - 我们如何确保深层链接始终打开我们自己的 native 应用程序而不是打开 chrome 浏览器

android - 使用 glide 显示带 ImageView 的动画 gif 文件(android)

java - 使用 Tesseract hOCR 提取文本属性

python-3.x - Google Drive API Python 服务帐户示例

google-apps-script - Google App 脚本 - DriveApp - getParents() 未按预期工作

android - 通过 OCR Tesseract 从图像中提取文本 - 错误 : can't find source file on baseApi. ini

python - NLP:文档 OCR 中单词的位置特征

android - 具有固定菜单项的抽屉布局

java - 使用压缩生成损坏的 docx

c# - 使用 C# 的 Google Drive API - 上传