上载图像并尝试从驱动器Android API进行OCR
我正在尝试从android代码上传一个图像到谷歌硬盘,并在上面执行ocr。 无法确定应如何请求ocr选项。我在这里读了几个答案,说我应该在某个地方使用.insert()方法,但我找不到这样的方法。Android开发者网站未能提供有关此api的适当文档 如果有人能告诉我要在我的代码中添加什么,请:上载图像并尝试从驱动器Android API进行OCR,android,google-drive-api,ocr,Android,Google Drive Api,Ocr,我正在尝试从android代码上传一个图像到谷歌硬盘,并在上面执行ocr。 无法确定应如何请求ocr选项。我在这里读了几个答案,说我应该在某个地方使用.insert()方法,但我找不到这样的方法。Android开发者网站未能提供有关此api的适当文档 如果有人能告诉我要在我的代码中添加什么,请: public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
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");
}
}
}
public类MainActivity扩展活动实现
GoogleAppClient.ConnectionCallbacks,
GoogleAppClient.OnConnectionFailedListener{
...
私有类EditFileAsyncTask扩展了AsyncTask{
@凌驾
受保护的布尔doInBackground(DriveApi.ContentsResult…params){
DriveApi.ContentsResult ContentsResult=params[0];
如果(!contentsResult.getStatus().isSuccess()){
showMessage(“未能创建新内容”);
返回false;
}
showMessage(“创建新内容”);
OutputStream OutputStream=contentsResult.getContents().getOutputStream();
ByteArrayOutputStream bitmapStream=新建ByteArrayOutputStream();
mbitmatosave.compress(Bitmap.CompressFormat.PNG,100,bitmapStream);
试一试{
write(bitmapStream.toByteArray());
}捕获(IOE异常){
showMessage(“无法写入文件内容”);
e、 printStackTrace();
}
MetadataChangeSet MetadataChangeSet=新建MetadataChangeSet.Builder()
.setMimeType(“图像/jpeg”)
.setTitle(“Rochare1.jpg”)
.build();
IntentSender IntentSender=Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialContents(contentsResult.getContents())
.构建(mGoogleApiClient);
试一试{
startinentsenderforresult(intentSender,请求代码\u创建者,null,0,0,0);
}捕获(发送){
showMessage(“未能启动文件选择器”);
e、 printStackTrace();
}
返回true;
}
@凌驾
受保护的void onPostExecute(布尔结果){
如果(!结果){
showMessage(“编辑内容时出错”);
返回;
}
showMessage(“已成功编辑内容”);
}
}
}
我假设这个任务将是请求ocr的地方,如果不是,请告诉我。这里有一个PHP脚本,使用插入方法上载文件,这似乎是您尝试为Android完成的任务-不确定这是否会有帮助,但这确实解释了下面的插入方法 您还可以在加载文件后编辑文件属性,这可能是使用“补丁”方法的解决方案 }
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);