Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now it is possible to select image to be processed (from file, from c…
…amera) Better UI.
- Loading branch information
abbyysdk
committed
Mar 13, 2013
1 parent
f5ea19d
commit 54d5804
Showing
10 changed files
with
395 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<?xml version="1.0" encoding="utf-8" standalone="no"?> | ||
<resources> | ||
|
||
<string name="hello">Hello World, demoActivity!</string> | ||
<string name="app_name">Android.demo</string> | ||
|
||
</resources> | ||
<string name="menu_settings">Settings</string> | ||
<string name="title_activity_main">ABBYY Cloud OCR SDK Demo App</string> | ||
<string name="fromCamera">From Camera</string> | ||
<string name="fromFile">From File</string> | ||
<string name="title_activity_results">Results</string> | ||
<string name="hello">hello</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package abbyy.ocrsdk.android; | ||
|
||
import java.io.FileOutputStream; | ||
|
||
import com.abbyy.ocrsdk.*; | ||
|
||
import android.app.*; | ||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.os.AsyncTask; | ||
|
||
public class AsyncProcessTask extends AsyncTask<String, String, Boolean> { | ||
|
||
public AsyncProcessTask(ResultsActivity activity) { | ||
this.activity = activity; | ||
dialog = new ProgressDialog(activity); | ||
} | ||
|
||
private ProgressDialog dialog; | ||
/** application context. */ | ||
private final ResultsActivity activity; | ||
|
||
protected void onPreExecute() { | ||
dialog.setMessage("Processing"); | ||
dialog.setCancelable(false); | ||
dialog.setCanceledOnTouchOutside(false); | ||
dialog.show(); | ||
} | ||
|
||
protected void onPostExecute(Boolean result) { | ||
if (dialog.isShowing()) { | ||
dialog.dismiss(); | ||
} | ||
|
||
activity.updateResults(); | ||
} | ||
|
||
@Override | ||
protected Boolean doInBackground(String... args) { | ||
|
||
String inputFile = args[0]; | ||
String outputFile = args[1]; | ||
|
||
try { | ||
Client restClient = new Client(); | ||
|
||
!!! Please provide credentials and remove this line. !!! | ||
|
||
// Name of application you created | ||
restClient.applicationId = "<your app_id>"; | ||
// You should get e-mail from ABBYY Cloud OCR SDK service with the application password | ||
restClient.password = "<your app_password>"; | ||
|
||
// Obtain installation id when running the application for the first time | ||
SharedPreferences settings = activity.getPreferences(Activity.MODE_PRIVATE); | ||
String instIdName = "installationId"; | ||
if( !settings.contains(instIdName)) { | ||
// Get installation id from server using device id | ||
String deviceId = android.provider.Settings.Secure.getString(activity.getContentResolver(), | ||
android.provider.Settings.Secure.ANDROID_ID); | ||
|
||
// Obtain installation id from server | ||
publishProgress( "First run: obtaining installation id.."); | ||
String installationId = restClient.activateNewInstallation(deviceId); | ||
publishProgress( "Done. Installation id is '" + installationId + "'"); | ||
|
||
SharedPreferences.Editor editor = settings.edit(); | ||
editor.putString(instIdName, installationId); | ||
editor.commit(); | ||
} | ||
|
||
String installationId = settings.getString(instIdName, ""); | ||
restClient.applicationId += installationId; | ||
|
||
publishProgress( "Uploading image..."); | ||
|
||
String language = "English"; // Comma-separated list: Japanese,English or German,French,Spanish etc. | ||
|
||
ProcessingSettings processingSettings = new ProcessingSettings(); | ||
processingSettings.setOutputFormat( ProcessingSettings.OutputFormat.txt ); | ||
processingSettings.setLanguage(language); | ||
|
||
publishProgress("Uploading.."); | ||
|
||
// If you want to process business cards, uncomment this | ||
/* | ||
BusCardSettings busCardSettings = new BusCardSettings(); | ||
busCardSettings.setLanguage(language); | ||
busCardSettings.setOutputFormat(BusCardSettings.OutputFormat.xml); | ||
Task task = restClient.processBusinessCard(filePath, busCardSettings); | ||
*/ | ||
Task task = restClient.processImage(inputFile, processingSettings); | ||
|
||
while( task.isTaskActive() ) { | ||
Thread.sleep(2000); | ||
|
||
publishProgress( "Waiting.." ); | ||
task = restClient.getTaskStatus(task.Id); | ||
} | ||
|
||
if( task.Status == Task.TaskStatus.Completed ) { | ||
publishProgress( "Downloading.." ); | ||
FileOutputStream fos = activity.openFileOutput(outputFile,Context.MODE_PRIVATE); | ||
|
||
restClient.downloadResult(task, fos); | ||
|
||
fos.close(); | ||
|
||
publishProgress( "Ready" ); | ||
} else if( task.Status == Task.TaskStatus.NotEnoughCredits ) { | ||
publishProgress( "Not enough credits to process task. Add more pages to your application's account." ); | ||
} else { | ||
publishProgress( "Task failed" ); | ||
} | ||
|
||
return true; | ||
} catch (Exception e) { | ||
publishProgress( "Error: " + e.getMessage()); | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
protected void onProgressUpdate(String... values) { | ||
// TODO Auto-generated method stub | ||
String stage = values[0]; | ||
dialog.setMessage(stage); | ||
// dialog.setProgress(values[0]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package abbyy.ocrsdk.android; | ||
|
||
import android.os.Bundle; | ||
import android.app.Activity; | ||
import android.view.Menu; | ||
|
||
import android.net.*; | ||
import java.io.*; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
import android.os.Bundle; | ||
import android.os.Environment; | ||
import android.provider.MediaStore; | ||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.database.Cursor; | ||
import android.view.Menu; | ||
import android.view.View; | ||
|
||
public class MainActivity extends Activity { | ||
|
||
private final int TAKE_PICTURE = 0; | ||
private final int SELECT_FILE = 1; | ||
|
||
private String resultUrl = "result.txt"; | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
getMenuInflater().inflate(R.menu.activity_main, menu); | ||
return true; | ||
} | ||
|
||
|
||
public void captureImageFromSdCard( View view ) { | ||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); | ||
intent.setType("image/*"); | ||
|
||
startActivityForResult(intent, SELECT_FILE); | ||
} | ||
|
||
public static final int MEDIA_TYPE_IMAGE = 1; | ||
|
||
private static Uri getOutputMediaFileUri(){ | ||
return Uri.fromFile(getOutputMediaFile()); | ||
} | ||
|
||
/** Create a File for saving an image or video */ | ||
private static File getOutputMediaFile(){ | ||
// To be safe, you should check that the SDCard is mounted | ||
// using Environment.getExternalStorageState() before doing this. | ||
|
||
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( | ||
Environment.DIRECTORY_PICTURES), "ABBYY Cloud OCR SDK Demo App"); | ||
// This location works best if you want the created images to be shared | ||
// between applications and persist after your app has been uninstalled. | ||
|
||
// Create the storage directory if it does not exist | ||
if (! mediaStorageDir.exists()){ | ||
if (! mediaStorageDir.mkdirs()){ | ||
return null; | ||
} | ||
} | ||
|
||
// Create a media file name | ||
File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "image.jpg" ); | ||
|
||
return mediaFile; | ||
} | ||
|
||
public void captureImageFromCamera( View view) { | ||
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); | ||
Uri fileUri = getOutputMediaFileUri(); // create a file to save the image | ||
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name | ||
|
||
startActivityForResult(intent, TAKE_PICTURE); | ||
} | ||
|
||
@Override | ||
public void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
super.onActivityResult(requestCode, resultCode, data); | ||
if (resultCode != Activity.RESULT_OK) | ||
return; | ||
|
||
String imageFilePath = null; | ||
|
||
switch (requestCode) { | ||
case TAKE_PICTURE: | ||
imageFilePath = getOutputMediaFileUri().getPath(); | ||
break; | ||
case SELECT_FILE: { | ||
Uri imageUri = data.getData(); | ||
|
||
String[] projection = { MediaStore.Images.Media.DATA }; | ||
Cursor cur = managedQuery(imageUri, projection, null, null, null); | ||
cur.moveToFirst(); | ||
imageFilePath = cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA)); | ||
} | ||
break; | ||
} | ||
|
||
//Remove output file | ||
deleteFile(resultUrl); | ||
|
||
Intent results = new Intent( this, ResultsActivity.class); | ||
results.putExtra("IMAGE_PATH", imageFilePath); | ||
results.putExtra("RESULT_PATH", resultUrl); | ||
startActivity(results); | ||
} | ||
|
||
} |
Oops, something went wrong.