2016-07-06 2 views
0

아래에 OCR을 수행하는 코드가 있습니다. 최상의 결과는 PNG 이미지 내에 있다는 것을 깨달았습니다. 내 저장 장치 액세스 프레임 워크를 사용하여 휴대 전화 및 Google 드라이브 내의 모든 파일을 봅니다. "startOCR"함수 내에서 PNG로 변환하고 싶습니다. 안드로이드에서 가능합니까?이미지 파일을 android의 PNG로 변환

/** 
    * Fires an intent to spin up the "file chooser" UI and select an image. 
    */ 
    public void performFileSearch(View view) { 

     // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file 
     // browser. 
     Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 

     // Filter to only show results that can be "opened", such as a 
     // file (as opposed to a list of contacts or timezones) 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 

     // Filter to show only images, using the image MIME data type. 
     // If one wanted to search for ogg vorbis files, the type would be "audio/ogg". 
     // To search for all documents available via installed storage providers, 
     // it would be "*/*". 
     intent.setType("image/*"); 

     startActivityForResult(intent, READ_REQUEST_CODE); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent resultData){ 

     if(requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK){ 
      Uri uri = null; 
      if(resultData != null){ 
       uri = resultData.getData(); 
       Log.i(TAG, "Uri" + uri.toString()); 

       Toast.makeText(MainActivity.this, "Uri:" + uri.toString(), Toast.LENGTH_LONG).show(); 
       IMGS_PATH = Environment.getExternalStorageDirectory().toString()+ "/TesseractSample/imgs"; 
       prepareDirectory(IMGS_PATH); 
       prepareTesseract(); 
       startOCR(uri); 
      } 
     } 
    } 

//Function that begins the OCR functionality. 
    private void startOCR(Uri imgUri) { 
     try { 

      Log.e(TAG, "Inside the startOCR function"); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      // 1 - means max size. 4 - means maxsize/4 size. Don't use value <4, because you need more memory in the heap to store your data. 
      options.inSampleSize = 4; 
      // FileOutputStream outStream = new FileOutputStream(String.valueOf(imgUri)); 
      Bitmap bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imgUri); 
      // bm.compress(Bitmap.CompressFormat.PNG,100,outStream); 

      Bitmap bitmap = BitmapFactory.decodeFile(imgUri.getPath()); 

      //The result variable will hold whatever is returned from "extractText" function. 
      result = extractText(bm); 

      //Creating the intent to go to the CropTest 
      Intent intentToCropTest = new Intent(MainActivity.this, CropTest.class); 
      intentToCropTest.putExtra("result",result); 
      startActivity(intentToCropTest); 

      //Setting the string result to the content of the TextView. 
      // textView.setText(result); 

     } catch (Exception e) { 
      Log.e(TAG, e.getMessage()); 
     } 
    } 

답변

1

이 작업을 시도 할 수 있습니다 :

public static Bitmap convertToPNG(Bitmap image) { 

    File storageDir = Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES); 
    File imageFile = File.createTempFile(
      imageFileName, /* prefix */ 
      ".png",   /* suffix */ 
      storageDir  /* directory */ 
    ); 

    FileOutputStream outStream = null; 
    try { 
     outStream = new FileOutputStream(imageFile); 
     image.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
     outStream.flush(); 
     outStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return BitmapFactory.decodeFile(imageFile.getAbsolutePath());; 
} 

가 이러한 권한을 추가

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>