Android如何创建运行时缩略图

70

我有一张大尺寸的图片。在运行时,我想要从存储中读取该图片并将其缩放,以便减小其大小和重量,使其成为缩略图。当用户点击缩略图时,我希望显示全尺寸的图片。

9个回答

136

试试这个

Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);

这个实用程序可从API_LEVEL 8开始使用。 [来源]


4
使用这段代码实际上是在内存中加载了一份大位图的副本,因此这不是处理大图像的好方法。 - GuillermoMP
11
正确的方法是解码文件的降采样版本。其他答案提供了这种方法。官方文档也很好地解释了这个过程:https://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html 。由于这个答案得票最高(因为它很简单),我只想警告大家,这并不是最好的方法。 - GuillermoMP
我该如何将此应用于可绘制资源? - JCarlosR
2
这个解决方案是否保持了宽高比? - Matan Tubul
这是什么导入? - Jithin Angel'z Jas'z

52

我的解决方案

byte[] imageData = null;

        try     
        {

            final int THUMBNAIL_SIZE = 64;

            FileInputStream fis = new FileInputStream(fileName);
            Bitmap imageBitmap = BitmapFactory.decodeStream(fis);

            imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            imageData = baos.toByteArray();

        }
        catch(Exception ex) {

        }

1
我的文件太大了,所以我不得不在 BitmapFactory.decodeStream(fis); 步骤中使用子采样。有关子采样的更多详细信息,请参见文档 - Diederik
7
最好使用Android的ThumbnailUtils类,就像下面的回答一样。 - afollestad
2
@afollestad 不是很对,这种方法才是正确的。只有在你确定处理的是小文件时,使用ThumbnailUtils才是一个好主意。 - GuillermoMP
如果(!imageBitmap.isRecycled()){ imageBitmap.recycle(); } - kakopappa
1
此解决方案仍将整个位图加载到内存中,然后进行缩放。 - Madeyedexter
显示剩余2条评论

14

我发现的最佳解决方案如下。与其他解决方案相比,这个解决方案不需要加载完整的图像来创建缩略图,因此更加高效!它的限制在于您无法获得精确宽度和高度的缩略图,但解决方案尽可能接近。

File file = ...; // the image file

Options bitmapOptions = new Options();
bitmapOptions.inJustDecodeBounds = true; // obtain the size of the image, without loading it in memory
BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);

// find the best scaling factor for the desired dimensions
int desiredWidth = 400;
int desiredHeight = 300;
float widthScale = (float)bitmapOptions.outWidth/desiredWidth;
float heightScale = (float)bitmapOptions.outHeight/desiredHeight;
float scale = Math.min(widthScale, heightScale);

int sampleSize = 1;
while (sampleSize < scale) {
    sampleSize *= 2;
}
bitmapOptions.inSampleSize = sampleSize; // this value must be a power of 2,
                                         // this is why you can not have an image scaled as you would like
bitmapOptions.inJustDecodeBounds = false; // now we want to load the image

// Let's load just the part of the image necessary for creating the thumbnail, not the whole image
Bitmap thumbnail = BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);

// Save the thumbnail
File thumbnailFile = ...;
FileOutputStream fos = new FileOutputStream(thumbnailFile);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();

// Use the thumbail on an ImageView or recycle it!
thumbnail.recycle();

2
这对于内存较低的设备最为适合。 - Duna
谢谢。它解决了内存不足异常的问题。 - Yury Finchenko

10

这是一个更完整的缩小位图到缩略图大小的解决方案。它在Bitmap.createScaledBitmap的基础上扩展了功能,通过保持图像的纵横比并将它们填充到相同的宽度,使它们在ListView中看起来更好。

此外,最好只进行一次缩放,并将结果作为blob存储在Sqlite数据库中。我已经包含了如何将Bitmap转换为字节数组以实现此目的的片段。

public static final int THUMBNAIL_HEIGHT = 48;
public static final int THUMBNAIL_WIDTH = 66;

imageBitmap = BitmapFactory.decodeByteArray(mImageData, 0, mImageData.length);
Float width  = new Float(imageBitmap.getWidth());
Float height = new Float(imageBitmap.getHeight());
Float ratio = width/height;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int)(THUMBNAIL_HEIGHT*ratio), THUMBNAIL_HEIGHT, false);

int padding = (THUMBNAIL_WIDTH - imageBitmap.getWidth())/2;
imageView.setPadding(padding, 0, padding, 0);
imageView.setImageBitmap(imageBitmap);



ByteArrayOutputStream baos = new ByteArrayOutputStream();  
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] byteArray = baos.toByteArray();

你为什么要在 createScaledBitmap 函数中交换 THUMBNAIL_HEIGHTTHUMBNAIL_HEIGHT 的参数顺序呢? - jayeffkay

6
使用BitmapFactory.decodeFile(...)方法获取你的Bitmap对象,并使用ImageView.setImageBitmap()方法将其设置到ImageView上。
ImageView上设置布局尺寸为较小值,例如:
android:layout_width="66dip" android:layout_height="48dip"

ImageView添加一个onClickListener,并启动一个新的活动,在其中以全尺寸显示图像。
android:layout_width="wrap_content" android:layout_height="wrap_content"

或者指定一些更大的尺寸。

11
当有多张图片时,你应该考虑事先将其缩小到缩略图大小。否则,在移动集合时可能会拖慢性能。 - Moritz
5
确实,为了做到这一点,您可以使用Bitmap.createScaledBitmap(originalBitmap,newWidth,newHeight,false); - Jim Blackler
1
这个方法会减小图片的大小吗? Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false) - d-man

3
/**
 * Creates a centered bitmap of the desired size.
 *
 * @param source original bitmap source
 * @param width targeted width
 * @param height targeted height
 * @param options options used during thumbnail extraction
 */
public static Bitmap extractThumbnail(
        Bitmap source, int width, int height, int options) {
    if (source == null) {
        return null;
    }

    float scale;
    if (source.getWidth() < source.getHeight()) {
        scale = width / (float) source.getWidth();
    } else {
        scale = height / (float) source.getHeight();
    }
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);
    Bitmap thumbnail = transform(matrix, source, width, height,
            OPTIONS_SCALE_UP | options);
    return thumbnail;
}


2
我发现一种简单的方法来做这件事
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(mPath),200,200)

语法

Bitmap thumbnail = ThumbnailUtils.extractThumbnail(Bitmap source,int width,int height)

或者

使用Picasso库

依赖项:compile 'com.squareup.picasso:picasso:2.5.2'

Picasso.with(context)
    .load("file:///android_asset/DvpvklR.png")
    .resize(50, 50)
    .into(imageView2);

Reference Picasso


0

这个答案基于https://developer.android.com/topic/performance/graphics/load-bitmap.html中提出的解决方案(不使用外部库),并由我进行了一些改进,使其功能更好、更实用。

关于这个解决方案的一些注意事项:

  1. 假设您想要保持纵横比。换句话说:

    finalWidth / finalHeight == sourceBitmap.getWidth() / sourceBitmap.getWidth() (不考虑强制转换和四舍五入问题)

  2. 假设您有两个值(maxWidthmaxHeight),您希望任何一个最终位图的尺寸都不超过其相应的值。换句话说:

    finalWidth <= maxWidth && finalHeight <= maxHeight

    因此,将minRatio作为计算基础(请参见实现)。与实际上将maxRatio作为计算基础的基本解决方案不同。此外,inSampleSize的计算更加合理、简洁和高效。

  3. 假设您希望(至少)最终尺寸中的一个维度恰好等于其相应的最大值 (通过考虑上述假设,每个值都是可能的)。换句话说:

    finalWidth == maxWidth || finalHeight == maxHeight

    与基本解决方案(Bitmap.createScaledBitmap(...))相比,最终的附加步骤是针对这个“恰好”约束条件的。 非常重要的一点是,您不应该像接受的答案那样首先采取此步骤,因为在处理大型图像时会消耗大量内存!

  4. 它用于解码file。您可以像基本解决方案一样更改它以解码resource(或任何BitmapFactory支持的内容)。

实现:

public static Bitmap decodeSampledBitmap(String pathName, int maxWidth, int maxHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);

    final float wRatio_inv = (float) options.outWidth / maxWidth,
          hRatio_inv = (float) options.outHeight / maxHeight; // Working with inverse ratios is more comfortable
    final int finalW, finalH, minRatio_inv /* = max{Ratio_inv} */;

    if (wRatio_inv > hRatio_inv) {
        minRatio_inv = (int) wRatio_inv;
        finalW = maxWidth;
        finalH = Math.round(options.outHeight / wRatio_inv);
    } else {
        minRatio_inv = (int) hRatio_inv;
        finalH = maxHeight;
        finalW = Math.round(options.outWidth / hRatio_inv);
    }

    options.inSampleSize = pow2Ceil(minRatio_inv); // pow2Ceil: A utility function that comes later
    options.inJustDecodeBounds = false; // Decode bitmap with inSampleSize set

    return Bitmap.createScaledBitmap(BitmapFactory.decodeFile(pathName, options),
          finalW, finalH, true);
}

/**
 * @return the largest power of 2 that is smaller than or equal to number. 
 * WARNING: return {0b1000000...000} for ZERO input.
 */
public static int pow2Ceil(int number) {
    return 1 << -(Integer.numberOfLeadingZeros(number) + 1); // is equivalent to:
    // return Integer.rotateRight(1, Integer.numberOfLeadingZeros(number) + 1);
}

示例用法,假设您有一个 imageView,其 layout_width 具有确定的值(match_parent 或显式值),而 layout_height 具有不确定的值(wrap_content),并且代替具有确定值的 maxHeight

imageView.setImageBitmap(decodeSampledBitmap(filePath, 
        imageView.getWidth(), imageView.getMaxHeight()));

0

如果你想要高质量的结果,那么使用[RapidDecoder][1]库。使用方法如下:

import rapid.decoder.BitmapDecoder;
...
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image)
                             .scale(width, height)
                             .useBuiltInDecoder(true)
                             .decode();

如果你想缩小不到50%并获得高质量的结果,请不要忘记使用内置解码器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接