裁剪后的图像不适用于 tess-two
Cropped image not working with tess-two
我正在创建一个可以通过相机捕获图像并可以使用 android.
的 tess-two 库提取图像中的文本的应用程序
在我添加裁剪图像的选项之前,代码工作正常。裁剪图像后,错误发生在
行
tessbaseAPI.setImage(bitmap)
日志说
Failed to read bitmap
下面是我裁剪图片的代码
private void performCrop() {
// take care of exceptions
try {
Log.v(TAG, "Inside try of performCrop");
// call the standard crop action intent (the user device may not
// support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
Log.v(TAG, "Going to onActivityResult now");
startActivityForResult(cropIntent, CROP_PIC);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
Toast toast = Toast
.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
toast.show();
}
}
并用于执行 OCR
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
Log.v(TAG, "Request Code is REQUEST_IMAGE_CAPTURE");
picUri = data.getData();
performCrop();
} else if (requestCode == CROP_PIC) {
Log.v(TAG, "Request Code is CROP_PIC");
Bundle extras = data.getExtras();
Bitmap bitmap = extras.getParcelable("data");
TextView res = (TextView) findViewById(R.id.hello);
//imageView.setImageBitmap(imageBitmap);
//Image image = ImageIO.read(imageFile);
//BufferedImage buffimg = (BufferedImage) image;
//BufferedImage img = ImageHelper.convertImageToGrayscale(buffimg);
//ITesseract instance = new Tesseract(); // JNA Interface Mapping
//ITesseract instance = new Tesseract1(); // JNA Direct Mapping
//TessDataManager.initTessTrainedData(context);
if (isStoragePermissionGranted() == true) {
TessBaseAPI tessBaseAPI = new TessBaseAPI();
String path = Environment.getExternalStorageDirectory() + "/";
//String path = "/mnt/sdcard/";
tessBaseAPI.setDebug(true);
tessBaseAPI.init(path, "eng");
tessBaseAPI.setImage(bitmap);
String text = tessBaseAPI.getUTF8Text();
tessBaseAPI.end();
res.setText(text);
}
}} else {
TextView res = (TextView) findViewById(R.id.hello);
res.setText("Well damn");
}
}
}
日志中的一行
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.abc.snaptravel/com.example.abc.snaptravel.MainActivity}: java.lang.RuntimeException: Failed to read bitmap
如有任何帮助,我将不胜感激。谢谢!
好吧,出现了很多问题。总的来说,我认为主要问题是我无法访问我的 SD 卡。我在某处读到 HTC 型号很难访问 SD 卡,而我只使用 HTC 型号。
我通过将裁剪后的图像保存在内存中然后将该路径提供给 tessbaseAPI.setImage(File file)
解决了这个问题
裁剪函数变成了这个 -
private void performCrop() {
// take care of exceptions
try {
Log.v(TAG, "Inside try of performCrop");
// call the standard crop action intent (the user device may not
// support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output=new File(dir, "CameraContentDemo.jpg");
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
// retrieve data on return
cropIntent.putExtra("return-data", false);
// start the activity - we handle returning in onActivityResult
Log.v(TAG, "Going to onActivityResult now");
startActivityForResult(cropIntent, CROP_PIC);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
Toast toast = Toast
.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
toast.show();
}
}
并且 onActivityResult() 变成了这个 -
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
Log.v(TAG, "Request Code is REQUEST_IMAGE_CAPTURE");
picUri = data.getData();
performCrop();
} else if (requestCode == CROP_PIC) {
Log.v(TAG, "Request Code is CROP_PIC");
Bundle extras = data.getExtras();
//Bitmap bitmap = (Bitmap)extras.get("data");
TextView res = (TextView) findViewById(R.id.hello);
ImageView im = (ImageView)findViewById(R.id.imageView);
//im.setImageBitmap(bitmap);
//imageView.setImageBitmap(imageBitmap);
//Image image = ImageIO.read(imageFile);
//BufferedImage buffimg = (BufferedImage) image;
//BufferedImage img = ImageHelper.convertImageToGrayscale(buffimg);
//ITesseract instance = new Tesseract(); // JNA Interface Mapping
//ITesseract instance = new Tesseract1(); // JNA Direct Mapping
//TessDataManager.initTessTrainedData(context);
if (isStoragePermissionGranted() == true) {
TessBaseAPI tessBaseAPI = new TessBaseAPI();
String path = Environment.getExternalStorageDirectory() + "/";
//String path = "/mnt/sdcard/";
tessBaseAPI.setDebug(true);
tessBaseAPI.init(path, "eng");
File file = new File(picUri.getPath());
tessBaseAPI.setImage(output);
String text = tessBaseAPI.getUTF8Text();
tessBaseAPI.end();
res.setText(text);
}
}} else {
TextView res = (TextView) findViewById(R.id.hello);
res.setText("Well damn");
}
}
}
请注意,我已经这样做了 -
cropIntent.putExtra("return-data", false);
当我们提供存储它的文件时,我们不需要来自此函数的数据。
我正在创建一个可以通过相机捕获图像并可以使用 android.
的 tess-two 库提取图像中的文本的应用程序在我添加裁剪图像的选项之前,代码工作正常。裁剪图像后,错误发生在
行tessbaseAPI.setImage(bitmap)
日志说
Failed to read bitmap
下面是我裁剪图片的代码
private void performCrop() {
// take care of exceptions
try {
Log.v(TAG, "Inside try of performCrop");
// call the standard crop action intent (the user device may not
// support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
Log.v(TAG, "Going to onActivityResult now");
startActivityForResult(cropIntent, CROP_PIC);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
Toast toast = Toast
.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
toast.show();
}
}
并用于执行 OCR
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
Log.v(TAG, "Request Code is REQUEST_IMAGE_CAPTURE");
picUri = data.getData();
performCrop();
} else if (requestCode == CROP_PIC) {
Log.v(TAG, "Request Code is CROP_PIC");
Bundle extras = data.getExtras();
Bitmap bitmap = extras.getParcelable("data");
TextView res = (TextView) findViewById(R.id.hello);
//imageView.setImageBitmap(imageBitmap);
//Image image = ImageIO.read(imageFile);
//BufferedImage buffimg = (BufferedImage) image;
//BufferedImage img = ImageHelper.convertImageToGrayscale(buffimg);
//ITesseract instance = new Tesseract(); // JNA Interface Mapping
//ITesseract instance = new Tesseract1(); // JNA Direct Mapping
//TessDataManager.initTessTrainedData(context);
if (isStoragePermissionGranted() == true) {
TessBaseAPI tessBaseAPI = new TessBaseAPI();
String path = Environment.getExternalStorageDirectory() + "/";
//String path = "/mnt/sdcard/";
tessBaseAPI.setDebug(true);
tessBaseAPI.init(path, "eng");
tessBaseAPI.setImage(bitmap);
String text = tessBaseAPI.getUTF8Text();
tessBaseAPI.end();
res.setText(text);
}
}} else {
TextView res = (TextView) findViewById(R.id.hello);
res.setText("Well damn");
}
}
}
日志中的一行
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.abc.snaptravel/com.example.abc.snaptravel.MainActivity}: java.lang.RuntimeException: Failed to read bitmap
如有任何帮助,我将不胜感激。谢谢!
好吧,出现了很多问题。总的来说,我认为主要问题是我无法访问我的 SD 卡。我在某处读到 HTC 型号很难访问 SD 卡,而我只使用 HTC 型号。
我通过将裁剪后的图像保存在内存中然后将该路径提供给 tessbaseAPI.setImage(File file)
裁剪函数变成了这个 -
private void performCrop() {
// take care of exceptions
try {
Log.v(TAG, "Inside try of performCrop");
// call the standard crop action intent (the user device may not
// support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output=new File(dir, "CameraContentDemo.jpg");
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
// retrieve data on return
cropIntent.putExtra("return-data", false);
// start the activity - we handle returning in onActivityResult
Log.v(TAG, "Going to onActivityResult now");
startActivityForResult(cropIntent, CROP_PIC);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
Toast toast = Toast
.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
toast.show();
}
}
并且 onActivityResult() 变成了这个 -
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
Log.v(TAG, "Request Code is REQUEST_IMAGE_CAPTURE");
picUri = data.getData();
performCrop();
} else if (requestCode == CROP_PIC) {
Log.v(TAG, "Request Code is CROP_PIC");
Bundle extras = data.getExtras();
//Bitmap bitmap = (Bitmap)extras.get("data");
TextView res = (TextView) findViewById(R.id.hello);
ImageView im = (ImageView)findViewById(R.id.imageView);
//im.setImageBitmap(bitmap);
//imageView.setImageBitmap(imageBitmap);
//Image image = ImageIO.read(imageFile);
//BufferedImage buffimg = (BufferedImage) image;
//BufferedImage img = ImageHelper.convertImageToGrayscale(buffimg);
//ITesseract instance = new Tesseract(); // JNA Interface Mapping
//ITesseract instance = new Tesseract1(); // JNA Direct Mapping
//TessDataManager.initTessTrainedData(context);
if (isStoragePermissionGranted() == true) {
TessBaseAPI tessBaseAPI = new TessBaseAPI();
String path = Environment.getExternalStorageDirectory() + "/";
//String path = "/mnt/sdcard/";
tessBaseAPI.setDebug(true);
tessBaseAPI.init(path, "eng");
File file = new File(picUri.getPath());
tessBaseAPI.setImage(output);
String text = tessBaseAPI.getUTF8Text();
tessBaseAPI.end();
res.setText(text);
}
}} else {
TextView res = (TextView) findViewById(R.id.hello);
res.setText("Well damn");
}
}
}
请注意,我已经这样做了 -
cropIntent.putExtra("return-data", false);
当我们提供存储它的文件时,我们不需要来自此函数的数据。