2013-05-05 48 views
1

我想我在做我的路径有问题,但我找不出什么。如果这是相关的,我正在测试Nexus 7。以下示例试图将tessdata文件夹复制到SD卡上,但它一直告诉我无法复制未找到的文件。我不太明白为什么。 (我有tesseract项目作为库,我有tessdata文件夹复制到资产)。 所有帮助表示感谢!Tesseract无法将数据复制到SD?

代码:

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.res.AssetManager; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Matrix; 
import android.media.ExifInterface; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.Toast; 

import com.googlecode.tesseract.android.TessBaseAPI; 

public class MainActivity extends Activity { 
     private static ImageView imageView; 
     // protected static Bitmap bit; 
     protected static Bitmap mImageBitmap; 
     // protected static String DATA_PATH; 
     public static final String STORAGE_PATH = Environment.getExternalStorageDirectory().toString() + "/rjb"; 
     protected static String tesspath = STORAGE_PATH + "/tessdata"; 
     protected static String savepath = null; 
     protected static String TAG = "OCR"; 
     protected static String lang = "eng"; 

     // main method 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 

       // This is my image view for to show the image with 
       imageView = (ImageView) this.findViewById(R.id.imageView1); 
       // this is the take a photo button 
       Button photoButton = (Button) this.findViewById(R.id.button1); 

       //check if the rjb directory is there 
       //make it if its not 
       createmydir(); 

       //if (!(new File(STORAGE_PATH + File.separator + "tessdata" + File.separator + lang + ".traineddata")).exists()) { 
         try { 
           AssetManager assetManager = this.getAssets(); 
           //open the asset manager and open the traineddata path 
           InputStream in = assetManager.open("tessdata/eng.traineddata"); 
           OutputStream out = new FileOutputStream(tesspath + "/eng.traineddata"); 
           byte[] buf = new byte[8024]; 
           int len; 
           while ((len = in.read(buf)) > 0) { 
             out.write(buf, 0, len); 
           } 
           in.close(); 
           out.close(); 
         } catch (IOException e) { 
           android.util.Log.e(TAG, "Was unable to copy " + lang 
               + " traineddata " + e.toString()); 
           android.util.Log.e(TAG, "IM PRINTING THE STACK TRACE"); 
           e.printStackTrace(); 
         } 
       //} else { 
         processImage(STORAGE_PATH + File.separator + "savedAndroid.jpg"); 
       //} 
       photoButton.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
           // CALL THE PICTURE (this works) 
           dispatchTakePictureIntent(0); 
         } 
       }); 
     } 

     private void createmydir() { 
       File t = new File(STORAGE_PATH); 

       if(t.exists()) { 
         Toast.makeText(getApplicationContext(), "IM TOASTIN CAUSE IT EXISTS", Toast.LENGTH_LONG).show(); 
       } 
       else { 

         t.mkdirs(); 
         Toast.makeText(getApplicationContext(), "IM TOASTIN CUZ I MADE IT EXIST", Toast.LENGTH_LONG).show(); 
       } 
     } 

     private void handleSmallCameraPhoto(Intent intent) { 
       Bundle extras = intent.getExtras(); 
       mImageBitmap = (Bitmap) extras.get("data"); 
       imageView.setImageBitmap(mImageBitmap); 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 4; 

       saveImageAndroid(mImageBitmap); 

       Bitmap bitmap = BitmapFactory.decodeFile(savepath, options); 
       ExifInterface exif; 
       try { 
         exif = new ExifInterface(savepath); 
         int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL); 
         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) { 
           int w = bitmap.getWidth(); 
           int h = bitmap.getHeight(); 
           // Setting pre rotate 
           Matrix mtx = new Matrix(); 
           mtx.preRotate(rotate); 
           // Rotating Bitmap & convert to ARGB_8888, required by tess 
           bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false); 
           bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 
         } 



         // DATA_PATH = getDataPath(); 

       } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
       } 
     } 

     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       handleSmallCameraPhoto(data); 
     } 

     // write bitmap to storage 
     // saves it as savedandroid.jpg 
     // saves location in savepath 
     private void saveImageAndroid(final Bitmap passedBitmap) { 
       try { 
         savepath = STORAGE_PATH + File.separator + "savedAndroid.jpg"; 
         FileOutputStream mFileOutStream = new FileOutputStream(savepath); 
         passedBitmap.compress(Bitmap.CompressFormat.JPEG, 100,mFileOutStream); 
         mFileOutStream.flush(); 
         mFileOutStream.close(); 
       } catch (Exception e) { 
         e.printStackTrace(); 
       } 
     } 

     private void dispatchTakePictureIntent(int actionCode) { 
       Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(takePictureIntent, actionCode); 
     } 

     private void processImage(final String filePath) { 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 2; 
       options.inPurgeable = true; 
       Bitmap bitmap = BitmapFactory.decodeFile(filePath, options); 
       if (bitmap != null) { 
         /* 
         * was for rotating but no longer needed int width = 
         * bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix 
         * = new Matrix(); matrix.postRotate(rotation); bitmap = 
         * Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); 
         * bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 
         */ 

         TessBaseAPI baseApi = new TessBaseAPI(); 
         baseApi.setDebug(true); 
         baseApi.init(STORAGE_PATH, "eng"); 
         baseApi.setPageSegMode(100); 
         baseApi.setPageSegMode(7); 
         baseApi.setImage(bitmap); 
         String recognizedText = baseApi.getUTF8Text(); 
         android.util.Log.i(TAG, "recognizedText: 1 " + recognizedText); 
         baseApi.end(); 
         if (lang.equalsIgnoreCase("eng")) { 
           recognizedText = recognizedText 
               .replaceAll("[^a-zA-Z0-9]+", " "); 
         } 
         android.util.Log.i(TAG, 
             "recognizedText: 2 " + recognizedText.trim()); 
       } 
     } 
} 

回答

0

首先尝试把SD卡上的文件,只是尝试像耳朵的简单的图像简单的OCR,如果工作,然后去通过程序复制语言文件,因为这你可能会知道错误在哪里。