在我的应用程序中,我捕获图像,然后在第二个活动中应用裁剪,然后调整捕获图像的大小并将其发送到服务器。保存的图像看起来模糊。如何在调整大小后以良好的质量保存图像。下面是我的代码:
PassImageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap photo = getIntent().getExtras().getParcelable("image");
resultIv.setImageBitmap(photo);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
int x = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 413, getResources().getDisplayMetrics());
int y = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 531, getResources().getDisplayMetrics());
cropView.of(Uri.fromFile(finalFile)).withAspect(x, y).initialize(Image.this);
}
});
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(croppedBitmap);
Map<String, String> params = new Hashtable<String, String>();
//Adding parameters
params.put(KEY_IMAGE, image);
return params;
}
//resize image
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int width = 413;
int height = 531;
Bitmap bm = Bitmap.createScaledBitmap(bmp, width, height, false);
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
使用以下方法压缩图像:
public String bitmapToBase64(Bitmap bitmap) {
String imageBase64String;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
imageBase64String = Base64.encodeToString(byteArray, Base64.DEFAULT);
return imageBase64String;
}
反编译:
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 512;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}