优就业Android教程Android图片压缩实现过程及代码讲述.doc

优就业Android教程Android图片压缩实现过程及代码讲述.doc

  1. 1、本文档共10页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
优就业Android教程-Android图片压缩实现过程及代码 android图片压缩无非两种,一种质量压缩,一种像素压缩,前者多用于图片上传时,后者多用于本地图片展示缩略图时。 对于质量压缩,主要用到的一个方法就是: public boolean compress(CompressFormat format, int quality, OutputStream stream) {} 这是Bitmap类里的一个方法,第一个参数表示图片压缩的格式,android中提供了以下格式: public enum CompressFormat { JPEG (0), PNG (1), WEBP (2); CompressFormat(int nativeInt) { this.nativeInt = nativeInt; } final int nativeInt; } 第二个参数表示压缩的质量,注意这个是压缩的关键,它的取值是0到100,越小表示压缩的越厉害,第三个参数表示把压缩的数据写入了outputstream流中。 OK,来看一个例子: public byte [] compressBitmap(Bitmap?bitmap,int max){ int quality = 100; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); press(Bitmap.CompressFormat.JPEG,quality,byteArrayOutputStream); while (byteArrayOutputStream.toByteArray().length / 1024 max){ byteArrayOutputStream.reset(); quality = quality -10; press(Bitmap.CompressFormat.JPEG,quality,byteArrayOutputStream); } return byteArrayOutputStream.toByteArray(); } 这个方法的第一个参数不必解释,第二个参数表示你要求的压缩后图片最大可以是多少。最后可以拿到一个byte数组。我们有了这个byte数组就可以转化为file或者bitmap。 注意这种质量压缩后,像素本身没有改变。 对于像素压缩,顾名思义就是压缩像素。这里用到的一个主要的方法就是: public static Bitmap decodeFile(String pathName, Options opts) { Bitmap bm = null; InputStream stream = null; try { stream = new FileInputStream(pathName); bm = decodeStream(stream, null, opts); } catch (Exception e) { /* do nothing. If the exception happened on open, bm will be null. */ Log.e(BitmapFactory, Unable to decode stream: + e); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { // do nothing here } } } return bm; } 这是BitmapFactory中的一个静态方法,第一个参数表示file的全路径,第二个参数是关键,Options是BitmapFactory类中的一个静态内部类,它有两个非常重要的属性: /** * If set to true, the decoder will return null (no bitmap), but * the out... fields will still be set, allowing the caller to query * the bitmap without having to allocate the memory for its pixels. */ public boolean inJustDecodeBounds; /** * If set to a value 1, requests the decoder to subsample the original * image, returning a smaller image to save memory. The sample size i

文档评论(0)

jiayou10 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8133070117000003

1亿VIP精品文档

相关文档