I'm useing a library called scanlibrary to scan the photo and then pass it to tess-two to perform the OCR process. The problem is that the directory "ScanDemoExample" is not being created thus the tessdata files aren't copied and when I run my activity I get the error :
我正在使用一个名为scanlibrary的库来扫描照片,然后将其传递给tess-two来执行OCR过程。问题是没有创建目录“ScanDemoExample”,因此不会复制tessdata文件,当我运行我的活动时,我收到错误:
E/Tesseract(native): Could not initialize Tesseract API with language=eng!
because tesseract can't find the files in the data_path. The code works when I use an existing directory instead of the following :
因为tesseract无法在data_path中找到文件。当我使用现有目录而不是以下目录时,代码可以工作:
public static final String DATA_PATH = Environment
.getExternalStorageDirectory() +"/ScanDemoExample/";
Here's my main activity:
这是我的主要活动:
package com.scanner.demo;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.googlecode.tesseract.android.TessBaseAPI;
import com.scanlibrary.ScanActivity;
import com.scanlibrary.ScanConstants;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MainActivity extends ActionBarActivity {
public static final String PACKAGE_NAME = "com.scanner.demo";
public static final String DATA_PATH = Environment
.getExternalStorageDirectory() +"/ScanDemoExample/";
// You should have the trained data file in assets folder
// You can get them at:
// http://code.google.com/p/tesseract-ocr/downloads/list
public static final String lang = "eng";
private static final String TAG = "MainActivity.java";
// public static final String _path = DATA_PATH + "/ocr.jpg";
private static final int REQUEST_CODE = 99;
private Button scanButton;
private Button cameraButton;
private Button mediaButton;
private ImageView scannedImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };
for (String path : paths) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.e(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
return;
} else {
Log.v(TAG, "Created directory " + path + " on sdcard");
}
}
}
// lang.traineddata file with the app (in assets folder)
// You can get them at:
// http://code.google.com/p/tesseract-ocr/downloads/list
// This area needs work and optimization
if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
try {
AssetManager assetManager = getAssets();
InputStream in = assetManager.open("tessdata/" + lang + ".traineddata");
//GZIPInputStream gin = new GZIPInputStream(in);
OutputStream out = new FileOutputStream(DATA_PATH
+ "tessdata/" + lang + ".traineddata");
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
//while ((lenf = gin.read(buff)) > 0) {
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
//gin.close();
out.close();
Log.v(TAG, "Copied " + lang + " traineddata");
} catch (IOException e) {
Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
}
}
init();
}
private void init() {
scanButton = (Button) findViewById(R.id.scanButton);
scanButton.setOnClickListener(new ScanButtonClickListener());
cameraButton = (Button) findViewById(R.id.cameraButton);
cameraButton.setOnClickListener(new ScanButtonClickListener(ScanConstants.OPEN_CAMERA));
mediaButton = (Button) findViewById(R.id.mediaButton);
mediaButton.setOnClickListener(new ScanButtonClickListener(ScanConstants.OPEN_MEDIA));
scannedImageView = (ImageView) findViewById(R.id.scannedImage);
}
private class ScanButtonClickListener implements View.OnClickListener {
private int preference;
public ScanButtonClickListener(int preference) {
this.preference = preference;
}
public ScanButtonClickListener() {
}
@Override
public void onClick(View v) {
startScan(preference);
}
}
protected void startScan(int preference) {
Intent intent = new Intent(this, ScanActivity.class);
intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
getContentResolver().delete(uri, null, null);
scannedImageView.setImageBitmap(bitmap);
///////////////////////////////////////////////////////////
Log.v(TAG, "Before baseApi");
Log.v(TAG, "ExternalStorageDirectory: "+Environment
.getExternalStorageDirectory().toString());
Log.v(TAG, "DATA_PATH: "+DATA_PATH);
TessBaseAPI baseApi = new TessBaseAPI();
baseApi.setDebug(true);
baseApi.init(DATA_PATH, lang);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
baseApi.setImage(bitmap);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();
// You now have the text in recognizedText var, you can do anything with it.
// We will display a stripped out trimmed alpha-numeric version of it (if lang is eng)
// so that garbage doesn't make it to the display.
Log.v(TAG, "OCRED TEXT: " + recognizedText);
if ( lang.equalsIgnoreCase("eng") ) {
recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private Bitmap convertByteArrayToBitmap(byte[] data) {
return BitmapFactory.decodeByteArray(data, 0, data.length);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Edit: here's my logcat
编辑:这是我的logcat
I/art: Explicit concurrent mark sweep GC freed 2044(210KB) AllocSpace objects, 6(395KB) LOS objects, 40% free, 3MB/6MB, paused 242us total 9.669ms
V/MainActivity.java: Before baseApi
V/MainActivity.java: ExternalStorageDirectory: /storage/emulated/0
V/MainActivity.java: DATA_PATH: /storage/emulated/0/ScanDemoExample/
W/linker: libjpgt.so: unused DT entry: type 0x6ffffffe arg 0x2114
W/linker: libjpgt.so: unused DT entry: type 0x6fffffff arg 0x1
W/linker: libpngt.so: unused DT entry: type 0x6ffffffe arg 0x4a04
W/linker: libpngt.so: unused DT entry: type 0x6fffffff arg 0x2
W/linker: liblept.so: unused DT entry: type 0x6ffffffe arg 0x1dc90
W/linker: liblept.so: unused DT entry: type 0x6fffffff arg 0x2
W/linker: libtess.so: unused DT entry: type 0x6ffffffe arg 0x5d2e8
W/linker: libtess.so: unused DT entry: type 0x6fffffff arg 0x3
E/Tesseract(native): Could not initialize Tesseract API with language=eng!
A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x8 in tid 2863 (om.scanner.demo)
Application terminated.
1 个解决方案
#1
0
Have you tried to enable this permission?
您是否尝试启用此权限?
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
there is a nice example: Storage permission error in Marshmallow
有一个很好的例子:Marshmallow中的存储权限错误
#1
0
Have you tried to enable this permission?
您是否尝试启用此权限?
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
there is a nice example: Storage permission error in Marshmallow
有一个很好的例子:Marshmallow中的存储权限错误