android - 从 Drive Android API 上传图片并尝试 OCR

标签 android google-drive-api ocr

我正在尝试从 android 代码将图像上传到 google drive 并对其执行 ocr。 无法弄清楚应该如何请求 ocr 选项。我在这里阅读了几个关于 SO 的答案,说我应该在某处使用 .insert() 方法,但我找不到这样的方法。 Android 开发者网站未能提供有关此 API 的适当文档。

如果有人可以告诉我要添加到我的代码中的内容,请:

public class MainActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

...

    private class EditFileAsyncTask extends AsyncTask<DriveApi.ContentsResult, Void, Boolean> {

        @Override
        protected Boolean doInBackground(DriveApi.ContentsResult... params) {
            DriveApi.ContentsResult contentsResult = params[0];
            if (!contentsResult.getStatus().isSuccess()) {
                showMessage("Failed to create new contents.");
                return false;
            }
            showMessage("New contents created.");
            OutputStream outputStream = contentsResult.getContents().getOutputStream();
            ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
            mBitmapToSave.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
            try {
                outputStream.write(bitmapStream.toByteArray());
            } catch (IOException e) {
                showMessage("Unable to write file contents.");
                e.printStackTrace();
            }

            MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                    .setMimeType("image/jpeg")
                    .setTitle("rochale1.jpg")
                    .build();

            IntentSender intentSender = Drive.DriveApi
                    .newCreateFileActivityBuilder()
                    .setInitialMetadata(metadataChangeSet)
                    .setInitialContents(contentsResult.getContents())
                    .build(mGoogleApiClient);
            try {
                startIntentSenderForResult(intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
            } catch (SendIntentException e) {
                showMessage("Failed to launch file chooser.");
                e.printStackTrace();
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (!result) {
                showMessage("Error while editing contents");
                return;
            }
            showMessage("Successfully edited contents");
        }
    }
}

我假设这个任务是请求 ocr 的地方,如果不是请告诉我。

最佳答案

这是一个使用插入方法上传文件的 PHP 脚本,这似乎是您要为 Android 完成的任务 - 不确定这是否有帮助,但这确实解释了下面的插入方法....

您还可以在加载后编辑文件属性,这可能是您使用“补丁”方法的解决方案

https://developers.google.com/drive/v2/reference/files/patch

https://developers.google.com/drive/v2/reference/files/insert

function uploadFile($service,$title,$description,$filePath,$mtype){

//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($title.".pdf");
$file->setDescription($description);
$file->setMimeType('application/pdf');

$data = file_get_contents($filePath);

sleep(1);

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => $mtype,
      'uploadType' => 'multipart',
      'convert' => true,
          'ocr'=> true
    ));


return ($createdFile);

关于android - 从 Drive Android API 上传图片并尝试 OCR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23185403/

相关文章:

android - Firebase 中单个主题订阅的用户最大限制是多少?

google-drive-api - 如何使用 Google Drive API 将 Google 文档转换为 MS Word 格式?

go - 如何使用Google云端硬盘API(v3)获取目录中所有文件的列表

google-apps-script - 上传到 Google Drive 的 URL

android - 无法更新 Android Studio - 只能下载

java - libgdx 布娃娃物理与动画

android - 仅将 alpha-transparency 设置为布局而不是其子级

python-3.x - 通过 pytesseract 和 PIL 提高文本识别的准确性

c# - 如何从pdf图像中查找文本?

api - 快速屏幕字体OCR技术还是API?