当我压缩从图库中获取的图像大小时,高度和宽度也会被压缩

When I'm compressing the image size getting from gallary, the height and width are also compressed

我的问题是当我压缩从图库中获取的图像大小时(Mb 到 Kb),图像的高度和宽度也在压缩。谁能告诉我如何解决这个问题?我想获得完整尺寸的图像(全高和全宽)。

这是图像上onclick监听器的代码:

 try {
  IsProfilePic = 5;
  usrimage5.setImageBitmap(Bitmap.createScaledBitmap(selectedBitmap, usrimage5.getWidth(), usrimage5.getHeight(), false));
  usrimage5.setScaleType(ImageView.ScaleType.FIT_XY);
  if (usrimage5.getTag() != new Integer(0))
  DeleteImageID = usrimage5.getTag().toString();
 } catch (OutOfMemoryError e) {
  Log.e("Nithin 5", "" + e.toString());
 }

这是我用来压缩图像的代码:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
encoded = resizeBase64Image(Base64.encodeToString(byteArray, Base64.DEFAULT));

这是我在调整图像大小时调用的方法resizeBase64Image()

 public String resizeBase64Image(String base64image) {
    byte[] encodeByte = Base64.decode(base64image.getBytes(), Base64.DEFAULT);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPurgeable = true;
    Bitmap image = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length, options);

    if (image.getHeight() <= 400 && image.getWidth() <= 400) {
        return base64image;
    }
    image = Bitmap.createScaledBitmap(image, 400, 400, false);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, baos);

    byte[] b = baos.toByteArray();
    System.gc();
    return Base64.encodeToString(b, Base64.NO_WRAP);
}
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

此行将以最大质量压缩图像,这可能不是您的目标。如果您想将图像压缩到较小的尺寸(和 JPEG 格式),请将质量设置为较低的值(例如:使用 90 或 80 而不是 100)

image.compress(Bitmap.CompressFormat.PNG, 100, baos);

这一行不会压缩你的图像,因为 PNG 是一种无损格式,并且会忽略整数值(这里你使用了 100,无论如何这是最大值)。

image = Bitmap.createScaledBitmap(image, 400, 400, false);

这是将图像缩放到较低尺寸的线。您只需在代码中忽略此方法,图像就不会调整大小。

Follow this link 了解有关图像压缩和缩放的更多信息。