【问题标题】:How to rescale a bitmap file in android?如何在android中重新缩放位图文件?
【发布时间】:2013-08-14 11:40:56
【问题描述】:

我目前有一个解码位图的方法来优化图像文件的大小,但是输入文件不会是位图,然后会解码为位图。所以我的问题是,如果我的输入已经是位图,我该如何调整它的大小?谢谢。

private Bitmap compressFile(File f) {
    int REQUIRED_SIZE = 80;
    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.
        int width_tmp = o.outWidth;// , height_tmp = o.outHeight;
        int scale = 1;
        while (REQUIRED_SIZE > 0) {
            if (width_tmp <= REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            // height_tmp /= 2;
            scale++;
        }

        // 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;
}

我想改成的是compressFile (Bitmap f)

【问题讨论】:

  • 你是从网上加载图片还是从图库中选择?

标签: android decode bitmapimage bitmapfactory


【解决方案1】:

你也可以用这个

Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));

或者你要求:

bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream);

查看this link了解更多信息

【讨论】:

  • 这将导致高分辨率图像出现 outOfMemory 错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 2011-02-21
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多