我正在开发一个应用程序,其中我正在生成QR码的位图,但是有一个问题,生成的以下QR码的位图质量非常低,因为我使用以下代码对其进行了改进:< / p>
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码。
然后我通过以下代码保存或分享它:
通过以下方式分享:
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"));
通过以下方式保存:
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;
}
但保存或共享的图像质量非常低(以位为单位),并且不会显示在图库中。
主要问题
- &gt; 如何通过保存/分享获得高质量的图像?
我不知道我用来改善位图质量的方法是否是最好的方法......
答案 0 :(得分:0)
谢谢,但正如@MorrisonChang指出的那样,我没有通过高质量的调整大小的位图来保存/共享方法。