提高 saving/sharing 的位图质量

Improving Quality of Bitmap for saving/sharing

我正在开发一个生成 QR 码位图的应用程序,但是有一个问题,以下 QR 码生成的位图质量非常低,因为我使用下面的代码对其进行了改进:

 private static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight, boolean isNecessaryToKeepOrig) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);

        // "RECREATE" THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        if(!isNecessaryToKeepOrig){
            bm.recycle();
        }
        return resizedBitmap;
    }

并使用此方法调用上述方法:

imageView.setImageBitmap(getResizedBitmap(myBitmap,1000,1000,true));

通过使用上述方法,质量提高了很多,我可以在 imageView 中看到一个非常高质量的 QR 码,与以前的不同。

然后我通过下面的代码保存或分享它:

  1. 分享方式:

    String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
    String mImageName="QR-"+ timeStamp +".jpg";
    String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), myBitmap,mImageName, "QR code Generated");
    Uri bitmapUri = Uri.parse(bitmapPath);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/jpg");
    intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
    startActivity(Intent.createChooser(intent, "Share"));
    
  2. 保存方式:

    private void storeImage(Bitmap image) {
        File pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            Log.d(TAG,
                    "Error creating media file, check storage permissions: ");// e.getMessage());
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            image.compress(Bitmap.CompressFormat.JPEG,100,fos);
            fos.flush();
            fos.close();
            Toast.makeText(ImagePopUp.this,"Barcode Saved!!!",Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
    private  File getOutputMediaFile(){
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
                + "/Pictures");
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
        String mImageName="QR-"+ timeStamp +".jpg";
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
        return mediaFile;
    }
    

但是正在保存或共享的图像质量非常低(以位为单位)并且不会出现在图库中。

主要问题

->如何通过save/share获得非常高质量的图像?

而且我不知道我用来提高位图质量的方法是否是最好的方法...

谢谢,但正如@MorrisonChang 所指出的那样,我没有将高质量调整大小的位图传递给 save/share 方法。