提高位图质量以节省/共享

问题描述 投票:0回答:1

我正在开发一个应用程序,其中我正在生成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; }

但保存或共享的图像质量很低(以位为单位),不会出现在图库中。

主要问题 - >如何通过保存/共享获得高质量的图像?

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

android bitmap qr-code
1个回答
0
投票

谢谢,但它的工作正如@MorrisonChang指出的那样,我没有通过高质量的调整大小的位图来保存/共享方法。

© www.soinside.com 2019 - 2024. All rights reserved.