我正在创建一个 Android 应用程序,该应用程序使用 Microsoft Hawaii SDK 来执行用手机相机拍摄的图片的 OCR。在执行我的 Android 应用程序期间出现错误。日志猫是这样的:
02-07 14:48:59.617: E/AndroidRuntime(24660): FATAL EXCEPTION: main
02-07 14:48:59.617: E/AndroidRuntime(24660): java.lang.NoClassDefFoundError: com.example.mobile.RecognitionActivity
02-07 14:48:59.617: E/AndroidRuntime(24660): at com.example.mobile.FotocameraActivity.onActivityResult(FotocameraActivity.java:143)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.Activity.dispatchActivityResult(Activity.java:4820)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2435)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2476)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1990)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3355)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.access$700(ActivityThread.java:127)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1155)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.os.Looper.loop(Looper.java:137)
02-07 14:48:59.617: E/AndroidRuntime(24660): at android.app.ActivityThread.main(ActivityThread.java:4476)
02-07 14:48:59.617: E/AndroidRuntime(24660): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 14:48:59.617: E/AndroidRuntime(24660): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 14:48:59.617: E/AndroidRuntime(24660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
02-07 14:48:59.617: E/AndroidRuntime(24660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
02-07 14:48:59.617: E/AndroidRuntime(24660): at dalvik.system.NativeStart.main(Native Method)
FotocameraActivity.Java 的代码是这样的:
package com.example.mobile;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.location.GpsStatus;
import android.location.GpsStatus.Listener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
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, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //Uri.fromFile(tmpFotoFile));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap datifoto = null;
Uri picUri = data.getData();
if(picUri != null){
try {
datifoto = android.provider.MediaStore.Images.Media.getBitmap(this.getContentResolver(), picUri);
//datifoto = android.provider.MediaStore.Images.Media.getBitmap(this.getContentResolver(), android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//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, 70, 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); **<-Here refers the logcat for the error**
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
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawBitmap( bitmap, null , new Rect(0,0,getWidth(),getHeight()),tmpPaint);
}
}
而 RecognitionActivity.Java 的代码是这样的:
package com.example.mobile;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import microsoft.hawaii.sampleappbase.HawaiiBaseAuthActivity;
public class RecognitionActivity extends HawaiiBaseAuthActivity {
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recognition);
tv.setText("Prova");
tv.setVisibility(View.VISIBLE);
}
@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;
}
}
我注意到,在 RecognitionActivity 中,如果我从 Activity 扩展该活动,它可以工作,但如果我扩展 HawaiiBaseAuthActivity,它会崩溃。
我注意到 logcat 中的另一条消息,它出现在应用程序启动的那一刻:
02-07 14:48:14.717: E/dalvikvm(24660): Could not find class 'com.example.mobile.RecognitionActivity', referenced from method com.example.mobile.FotocameraActivity.onActivityResult
但是该活动是在我的工作区中定义的!
如果有人可以帮助我,我将不胜感激。