图片的压缩的几种方法

news/2024/7/1 22:43:24

首先该文章是总结, 不是原创, 是通过看网上其他大神的文章和自己的一些实践总结出来的. 

一.图片的存在形式

1.文件形式(即以二进制形式存在于硬盘上)
2.流的形式(即以二进制形式存在于内存中)
3.Bitmap形式
这三种形式的区别: 文件形式和流的形式对图片体积大小并没有影响,也就是说,如果你手机SD卡上的如果是100K,那么通过流的形式读到内存中,也一定是占100K的内存,注意是流的形式,不是Bitmap的形式,当图片以Bitmap的形式存在时,其占用的内存会瞬间变大, 我试过500K文件形式的图片加载到内存,以Bitmap形式存在时,占用内存将近10M,当然这个增大的倍数并不是固定的

检测图片三种形式大小的方法:
文件形式: file.length()
流的形式: 讲图片文件读到内存输入流中,看它的byte数
Bitmap:    bitmap.getByteCount()

二.常见的压缩方式

1. 将图片保存到本地时进行压缩, 即将图片从Bitmap形式变为File形式时进行压缩,
    特点是:  File形式的图片确实被压缩了, 但是当你重新读取压缩后的file为 Bitmap是,它占用的内存并没有改变   
[java] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public static void compressBmpToFile(Bitmap bmp,File file){  
  2.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  3.         int options = 80;//个人喜欢从80开始,   
  4.         bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  5.         while (baos.toByteArray().length / 1024 > 100) {   
  6.             baos.reset();  
  7.             options -= 10;  
  8.             bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  9.         }  
  10.         try {  
  11.             FileOutputStream fos = new FileOutputStream(file);  
  12.             fos.write(baos.toByteArray());  
  13.             fos.flush();  
  14.             fos.close();  
  15.         } catch (Exception e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  
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();
		}
	}
方法说明: 该方法是压缩图片的质量, 注意它不会减少图片的像素,比方说, 你的图片是300K的, 1280*700像素的, 经过该方法压缩后, File形式的图片是在100以下, 以方便上传服务器, 但是你BitmapFactory.decodeFile到内存中,变成Bitmap时,它的像素仍然是1280*700, 计算图片像素的方法是 bitmap.getWidth()和bitmap.getHeight(), 图片是由像素组成的, 每个像素又包含什么呢? 熟悉PS的人知道, 图片是有色相,明度和饱和度构成的. 

该方法的官方文档也解释说, 它会让图片重新构造, 但是有可能图像的位深(即色深)和每个像素的透明度会变化,JPEG onlysupports opaque(不透明), 也就是说以jpeg格式压缩后, 原来图片中透明的元素将消失.所以这种格式很可能造成失真

既然它是改变了图片的显示质量, 达到了对File形式的图片进行压缩, 图片的像素没有改变的话, 那重新读取经过压缩 的file为Bitmap时, 它占用的内存并不会少.(不相信的可以试试)

因为: bitmap.getByteCount() 是计算它的像素所占用的内存, 请看官方解释: Returns the number of bytes used to  store this bitmap's pixels.

2.   将图片从本地读到内存时,进行压缩 ,即图片从File形式变为Bitmap形式
       特点: 通过设置采样率, 减少图片的像素, 达到对内存中的Bitmap进行压缩
       先看一个方法: 该方法是对内存中的Bitmap进行质量上的压缩, 由上面的理论可以得出该方法是无效的, 而且也是没有必要的, 因为你已经将它读到内存中了,再压缩多此一举, 尽管在获取系统相册图片时,某些手机会直接返回一个Bitmap, 但是这种情况下, 返回的Bitmap都是经过压缩的, 它不可能直接返回一个原声的Bitmap形式的图片, 后果可想而知
[java] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. private Bitmap compressBmpFromBmp(Bitmap image) {  
  2.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  3.         int options = 100;  
  4.         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  5.         while (baos.toByteArray().length / 1024 > 100) {   
  6.             baos.reset();  
  7.             options -= 10;  
  8.             image.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  9.         }  
  10.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
  11.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, nullnull);  
  12.         return bitmap;  
  13.     }  
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;
	}
  再看一个方法:
[java] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1.     private Bitmap compressImageFromFile(String srcPath) {  
  2.         BitmapFactory.Options newOpts = new BitmapFactory.Options();  
  3.         newOpts.inJustDecodeBounds = true;//只读边,不读内容   
  4.         Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
  5.   
  6.         newOpts.inJustDecodeBounds = false;  
  7.         int w = newOpts.outWidth;  
  8.         int h = newOpts.outHeight;  
  9.         float hh = 800f;//   
  10.         float ww = 480f;//   
  11.         int be = 1;  
  12.         if (w > h && w > ww) {  
  13.             be = (int) (newOpts.outWidth / ww);  
  14.         } else if (w < h && h > hh) {  
  15.             be = (int) (newOpts.outHeight / hh);  
  16.         }  
  17.         if (be <= 0)  
  18.             be = 1;  
  19.         newOpts.inSampleSize = be;//设置采样率   
  20.           
  21.         newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设   
  22.         newOpts.inPurgeable = true;// 同时设置才会有效   
  23.         newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收   
  24.           
  25.         bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
  26. //      return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩   
  27.                                     //其实是无效的,大家尽管尝试   
  28.         return bitmap;  
  29.     }  
	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形式的图片进行压缩, 也就是通过设置采样率, 减少Bitmap的像素, 从而减少了它所占用的内存

http://www.niftyadmin.cn/n/3649539.html

相关文章

[Wap]OnViewStateExpire异常的处理办法

[Wap]OnViewStateExpire异常的处理办法编写者日期关键词郑昀ultrapower2005-7-14Wap ASP.NET session timeout OnViewStateExpire我们的Wap页面由dotNET 编写而成&#xff0c;当页面停留时间过长后&#xff0c;由于IIS的session超时时间是20分钟&#xff0c;这时候再点击页面上…

css网格_在CSS网格中放置,扩展和密度

css网格介绍 (Introduction) The most common thing you learn in CSS Grid is usually related to the grid container and rather than the grid items. A generic grid definition applied to the grid container is enough for a basic layout structure. However, when yo…

华为云鲲鹏云服务器介绍

由于我的服务器到期&#xff0c;所以现在重新选购了一款服务器&#xff0c;基于ARM架构的华为云鲲鹏ECS。 ▣ 博主主站地址&#xff1a;微笑涛声 【www.cztcms.cn】 ▣ 博主其他平台&#xff1a; CSDN 简书 开源中国 思否 华为云博客 ◈ 华为鲲鹏处理器&#xff0c;坚持持续创…

创建react应用程序_React应用程序的6个优化技巧

创建react应用程序介绍 (Introduction) In the last few years JavaScript frameworks have completely changed the way we build apps, and React has had its fair share of the conversion process. Optimizing page load time is important because the time a page takes…

华为云鲲鹏云服务器安装MySQL 5.7.30

▣ 博主主站地址&#xff1a;微笑涛声 【www.cztcms.cn】 ▣ 博主其他平台&#xff1a; CSDN 简书 开源中国 思否 华为云博客 华为云鲲鹏云服务器搭载的是华为鲲鹏处理器&#xff08;916/920&#xff09;,华为鲲鹏处理器是基于ARM架构的处理器&#xff0c;不同于传统的X86架构…

prisma orm_Prisma与Node.js-第2部分:Prisma绑定

prisma ormOver in Part 1 we discussed how to connect our deployed Prisma API to our Node.js-based GraphQL server. Now we’re going to look into using the prisma-bindings library to start using all the superpowers that Prisma has generated for us. 在第1部分…

华为云鲲鹏云服务器安装Nginx

▣ 博主主站地址&#xff1a;微笑涛声 【www.cztcms.cn】 ▣ 博主其他平台&#xff1a; CSDN 简书 开源中国 思否 华为云博客 华为云鲲鹏云服务器搭载的是华为鲲鹏处理器&#xff08;916/920&#xff09;,华为鲲鹏处理器是基于ARM架构的处理器&#xff0c;不同于传统的X86架构…

几种常见的设计模式

--什么是设计模式&#xff1f;设计模式有哪几种分类&#xff1f;每类模式重点解决什么问题&#xff1f; 设计模式&#xff1a;是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。 …