您的位置:首页 > 移动开发 > Android开发

Android的图片压缩类ThumbnailUtils,供源码研究

2015-10-09 19:52 435 查看


一. 

主要是通过BitmapFactory.Options 来实现。

Options中有个属性inJustDecodeBounds。我们可以充分利用它,来避免大图片的溢出问题。他是什么原理呢?

API这样说:如果该 值设为true那么将不返回实际的bitmap,也不给其分配内存空间这样就避免内存溢出了。但是允许我们查询图片的信息这其中就包括图片大小信息(

options.outHeight (图片原始高度)和option.outWidth(图片原始宽度))。

Options中有个属性inSampleSize。我们可以充分利用它,实现缩放。

如果被设置为一个值>
1,要求解码器解码出原始图像的一个子样本,返回一个较小的bitmap,以节省存储空间。

例如,inSampleSize
= = 2,则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。

对于任何值<
= 1的同样处置为1。

那么相应的方法也就出来了,通过设置 inJustDecodeBounds为true,获取到outHeight(图片原始高度)和 outWidth(图片的原始宽度),然后计算一个inSampleSize(缩放值),

然后就可以取图片了,这里要注意的是,inSampleSize 可能小于0,必须做判断。

实现步骤:

第一步:BitmapFactory.Option

设置 inJustDecodeBounds为true

第二步:BitmapFactory.decodeFile(path,option)方法

解码图片路径为一个位图。如果指定的文件名是空的,或者不能解码到一个位图,函数将返回null[空值]。

获取到outHeight(图片原始高度)和 outWidth(图片的原始宽度)

第三步:计算缩放比例,也可以不计算,直接给它设定一个值。

options.inSampleSize = "你的缩放倍数";

如果是2就是高度和宽度都是原始的一半。

第四步:设置options.inJustDecodeBounds = false;

重新读出图片

bitmap = BitmapFactory.decodeFile(path,
options);

[java] view
plaincopyprint?

package com.tracyZhang.media;  

  

import android.app.Activity;  

import android.graphics.Bitmap;  

import android.graphics.BitmapFactory;  

import android.os.Bundle;  

import android.widget.ImageView;  

import android.widget.LinearLayout.LayoutParams;  

  

public class ImageCompressActivity extends Activity {  

      

      

    ImageView image;  

      

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.main);  

        image = (ImageView) findViewById(R.id.image);  

        image.setImageBitmap(decodeBitmap());  

    }  

  

    public Bitmap decodeBitmap()  

    {  

        BitmapFactory.Options options = new BitmapFactory.Options();  

        options.inJustDecodeBounds = true;  

        // 获取图片的宽和高  

        Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/xx.png", options);  

        float realWidth = options.outWidth;  

        float realHeight = options.outHeight;  

        System.out.println("真实图片高度:" + realHeight + "宽度:" + realWidth);  

        // 缩放比  

        int scale = (int) ((realHeight > realWidth ? realHeight : realWidth) / 300);  

        if (scale <= 0)  

        {  

            scale = 1;  

        }  

        options.inSampleSize = scale;  

        options.inJustDecodeBounds = false;  

        // 注意这次要把options.inJustDecodeBounds 设为 false,这次图片是要读取出来的。  

        bitmap = BitmapFactory.decodeFile("/sdcard/xx.png", options);  

        int w = bitmap.getWidth();  

        int h = bitmap.getHeight();  

        System.out.println("缩略图高度:" + h + "宽度:" + w);  

        image.setLayoutParams(new LayoutParams(w, h));  

        return bitmap;  

    }  

}  

这样我们就可以读取较大的图片而不会内存溢出了。


二.

从Android
2.2开始系统新增了一个缩略图ThumbnailUtils类,位于framework包下的android.media.ThumbnailUtils位置,可以帮助我们从mediaprovider中获取系统中的视频或图片文件的缩略图,该类提供了三种静态方法可以直接调用获取。

1、 static Bitmap  createVideoThumbnail(String filePath, int kind)  //获取视频文件的缩略图,第一个参数为视频文件的位置,比如/sdcard/android123.3gp,而第二个参数可以为MINI_KIND或 MICRO_KIND最终和分辨率有关 

2、 static Bitmap  extractThumbnail(Bitmap source, int width, int height, int options)  //直接对Bitmap进行缩略操作,最后一个参数定义为OPTIONS_RECYCLE_INPUT ,来回收资源

3、 static Bitmap  extractThumbnail(Bitmap source, int width, int height) // 这个和上面的方法一样,无options选项,最后提醒大家,ThumbnailUtils类是API Level从8或更高才开始支持的。

 
1、extractThumbnail (source, width, height):
 
Java代码 
/**
 * 
 * 创建一个指定大小的缩略图
 * @param source 源文件(Bitmap类型)
 * @param width  压缩成的宽度
 * @param height 压缩成的高度
 */ 
ThumbnailUtils.extractThumbnail(source, width, height); 
 
2、extractThumbnail(source, width, height, options):
 
 
Java代码 
/**
 * 创建一个指定大小居中的缩略图
 * 
 * @param source 源文件(Bitmap类型)
 * @param width  输出缩略图的宽度
 * @param height 输出缩略图的高度
 * @param options 如果options定义为OPTIONS_RECYCLE_INPUT,则回收@param source这个资源文件
 * (除非缩略图等于@param source)
 * 
 */ 
 ThumbnailUtils.extractThumbnail(source, width, height, options); 
 
 
3、createVideoThumbnail(filePath, kind):
 
 
Java代码 
/**
 * 创建一张视频的缩略图
 * 如果视频已损坏或者格式不支持可能返回null
 * 
 * @param filePath 视频文件路径  如:/sdcard/android.3gp
 * @param kind kind可以为MINI_KIND或MICRO_KIND
 * 
 */ 
ThumbnailUtils.createVideoThumbnail(filePath, kind); 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: