seriud89
30-01-2013, 10:43
Ciao a tutti! Sto sviluppando una app che utilizza il webservice Microsoft Hawaii per l'estrazione del testo da un'immagine!
Dopo che scatto una foto e cerco di inviarla al web service, l'app crasha, portandomi questo errore nel logcat:
01-30 11:34:35.390: E/AndroidRuntime(11993): FATAL EXCEPTION: main
01-30 11:34:35.390: E/AndroidRuntime(11993): java.lang.NoClassDefFoundError: your.Mobile.RecognitionActivity
01-30 11:34:35.390: E/AndroidRuntime(11993): at your.Mobile.FotocameraActivity.onActivityResult(FotocameraActivity.java:135)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.Activity.dispatchActivityResult(Activity.java:4820)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3027)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.ActivityThread.access$1100(ActivityThread.java:127)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1181)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.os.Looper.loop(Looper.java:137)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.ActivityThread.main(ActivityThread.java:4476)
01-30 11:34:35.390: E/AndroidRuntime(11993): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 11:34:35.390: E/AndroidRuntime(11993): at java.lang.reflect.Method.invoke(Method.java:511)
01-30 11:34:35.390: E/AndroidRuntime(11993): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
01-30 11:34:35.390: E/AndroidRuntime(11993): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
01-30 11:34:35.390: E/AndroidRuntime(11993): at dalvik.system.NativeStart.main(Native Method)
L'errore si riferisce a questa Activity:
public class FotocameraActivity extends Activity {
// dati che servono per la fotocamera
private static final int CAMERA_REQUEST = 100; // un numero a nostro
// piacimento
File tmpFotoFile = null;
byte[] bitmapdata;
ImageView preview;
LocationManager locationManager;
String gps;
final static String ARRAY_BYTE = "ARRAY_BYTE";
final static String GPS = "GPS";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
startGpsTracking();
}
try {
launchCamera();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void startGpsTracking() {
// TODO Auto-generated method stub
locationManager.addGpsStatusListener(gpsListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 3, myLocationListener);
}
private Listener gpsListener = new Listener(){
public void onGpsStatusChanged(int status){
switch(status){
case GpsStatus.GPS_EVENT_FIRST_FIX:
Log.d(LOCATION_SERVICE,"onGpsStatusChanged First Fix");
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
Log.d(LOCATION_SERVICE, "onGpsStatusChanged Satellite");
break;
case GpsStatus.GPS_EVENT_STARTED:
Log.d(LOCATION_SERVICE, "onGpsStatusChanged Started");
break;
case GpsStatus.GPS_EVENT_STOPPED:
Log.d(LOCATION_SERVICE, "onGpsStatusChanged Stopped");
break;
}
}
};
private LocationListener myLocationListener = new LocationListener(){
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lng = location.getLongitude(), lat = location.getLatitude();
gps += String.format("%6f", lng) + "#" + String.format("%6f", lat);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
private void launchCamera() throws IOException {
// Fase 1
tmpFotoFile = File.createTempFile("OCRPic", null);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tmpFotoFile));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap datifoto = null;
try {
datifoto = android.provider.MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(tmpFotoFile));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
datifoto.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bitmapdata = bos.toByteArray();
tmpFotoFile.delete();
SimpleView view = new SimpleView(this, datifoto); // creo l'istanza
// // della // view...
setContentView(view); // e la setto
Intent intentRecognize = new Intent(this, RecognitionActivity.class); <-------A QUESTA RIGA SI RIFERISCE L'ERRORE
intentRecognize.putExtra(ARRAY_BYTE, bitmapdata);
intentRecognize.putExtra(GPS, gps);
startActivity(intentRecognize);
}
}
}
class SimpleView extends View{
private Bitmap bitmap;
private Paint tmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public SimpleView(Context context, Bitmap bitmap) {
super(context);
this.bitmap = Bitmap.createScaledBitmap(bitmap,480,320,false);//ridimensiono l'immagine
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawBitmap( bitmap, null , new Rect(0,0,getWidth(),getHeight()),tmpPaint);
}
}
Ma l'activity RecognitionActivity è presente nel mio progetto ed è la seguente:
public class RecognitionActivity extends HawaiiBaseAuthActivity {
private Bundle extras;
private LinearLayout resultContainer;
private TextView ocrResultView;
private AsyncTask<Void, Integer, AlertDialog.Builder> currentOcrTask;
@SuppressWarnings("unused")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recognition);
final RecognitionActivity thisActivity = this;
extras = getIntent().getExtras();
byte [] bitmapArray = extras.getByteArray(FotocameraActivity.ARRAY_BYTE);
TextView tv = new TextView(thisActivity);
tv.setText("Questa è una prova");
tv.setVisibility(View.VISIBLE);
class OcrTask extends AsyncTask<Void, Integer, AlertDialog.Builder> {
private OcrServiceResult serviceResult;
protected void onPreExecute(){
TextView tv = new TextView(thisActivity);
tv.setText("Sto elaborando l'immagine...");
tv.setVisibility(View.VISIBLE);
ProgressBar progress = new ProgressBar(thisActivity);
progress.setVisibility(View.VISIBLE);
}
@Override
protected AlertDialog.Builder doInBackground(Void... params) {
// TODO Auto-generated method stub
extras = getIntent().getExtras();
byte [] bitmapArray = extras.getByteArray(FotocameraActivity.ARRAY_BYTE);
try {
OcrService.recognizeImage(thisActivity.getBaseApplication().getClientIdentity(), bitmapArray,
new OnCompleteListener<OcrServiceResult>() {
public void done(OcrServiceResult result){
serviceResult = result;}}, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(AlertDialog.Builder dialogBuilder) {
if (dialogBuilder != null) {
thisActivity.showErrorMessage(dialogBuilder);
} else {
resultContainer.setVisibility(View.VISIBLE);
List<OcrText> resultList = serviceResult.getOcrTexts();
if (resultList.size() > 0) {
String ocrResult = resultList.get(0).getText();
if (!Utility.isStringNullOrEmpty(ocrResult)) {
ocrResultView.setText(ocrResult);
} else {
thisActivity.showErrorMessage("Couldn't recognize the specified image",null);
}
}
}
}
}
currentOcrTask = new OcrTask();
currentOcrTask.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_recognition, menu);
return true;
}
}
Tuttavia mi sono accorto che se metto tra commenti la classe interna, sembra funzionare!
Vi chiedo gentilmente di aiutarmi, perchè questo è il mio lavoro di tesi e sono da molto tempo bloccato a questo punto senza riuscire ad andare avanti!:muro:
Grazie mille davvero.
Sergio
Dopo che scatto una foto e cerco di inviarla al web service, l'app crasha, portandomi questo errore nel logcat:
01-30 11:34:35.390: E/AndroidRuntime(11993): FATAL EXCEPTION: main
01-30 11:34:35.390: E/AndroidRuntime(11993): java.lang.NoClassDefFoundError: your.Mobile.RecognitionActivity
01-30 11:34:35.390: E/AndroidRuntime(11993): at your.Mobile.FotocameraActivity.onActivityResult(FotocameraActivity.java:135)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.Activity.dispatchActivityResult(Activity.java:4820)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3027)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.ActivityThread.access$1100(ActivityThread.java:127)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1181)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.os.Looper.loop(Looper.java:137)
01-30 11:34:35.390: E/AndroidRuntime(11993): at android.app.ActivityThread.main(ActivityThread.java:4476)
01-30 11:34:35.390: E/AndroidRuntime(11993): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 11:34:35.390: E/AndroidRuntime(11993): at java.lang.reflect.Method.invoke(Method.java:511)
01-30 11:34:35.390: E/AndroidRuntime(11993): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
01-30 11:34:35.390: E/AndroidRuntime(11993): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
01-30 11:34:35.390: E/AndroidRuntime(11993): at dalvik.system.NativeStart.main(Native Method)
L'errore si riferisce a questa Activity:
public class FotocameraActivity extends Activity {
// dati che servono per la fotocamera
private static final int CAMERA_REQUEST = 100; // un numero a nostro
// piacimento
File tmpFotoFile = null;
byte[] bitmapdata;
ImageView preview;
LocationManager locationManager;
String gps;
final static String ARRAY_BYTE = "ARRAY_BYTE";
final static String GPS = "GPS";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
startGpsTracking();
}
try {
launchCamera();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void startGpsTracking() {
// TODO Auto-generated method stub
locationManager.addGpsStatusListener(gpsListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 3, myLocationListener);
}
private Listener gpsListener = new Listener(){
public void onGpsStatusChanged(int status){
switch(status){
case GpsStatus.GPS_EVENT_FIRST_FIX:
Log.d(LOCATION_SERVICE,"onGpsStatusChanged First Fix");
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
Log.d(LOCATION_SERVICE, "onGpsStatusChanged Satellite");
break;
case GpsStatus.GPS_EVENT_STARTED:
Log.d(LOCATION_SERVICE, "onGpsStatusChanged Started");
break;
case GpsStatus.GPS_EVENT_STOPPED:
Log.d(LOCATION_SERVICE, "onGpsStatusChanged Stopped");
break;
}
}
};
private LocationListener myLocationListener = new LocationListener(){
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lng = location.getLongitude(), lat = location.getLatitude();
gps += String.format("%6f", lng) + "#" + String.format("%6f", lat);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
private void launchCamera() throws IOException {
// Fase 1
tmpFotoFile = File.createTempFile("OCRPic", null);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tmpFotoFile));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap datifoto = null;
try {
datifoto = android.provider.MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(tmpFotoFile));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
datifoto.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bitmapdata = bos.toByteArray();
tmpFotoFile.delete();
SimpleView view = new SimpleView(this, datifoto); // creo l'istanza
// // della // view...
setContentView(view); // e la setto
Intent intentRecognize = new Intent(this, RecognitionActivity.class); <-------A QUESTA RIGA SI RIFERISCE L'ERRORE
intentRecognize.putExtra(ARRAY_BYTE, bitmapdata);
intentRecognize.putExtra(GPS, gps);
startActivity(intentRecognize);
}
}
}
class SimpleView extends View{
private Bitmap bitmap;
private Paint tmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public SimpleView(Context context, Bitmap bitmap) {
super(context);
this.bitmap = Bitmap.createScaledBitmap(bitmap,480,320,false);//ridimensiono l'immagine
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawBitmap( bitmap, null , new Rect(0,0,getWidth(),getHeight()),tmpPaint);
}
}
Ma l'activity RecognitionActivity è presente nel mio progetto ed è la seguente:
public class RecognitionActivity extends HawaiiBaseAuthActivity {
private Bundle extras;
private LinearLayout resultContainer;
private TextView ocrResultView;
private AsyncTask<Void, Integer, AlertDialog.Builder> currentOcrTask;
@SuppressWarnings("unused")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recognition);
final RecognitionActivity thisActivity = this;
extras = getIntent().getExtras();
byte [] bitmapArray = extras.getByteArray(FotocameraActivity.ARRAY_BYTE);
TextView tv = new TextView(thisActivity);
tv.setText("Questa è una prova");
tv.setVisibility(View.VISIBLE);
class OcrTask extends AsyncTask<Void, Integer, AlertDialog.Builder> {
private OcrServiceResult serviceResult;
protected void onPreExecute(){
TextView tv = new TextView(thisActivity);
tv.setText("Sto elaborando l'immagine...");
tv.setVisibility(View.VISIBLE);
ProgressBar progress = new ProgressBar(thisActivity);
progress.setVisibility(View.VISIBLE);
}
@Override
protected AlertDialog.Builder doInBackground(Void... params) {
// TODO Auto-generated method stub
extras = getIntent().getExtras();
byte [] bitmapArray = extras.getByteArray(FotocameraActivity.ARRAY_BYTE);
try {
OcrService.recognizeImage(thisActivity.getBaseApplication().getClientIdentity(), bitmapArray,
new OnCompleteListener<OcrServiceResult>() {
public void done(OcrServiceResult result){
serviceResult = result;}}, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(AlertDialog.Builder dialogBuilder) {
if (dialogBuilder != null) {
thisActivity.showErrorMessage(dialogBuilder);
} else {
resultContainer.setVisibility(View.VISIBLE);
List<OcrText> resultList = serviceResult.getOcrTexts();
if (resultList.size() > 0) {
String ocrResult = resultList.get(0).getText();
if (!Utility.isStringNullOrEmpty(ocrResult)) {
ocrResultView.setText(ocrResult);
} else {
thisActivity.showErrorMessage("Couldn't recognize the specified image",null);
}
}
}
}
}
currentOcrTask = new OcrTask();
currentOcrTask.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_recognition, menu);
return true;
}
}
Tuttavia mi sono accorto che se metto tra commenti la classe interna, sembra funzionare!
Vi chiedo gentilmente di aiutarmi, perchè questo è il mio lavoro di tesi e sono da molto tempo bloccato a questo punto senza riuscire ad andare avanti!:muro:
Grazie mille davvero.
Sergio