Java 在android上将图像文件转换为PNG

Java 在android上将图像文件转换为PNG,java,android,google-drive-android-api,Java,Android,Google Drive Android Api,我在下面的代码中执行OCR。我意识到最好的结果是在PNG图像中。我正在使用存储访问框架查看手机和谷歌硬盘中的所有文件。在“startOCR”函数中,我想转换PNG。这在安卓系统中可能吗 /** * Fires an intent to spin up the "file chooser" UI and select an image. */ public void performFileSearch(View view) { // ACTION_

我在下面的代码中执行OCR。我意识到最好的结果是在PNG图像中。我正在使用存储访问框架查看手机和谷歌硬盘中的所有文件。在“startOCR”函数中,我想转换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());
        }
    }
/**
*激发启动“文件选择器”UI并选择图像的意图。
*/
公共作废performFileSearch(视图){
//操作\u打开\u文档旨在通过系统文件选择文件
//浏览器。
意向意向=新意向(意向.行动\打开\文件);
//筛选以仅显示可“打开”的结果,例如
//文件(与联系人或时区列表相反)
intent.addCategory(intent.CATEGORY\u可打开);
//使用图像MIME数据类型筛选以仅显示图像。
//如果要搜索ogg vorbis文件,类型将为“audio/ogg”。
//要搜索通过已安装的存储提供商提供的所有文档,
//它将是“*/*”。
intent.setType(“image/*”);
startActivityForResult(意图、读取请求代码);
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图结果数据){
if(requestCode==READ\u REQUEST\u CODE&&resultCode==Activity.RESULT\u OK){
Uri=null;
if(resultData!=null){
uri=resultData.getData();
Log.i(标记“Uri”+Uri.toString());
Toast.makeText(MainActivity.this,“Uri:+Uri.toString(),Toast.LENGTH\u LONG.show();
IMGS_PATH=Environment.getExternalStorageDirectory().toString()+“/TesseractSample/IMGS”;
准备目录(IMGS_路径);
preparetesetract();
startOCR(uri);
}
}
}
//开始OCR功能的函数。
私有void startOCR(Uri imgUri){
试一试{
Log.e(标记“startOCR函数内部”);
BitmapFactory.Options=new-BitmapFactory.Options();
//1-表示最大尺寸。4-表示最大尺寸/4尺寸。不要使用值您可以尝试以下方法:

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"/>