2017-03-16 55 views
0

请原谅任何不好的英语,这是我第一次张贴在计算器的问题。母语:无法用语言初始化正方体API =工程

我想使用的Tesseract OCR引擎打造一个OCR Android应用程序,面对下面的错误,我试图寻找,但周围却没有找到任何解决办法,将不胜感激你的帮助。谢谢。

代码我想:

TessBaseAPI baseApi = new TessBaseAPI(); 
baseApi.init(Environment.getExternalStorageDirectory().toString()+"/", `"eng");` 

我已经在我的内eng.traineddata文件设备根目录创建一个文件夹tessdata,但是我提示以下错误,当我访问该功能。

无法使用language = eng初始化Tesseract API!

我使用的是Android 6.0.1,API 23

希望得到任何帮助!在此先感谢〜

+0

您是否有权限访问外部存储? – zsmb13

+0

嗨@ zsmb13,感谢您的回复,我让他们在manifest文件,但它不工作。原来,当我在java文件中插入权限代码时,它工作。这解决了我的问题! –

回答

1

试试这个代码了。它可以让你拍摄一张照片,并显示。还有在这个代码小错误。尝试在记事本中键入 忽略的各种文件字母的代码被放置在tessdata文件夹中的文本。我想读数学方程,因此我需要那些。我已经评论了其他文件,它不应该打扰你。如果您愿意尝试,请尝试使用Mobile Vision API。 希望这会有所帮助:)

public class MainActivity extends AppCompatActivity { 
    String imgPath; 
    Bitmap imgBitmap; 
    Uri imgUri; 
    InputStream trainDataInputStream; 
    OutputStream trainDataOutputStream; 
    AssetManager assetManager; 
    String externalDataPath; 
    TextView t; 
    String[] fileToBeCopied = {"eng.cube.bigrams", "eng.cube.fold", "eng.cube.lm", "eng.cube.nn", "eng.cube.params", "eng.cube.size", "eng.cube.word-freq", "eng.tesseract_cube.nn", "eng.traineddata","equ.traineddata"}; 
    ProgressDialog pDialog; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     t = (TextView) findViewById(R.id.text); 
     new CopyFile().execute(); 
     //placeFileFromAssetsToExternalStorage(); 
     takePicture(); 

    } 

    class CopyFile extends AsyncTask { 

     @Override 
     protected void onPreExecute() { 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Fetching image..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     @Override 
     protected Object doInBackground(Object[] objects) { 
      //placeFileFromAssetsToExternalStorage(fileToBeCopied[0]); 
      //placeFileFromAssetsToExternalStorage(fileToBeCopied[1]); 
      //placeFileFromAssetsToExternalStorage(fileToBeCopied[2]); 
      //placeFileFromAssetsToExternalStorage(fileToBeCopied[3]); 
      //placeFileFromAssetsToExternalStorage(fileToBeCopied[4]); 
      //placeFileFromAssetsToExternalStorage(fileToBeCopied[5]); 
      //placeFileFromAssetsToExternalStorage(fileToBeCopied[6]); 
      //placeFileFromAssetsToExternalStorage(fileToBeCopied[7]); 
      placeFileFromAssetsToExternalStorage(fileToBeCopied[8]); 
      //placeFileFromAssetsToExternalStorage(fileToBeCopied[9]); 

      return null; 
     } 


     @Override 
     protected void onPostExecute(Object o) { 
      pDialog.dismiss(); 

     } 
    } 

    private void takePicture() { 
     File photoFile = null; 
     Intent iPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (iPicture.resolveActivity(getPackageManager()) != null) { 

      try { 
       photoFile = createImageFile(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      //if photo file is created 
      if (photoFile != null) { 
       Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), "com.scorpio.fileprovider", photoFile); 
       System.out.println(imgPath); 
       iPicture.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
       startActivityForResult(iPicture, 1); 

      } 
     } 
    } 

    private File createImageFile() { 
     File imgFile = null; 
     String fileStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     File storageDir = Environment.getExternalStorageDirectory(); 
     try { 
      imgFile = File.createTempFile(fileStamp, ".jpeg", storageDir); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     imgPath = imgFile.getAbsolutePath(); 
     return imgFile; 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == 1 && resultCode == RESULT_OK) { 
      galleryAddPic(); 
     } 
    } 

    private void galleryAddPic() { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     File f = new File(imgPath); 
     System.out.println("Image path ->" + imgPath); 
     Uri contentUri = Uri.fromFile(f); 
     imgUri = contentUri; 
     System.out.println("Image uri " + imgUri); 
     mediaScanIntent.setData(contentUri); 
     this.sendBroadcast(mediaScanIntent); 
     ocrImage(); 
    } 


    public void ocrImage() { 
     try { 
      //getting image for ocr 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 4; 
      imgBitmap = BitmapFactory.decodeFile(imgPath, options); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     ExifInterface exif = null; 
     try { 
      exif = new ExifInterface(imgPath); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     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 = imgBitmap.getWidth(); 
      int h = imgBitmap.getHeight(); 

      // Setting pre rotate 
      Matrix mtx = new Matrix(); 
      mtx.preRotate(rotate); 

      // Rotating Bitmap & convert to ARGB_8888, required by tess 
      imgBitmap = Bitmap.createBitmap(imgBitmap, 0, 0, w, h, mtx, false); 
     } 
     imgBitmap = imgBitmap.copy(Bitmap.Config.ARGB_8888, true); 
     TessBaseAPI baseApi = new TessBaseAPI(); 
     baseApi.init(externalDataPath, "eng"); 
     baseApi.setImage(imgBitmap); 
     String ocrResult = baseApi.getUTF8Text(); 
     System.out.println(ocrResult); 
     baseApi.end(); 
     t.setText(ocrResult); 

    } 

    public void placeFileFromAssetsToExternalStorage(String filename) { 
     System.out.println("Running DataRunnable class "); 
     assetManager = getResources().getAssets(); 
     externalDataPath = Environment.getExternalStorageDirectory() + "/tessdata"; 
     System.out.println("external data path " + externalDataPath); 
     //creating eng.trainedData 
     File f = new File(externalDataPath); 
     try { 
      if (!f.exists()) { 
       f.mkdir(); 
      } 
      externalDataPath = externalDataPath + "/" + filename; 
      f = new File(externalDataPath); 

      if (!f.exists()) 
       f.createNewFile(); 

      externalDataPath = Environment.getExternalStorageDirectory().toString(); 

      trainDataInputStream = assetManager.open(filename); 
      trainDataOutputStream = new FileOutputStream(f); 
      byte[] buffer = new byte[1024]; 
      int read; 
      while ((read = trainDataInputStream.read(buffer)) != -1) { 
       trainDataOutputStream.write(buffer, 0, read); 
      } 
      trainDataOutputStream.flush(); 
      trainDataOutputStream.close(); 
      trainDataInputStream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
} 
+0

您好,谢谢您的回复,权限导致了这个问题,我设法修复它。谢谢! –