使用 Google Drive 在 Android 中集成 OCR

Posted

技术标签:

【中文标题】使用 Google Drive 在 Android 中集成 OCR【英文标题】:OCR integration in Android using Google Drive 【发布时间】:2013-04-20 04:09:27 【问题描述】:

我搜索了很多,但我还没有成功。我不想使用 Tesseract 方法。

我已经根据

创建了项目

https://developers.google.com/drive/quickstart-android

在运行时,图像被上传到了 goggle docs 。我收到了文件上传成功的消息,文件名为 .

用于下载我关注的数据

public void downloadfile(File file)
  
      String imageAsTextUrl = file.getExportLinks().get("text/plain");

      HttpClient client = new DefaultHttpClient();
      HttpGet get = new HttpGet(imageAsTextUrl);
      HttpResponse response;

      StringBuffer sb = new StringBuffer();
      BufferedReader in = null;
      try 
      
          response = client.execute(get);
          in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
          String str;
          while ((str = in.readLine()) != null) 
          
              sb.append(str);
          
          Log.e(" sb data ", sb.toString());
          in.close();
       
      catch (ClientProtocolException e) 
      
          e.printStackTrace();
       
      catch (IOException e) 
      
          e.printStackTrace();
      
  

我得到了返回数据

" <!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8"> <title>Welcome to Google Docs</title><style type="text/css"> html, body, div, h1, h2, h3, h4, h5, h6, p, img, dl, dt, dd, ol, ul, li, table, tr, td, form, object, embed, article, aside, canvas, command, details, fieldset, figcaption, figure, footer, group, header, hgroup, legend, mark, menu, meter, nav, output, progress, section, summary, time, audio, video margin: 0; padding: 0; border: 0; article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section display: block; html font: 81.25% arial, helvetica, sans-serif; background: #fff; color: #333; line-height: 1; direction: ltr; a color: #15c; text-decoration: none; a:active color: #d14836; a:hover text-decoration: underline; h1, h2, h3, h4, h5, h6 color: #222; font-size: 1.54em; font-weight: normal; line-height: 24px; margin: 0 0 .46em; p line-height: 17px; margin: 0 0 1em; ol, ul list-style: none; line-height: 17px; margin: 0 0 1em; li margin: 0 0 .5em; table border-collapse: collapse; border-spacing: 0; strong color: #222; </style><style type="text/css"> html, body position: absolute; height: 100%; min-width: 100%; .wrapper position: relative; min-height: 100%; .wrapper + style + iframe display: none; .content padding: 0 44px; .topbar text-align: right; padding-top: .5em; padding-bottom: .5em; .google-header-bar height: 71px; background: #f1f1f1; border-bottom: 1px solid #e5e5e5; overflow: hidden; .header .logo margin: 17px 0 0; float: left; .header .signin, .header .signup margin: 28px 0 0; float: right; font-weight: bold; .header .signin-button, .header .signup-button margin: 22px 0 0; float: right; .header .signin-button a font-size: 13px; font-weight: normal; .header .signup-button a position: relative; top: -1px; margin: 0 0 0 1em; .main margin: 0 auto; width: 650px; padding-top: 23px; padding-bottom: 100px; .main h1:first-child margin: 0 0 .92em; .google-footer-bar position: absolute; bottom: 0; height: 35px; width: 100%; border-top: 1px solid #ebebeb; overflow: hidden; .footer padding-top: 9px; font-size: .85em; white-space: nowrap; line-height: 0; .footer ul color: #999; float: left; max-width: 80%; .footer ul li display: inline; padding: 0 1.5em 0 0; .footer a color: #333; .footer .lang-chooser-wrap float: right; max-width: 20%; .footer .lang-chooser-wrap img vertical-align: middle; .footer .attribution float: right; .footer .attribution span vertical-align: text-top; .redtext color: #dd4b39; .greytext color: #555; .secondary font-size: 11px; color: #666; .source color: #093; .hidden display: none; .announce-bar position: absolute; bottom: 35px; height: 33px; z-index: 2; width: 100%; background: #f9edbe; border-top: 1px solid #efe1ac; border-bottom: 1px solid #efe1ac; overflow: hidden; .announce-bar .message font-size: .85em; line-height: 33px; margin: 0; .announce-bar .message .separated margin-left: 1.5em; .announce-bar-ac background: #eee; border-top: 1px solid #e5e5e5; border-bottom: 1px solid #e5e5e5; .clearfix:after visibility: hidden; display: block; font-size: 0; content: '.'; clear: both; height: 0; * html .clearfix zoom: 1; *:first-child+html .clearfix zoom: 1; pre font-family: monospace; position: absolute; left: 0; margin: 0; padding: 1.5em; font-size: 13px; background: #f1f1f1; border-top: 1px solid #e5e5e5; direction: ltr; </style><style type="text/css"> button, input, select, textarea font-family: inherit; font-size: inherit; button::-moz-focus-inner, input::-moz-focus-inner border: 0; input[type=email], input[type=number], input[type=password], input[type=tel], input[type=text], input[type=url] -webkit- "

但这不会返回图片中的文字,我上传了。如何获取文本,我作为图片上传?

当我尝试时

Android Open and Save files to/from Google Drive SDK

下载时出现错误 “DriveRequest driveRequest = (DriveRequest) request;” 它无法将 HttpRequest 转换为 DriveRequest

然后我尝试了

DriveRequest driveRequest = DriveRequest.class.cast(request);

但是我在说 ClassCastExcaption 的那一行中遇到了崩溃。

欢迎任何相关答案。提前致谢。

【问题讨论】:

【参考方案1】:

我的答案是正确的添加

get.setHeader("Authorization", "Bearer " + credential.getToken());

downloadfile 方法。

由于我的身份验证不正确,它返回了 Google Docs 欢迎页面的 HTML。添加这一行后,我得到了正确识别的文本。

【讨论】:

以上是关于使用 Google Drive 在 Android 中集成 OCR的主要内容,如果未能解决你的问题,请参考以下文章

Android API Google Drive“连接失败”

使用预建帐户登录,无需手动登录 - Android Google Drive API

Google Drive 范围 drive.file 不足以将应用拥有的文件复制到应用用户的 Google Drive

将数据库与 Android 设备和 Google Drive 同步

有人可以为 Google Drive REST API v3 提供最新的 Android 指南吗?

来自 Google Drive Android Studio API 的图像被旋转