2017-11-13 19 views
0

갤러리에서 사진을 가져 와서 오랫동안 viewImage에 저장하고 텍스트로 변경하려고 시도합니다. 오랜 시간 동안로드되고 응답하지 않습니다. 작은 크기의 jpg 파일로 시도하면 변환. 내가 솔루션과 함께 제공하고이미지에서 텍스트로의 변환을 위해 tess-two를 사용하고 있습니다.

`public void processImage(View view){ 
     String OCRresult = null; 
     mTess.setImage(photo); 
     OCRresult = mTess.getUTF8Text(); 
     TextView OCRTextView = (TextView) findViewById(R.id.OCRTextView); 
     OCRTextView.setText(OCRresult); 
    } 
private void checkFile(File dir) { 
     if (!dir.exists()&& dir.mkdirs()){ 
      copyFiles(); 
     } 
     if(dir.exists()) { 
      String datafilepath = datapath+ "/tessdata/eng.traineddata"; 
      File datafile = new File(datafilepath); 
     if (!datafile.exists()) { 
       copyFiles(); 
      } 
     } 
    } 
private void copyFiles() { 
     try { 
      String filepath = datapath + "/tessdata/eng.traineddata"; 
      AssetManager assetManager = getAssets(); 
      InputStream instream = assetManager.open("tessdata/eng.traineddata"); 
      OutputStream outstream = new FileOutputStream(filepath); 
      byte[] buffer = new byte[1024]; 
      int read; 
      while ((read = instream.read(buffer)) != -1) { 
       outstream.write(buffer, 0, read); 
      } 
      outstream.flush(); 
      outstream.close(); 
      instream.close(); 
      File file = new File(filepath); 
      if (!file.exists()) { 
       throw new FileNotFoundException(); 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    }` 

답변

0

갤러리에서와 촬영 한 후 함수를 호출하기위한 초기화 정팔 포체의 API

`Uri selectedImage = data.getData(); 
        String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
        System.out.println("image:" +selectedImage); 
        Cursor cursor = getContentResolver().query(selectedImage, 
          filePathColumn, null, null, null); 
        cursor.moveToFirst(); 
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        String picturePath = cursor.getString(columnIndex); 
        Log.d("Thisbuddy",picturePath); 
        photo = BitmapFactory.decodeFile(picturePath); 
        cursor.close(); 
        imageV.setImageBitmap(BitmapFactory.decodeFile(picturePath));` 

에 대한

`findViewById(R.id.gallery).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       photoPickerIntent.setType("image/*"); 
       startActivityForResult(photoPickerIntent, REQUEST_GALLERY); 
      } 
     });` 

for calling gallery 
`String language = "eng"; 
     datapath = getFilesDir()+ "/tesseract"; 
     mTess = new TessBaseAPI(); 
     checkFile(new File(datapath + "/tessdata/")); 
     mTess.init(datapath, language);` 

는 난 그냥 사용하여 이미지를 축소, viewImage에 보관 비트 맵

` float ratio = Math.min(
         (float) maxImageSize/photo.getWidth(), 
         (float) maxImageSize/photo.getHeight()); 
       int width = Math.round((float) ratio * photo.getWidth()); 
       int height = Math.round((float) ratio * photo.getHeight()); 

       Bitmap newBitmap = Bitmap.createScaledBitmap(photo, width, 
         height, filter);` 

이제 나를 위해 잘 작동합니다.