【问题标题】:When I'm compressing the image size getting from gallary, the height and width are also compressed当我压缩从图库中获取的图像大小时,高度和宽度也会被压缩
【发布时间】:2017-02-23 12:05:47
【问题描述】:

我的问题是,当我压缩从图库中获取的图像大小(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);
}

【问题讨论】:

    标签: android android-camera android-bitmap bitmapfactory image-compression


    【解决方案1】:
    selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
    

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

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

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

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

    这是将您的图像缩放到较低维度的线。您可以在代码中简单地忽略此方法,并且您的图像不会调整大小。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 2016-02-08
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      相关资源
      最近更新 更多