用于 OCR 的 Google Drive SDK
Posted
技术标签:
【中文标题】用于 OCR 的 Google Drive SDK【英文标题】:Google Drive SDK for OCR 【发布时间】:2013-07-28 18:02:04 【问题描述】:我刚刚在this 链接上设置了适用于 android 的 quickstart google drive sdk 应用程序
我正在尝试上传图片,然后对其进行 OCR。 Android 快速入门上的示例应用程序运行良好,但是当我尝试将 OCR 的布尔值设置为 true 时,我得到以下 IOException:
07-29 03:33:01.172: D/OCR_SERVICE(22602): Error 2: 400 Bad Request
07-29 03:33:01.172: D/OCR_SERVICE(22602):
07-29 03:33:01.172: D/OCR_SERVICE(22602): "code": 400,
07-29 03:33:01.172: D/OCR_SERVICE(22602): "errors": [
07-29 03:33:01.172: D/OCR_SERVICE(22602):
07-29 03:33:01.172: D/OCR_SERVICE(22602): "domain": "global",
07-29 03:33:01.172: D/OCR_SERVICE(22602): "message": "Bad Request",
07-29 03:33:01.172: D/OCR_SERVICE(22602): "reason": "badRequest"
07-29 03:33:01.172: D/OCR_SERVICE(22602):
07-29 03:33:01.172: D/OCR_SERVICE(22602): ],
07-29 03:33:01.172: D/OCR_SERVICE(22602): "message": "Bad Request"
07-29 03:33:01.172: D/OCR_SERVICE(22602):
有趣的是,我能够进行大约 2 次 OCR,然后我可能已经修改了源代码或其他东西,但现在它无法正常工作。但我记得我必须对快速入门应用程序进行的唯一更改就是将这一行:File file = service.files().insert(body, mediaContent).execute();
更改为以下内容:
File file = service.files().insert(body, mediaContent).setOcr(true).execute();
这是我的代码:
public class MainActivity extends Activity
static final int REQUEST_ACCOUNT_PICKER = 1;
static final int REQUEST_AUTHORIZATION = 2;
static final int CAPTURE_IMAGE = 3;
private static Uri fileUri;
private static Drive service;
private GoogleAccountCredential credential;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data)
switch (requestCode)
case REQUEST_ACCOUNT_PICKER:
if (resultCode == RESULT_OK && data != null && data.getExtras() != null)
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null)
credential.setSelectedAccountName(accountName);
service = getDriveService(credential);
startCameraIntent();
break;
case REQUEST_AUTHORIZATION:
if (resultCode == Activity.RESULT_OK)
saveFileToDrive();
else
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
break;
case CAPTURE_IMAGE:
if (resultCode == Activity.RESULT_OK)
saveFileToDrive();
private void startCameraIntent()
String mediaStorageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getPath();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
fileUri = Uri.fromFile(new java.io.File(mediaStorageDir + java.io.File.separator + "IMG_"
+ timeStamp + ".jpg"));
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(cameraIntent, CAPTURE_IMAGE);
private void saveFileToDrive()
Thread t = new Thread(new Runnable()
@Override
public void run()
try
// File's binary content
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
File file = service.files().insert(body, mediaContent).setOcr(true).execute();
if (file != null)
showToast("Photo uploaded: " + file.getTitle());
startCameraIntent();
catch (UserRecoverableAuthIOException e)
Log.d("OCR_SERVICE", "Error 1: " + e.getMessage());
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
catch (IOException e)
Log.d("OCR_SERVICE", "Error 2: " + e.getMessage());
e.printStackTrace();
);
t.start();
private Drive getDriveService(GoogleAccountCredential credential)
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
.build();
public void showToast(final String toast)
runOnUiThread(new Runnable()
@Override
public void run()
Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
);
【问题讨论】:
收到 http 400 错误可能是损坏的图像 blob。 我遇到了完全相同的问题。可能是因为 mime 类型?由于驱动器文档将同时具有图像和文本? 您可以尝试使用 application/vnd.google-apps.photo,如developers.google.com/drive/mime-types中所述 【参考方案1】:我刚刚从文档工作中完成了这个 Google Drive 快速入门示例。代码已经过时,一开始甚至没有编译。一旦成功,我在设备上运行应用程序时遇到了一些其他问题。
我已经列出了使其工作所需的更改,并提交给以下 github 项目。这是一个 Eclipse ADT 项目,因此请随时检查并与您的代码进行比较。我在启用 OCR 选项的情况下进行了测试并验证了结果。
https://github.com/hanscappelle/more-android-examples/tree/master/DriveQuickstart
自述文件概述了所有必需的更改。
【讨论】:
以上是关于用于 OCR 的 Google Drive SDK的主要内容,如果未能解决你的问题,请参考以下文章
IOS:如何使用 Google Drive sdk 库将文件上传到特定的 Google Drive 文件夹
google-drive-sdk 导出每日限制未经身份验证的使用
google drive SDK for iOS 是不是支持 64 位架构?