![](/img/trans.png)
[英]How to upload encrypted image file on google drive from android using Google Drive API
[英]upload image and attempt OCR from Drive Android API
我正在尝试将图像从android代码上传到Google驱动器并对其执行ocr。 无法弄清楚应如何请求ocr选项。 我在这里阅读了几个答案,说应该在某个地方使用.insert()方法,但找不到这种方法。 Android Developer网站无法在此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的地方,否则请告诉我。
这是一个使用insert方法上传文件的PHP脚本,这似乎是您要为Android尝试完成的任务-不确定这是否有帮助,但这确实在下面解释了insert方法。
您还可以在文件加载后编辑文件属性,这可能是使用“补丁”方法的解决方案
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.