我有这个代码按钮点击它在Android模拟器nexus 5中正常工作,但当我在Android手机中安装它并点击扫描按钮我得到错误不幸的是应用已经停止。
谁能帮助我?Bundle extras = getIntent().getExtras();
String imgPath = extras.getString("picPath");
Bitmap bmp = decodeBitmapFile(imgPath, 1200, 1500);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanText();
}
});
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeBitmapFile(String filePath,int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);}
执行OCR的代码
public void scanText(){
try {
ExifInterface exif = new ExifInterface(imgPath);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Log.v(TAG, "Orient: " + exifOrientation);
int rotate = 0;
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}
if (rotate != 0) {
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(rotate);
// Rotating Bitmap
bmp = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, false);
}
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);
} catch (IOException e) {
Log.e(TAG, "Couldn't correct orientation: " + e.toString());
}
Log.v(TAG, "Before baseApi");
TessBaseAPI baseApi = new TessBaseAPI();
baseApi.setDebug(true);
baseApi.init(DATA_PATH, lang);
baseApi.setImage(bmp);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();
Log.v(TAG, "Scanned TEXT: " + recognizedText);
if (lang.equalsIgnoreCase("eng")) {
recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", "");
}
recognizedText = recognizedText.trim();
if (recognizedText.length() != 0) {
Intent x = new Intent(Main2Activity.this, Main3Activity.class);
x.putExtra("txtScan", recognizedText);
x.putExtra("pic", imgPath);
startActivity(x);
finish();
} else {
Toast.makeText(Main2Activity.this, "Error while extracting text..", Toast.LENGTH_LONG)
.show();
}
}
这是LOGCAT中的错误
09-08 11:36:36.424 7588-7588/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.scanme.scanme, PID: 7588
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "png_set_longjmp_fn" referenced by "liblept.so"...
at java.lang.Runtime.loadLibrary(Runtime.java:364)
at java.lang.System.loadLibrary(System.java:526)
at com.googlecode.tesseract.android.TessBaseAPI.<clinit>(TessBaseAPI.java:44)
at com.scanme.scanme.Main2Activity.scanText(Main2Activity.java:131)
at com.scanme.scanme.Main2Activity$2.onClick(Main2Activity.java:94)
at android.view.View.performClick(View.java:4493)
at android.view.View$PerformClick.run(View.java:18586)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)