转自:http://blog.csdn.net/cherry609195946/article/details/9264409
首先该文章是总结, 不是原创, 是通过看网上其他大神的文章和自己的一些实践总结出来的.
一.图片的存在形式
2.流的形式(即以二进制形式存在于内存中)
3.Bitmap形式
二.常见的压缩方式
- public static void compressBmpToFile(Bitmap bmp,File file){
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int options = 80;//个人喜欢从80开始,
- bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
- while (baos.toByteArray().length / 1024 > 100) {
- baos.reset();
- options -= 10;
- bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
- }
- try {
- FileOutputStream fos = new FileOutputStream(file);
- fos.write(baos.toByteArray());
- fos.flush();
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private Bitmap compressBmpFromBmp(Bitmap image) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int options = 100;
- image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- while (baos.toByteArray().length / 1024 > 100) {
- baos.reset();
- options -= 10;
- image.compress(Bitmap.CompressFormat.JPEG, options, baos);
- }
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
- return bitmap;
- }
- private Bitmap compressImageFromFile(String srcPath) {
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
- newOpts.inJustDecodeBounds = true;//只读边,不读内容
- Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
- newOpts.inJustDecodeBounds = false;
- int w = newOpts.outWidth;
- int h = newOpts.outHeight;
- float hh = 800f;//
- float ww = 480f;//
- int be = 1;
- if (w > h && w > ww) {
- be = (int) (newOpts.outWidth / ww);
- } else if (w < h && h > hh) {
- be = (int) (newOpts.outHeight / hh);
- }
- if (be <= 0)
- be = 1;
- newOpts.inSampleSize = be;//设置采样率
- newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设
- newOpts.inPurgeable = true;// 同时设置才会有效
- newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收
- bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
- // return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩
- //其实是无效的,大家尽管尝试
- return bitmap;
- }
总结: 要想压缩内存中的Bitmap, 就要减少它的像素; 要想压缩为File, 就要降低它的质量
Matrix压缩法
public void transImage(String fromFile, int width, int height) { Bitmap bitmap = BitmapFactory.decodeFile(fromFile); int bitmapWidth = bitmap.getWidth(); int bitmapHeight = bitmap.getHeight(); // 缩放图片的尺寸 float scaleWidth = (float) width / bitmapWidth; float scaleHeight = (float) height / bitmapHeight; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // 产生缩放后的Bitmap对象 Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false); if (!bitmap.isRecycled()) { bitmap.recycle();// 记得释放资源,否则会内存溢出 } if (!resizeBitmap.isRecycled()) { resizeBitmap.recycle(); } }
ThumbnailUtils压缩法(android 2.2开始加入)
该类提供了三种静态方法可以直接调用获取。
static Bitmap createVideoThumbnail(String filePath, int kind) //获取视频文件的缩略图,第一个参数为视频文件的位置,比如/sdcard/android123.3gp,而第二个参数可以为MINI_KIND或MICRO_KIND最终和分辨率有关
static Bitmap extractThumbnail(Bitmap source, int width, int height, int options) //直接对Bitmap进行缩略操作,最后一个参数定义为OPTIONS_RECYCLE_INPUT ,来回收资源
static Bitmap extractThumbnail(Bitmap source, int width, int height) // 这个和上面的方法一样,无options选项
内存里面的Bitmap都是无压缩的,而我们保存的File一般都是经过压缩的,所以,将File读取到内存中会大很多。
只有bitmap.compress()方法是可以把bitmap写到File中的,此方法的压缩是质量压缩,即图片像素不变,减少每个点所保存的信息量来达到压缩的目的,保存下来的File的大小会变小,将压缩的File读取到内存中的Bitmap对象时,所占用的内存跟没压缩的是一样大小,即bitmap的像素多少不变,图片会失真。
SampleSize取样压缩, Matrix矩阵压缩,ThumbnailUtils(实际上也是矩阵压缩)都是对内存中的Bitmap进行压缩,会改变Bitmap的大小,即改变Bitmap的像素大小,得到一个新的压缩过的bitmap。
android 2.2开始,有一个类ThumbnailUtils用来处理图像压缩。