android图片压缩工具类
好久没写博客了,一方面是因为最近找了家实习单位,很累基本上下班后就没有打不起精神去学习,另一方面我自己觉得写博客确实有点耗时间,趁着周六周日想花点时间研究下fresco,picass,Glide等框架,但是如何哪种框架,Bitmap总是基础,花了一上午的时间整理了下bitmap压缩的工具类,这里分享一下
package com.example.liujian.bitmapdemo;import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;
import android.view.WindowManager;import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;/*** create by liujian 2015/11/10* 图片压缩工具类*/
public class ImageResizerUtils {private static final String TAG = ImageResizerUtils.class.getSimpleName();private Context context;private WindowManager mWindowManager;public ImageResizerUtils(Context context) {this.context=context;}/*** 放大缩小图片,指定宽高* @param bitmap* @param w* @param h* @return*/public Bitmap resizeBitmap(Bitmap bitmap, int w, int h) {int width = bitmap.getWidth();int height = bitmap.getHeight();Matrix matrix = new Matrix();float scaleWidht = ((float) w / width);float scaleHeight = ((float) h / height);matrix.postScale(scaleWidht, scaleHeight);Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,matrix, true);if (!bitmap.isRecycled()) {bitmap.recycle();}return newbmp;}/*** Matrix类实现图片放大缩小,指定宽,宽高比例跟原图一致* @param bitmap * @param newWidth * @return */ public Bitmap resizeBitmap(Bitmap bitmap, int newWidth) {if (bitmap == null) return null; int w = bitmap.getWidth(); int h = bitmap.getHeight(); float temp = ((float) h) / ((float) w); int newHeight = (int) (newWidth * temp); float scaleWidth = ((float) newWidth) / w; float scaleHeight = ((float) newHeight) / h; Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); if (!bitmap.isRecycled()) {bitmap.recycle();}return resizedBitmap; }/*** 压缩成屏幕能够接受的大小* @param is* @return*/public Bitmap resizeBitmap(FileInputStream is) throws IOException {return resizeBitmap(is.getFD());}public Bitmap resizeBitmap(FileInputStream is,int reqWidth,int reqHeight) throws IOException {return resizeBitmap(is.getFD(),reqWidth,reqHeight);}public Bitmap resizeBitmap(FileDescriptor fd) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFileDescriptor(fd, null, options);options.inSampleSize = calculateInWindowSize(options);options.inJustDecodeBounds = false;return BitmapFactory.decodeFileDescriptor(fd, null, options);}/*** 压缩方法对FileInputStream的缩放存在问题,原因是FileInputStream是一种有序的* 文件流,而两次decodeStream调用影响了文件流的位置属性,导致第二次BitmapFactory.* decodeStream时得到是null。我们可以通过文件流来得到它所对应的文件描述符,* @param fd:FileDescriptor df = is.getFD();* @param reqWidth:所需图片宽(像素)* @param reqHeight:所需图片高(像素)* @return*/public Bitmap resizeBitmap(FileDescriptor fd,int reqWidth, int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFileDescriptor(fd, null, options);options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeFileDescriptor(fd, null, options);}/**** @param res:Resources对象* @param resId:资源文件id* @return*/public Bitmap resizeBitmap(Resources res, int resId) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;// 不去解析真正的位图,只是去获取这个文件的头文件信息BitmapFactory.decodeResource(res, resId, options);// 得到压缩的缩放比options.inSampleSize = calculateInWindowSize(options);options.inJustDecodeBounds = false;return BitmapFactory.decodeResource(res, resId, options);}/**** @param res:Resources对象* @param resId:资源文件id* @param reqWidth:缩放的目标Bitmao的宽度* @param reqHeight:缩放的目标Bitmap的高度* @return*/public Bitmap resizeBitmap(Resources res, int resId,int reqWidth, int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;// 不去解析真正的位图,只是去获取这个文件的头文件信息BitmapFactory.decodeResource(res, resId, options);// 得到压缩的缩放比options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeResource(res, resId, options);}/*** 根据路径压缩成指定的宽高* @param path* @param reqWidth* @param reqHeight* @return*/public Bitmap resizeBitmap(String path,int reqWidth,int reqHeight){BitmapFactory.Options opts=new BitmapFactory.Options();//解析位图的附加条件opts.inJustDecodeBounds=true;BitmapFactory.decodeFile(path,opts);opts.inSampleSize=calculateInSampleSize(opts,reqWidth,reqHeight);opts.inJustDecodeBounds=false;return BitmapFactory.decodeFile(path,opts);}/*** 根据路径压缩成屏幕可以接受的大小* @param path* @return*/public Bitmap resizeBitmap(String path){BitmapFactory.Options opts=new BitmapFactory.Options();//解析位图的附加条件opts.inJustDecodeBounds=true;BitmapFactory.decodeFile(path,opts);opts.inSampleSize=calculateInWindowSize(opts);opts.inJustDecodeBounds=false;return BitmapFactory.decodeFile(path,opts);}/*** 根据需要的图片宽高(px像素)来压缩 返回缩放比 缩放比大于1时,比如为2,* 压缩后的图片其宽高均为原图大小的1/2,其所占的内存大小也为原图的1/4* @param options* @param reqWidth:压缩的目标宽* @param reqHeight:压缩的目标高* @return 返回缩放比*/private int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {if (reqWidth <= 0 || reqHeight <= 0) {return 1;}// 得到图片的宽高final int height = options.outHeight;final int width = options.outWidth;Log.d(TAG, "origin, w= " + width + " h=" + height);int inSampleSize = 1;if (height > reqHeight || width > reqWidth) {final int halfHeight = height / 2;final int halfWidth = width / 2;while ((halfHeight / inSampleSize) >= reqHeight&& (halfWidth / inSampleSize) >= reqWidth) {inSampleSize *= 2;}}Log.d(TAG, "sampleSize:" + inSampleSize);return inSampleSize;}/*** 压缩成屏幕能够接受的大小的缩放比* @param options* @return*/private int calculateInWindowSize(BitmapFactory.Options options){//得到屏幕宽高if(mWindowManager==null){mWindowManager=((Activity)context).getWindowManager();}int windowWidth=mWindowManager.getDefaultDisplay().getWidth();int windowHeight=mWindowManager.getDefaultDisplay().getHeight();int bitmapWidth=options.outWidth;int bitmapHeight=options.outHeight;int dx=bitmapWidth/windowWidth;int dy=bitmapHeight/windowHeight;int scale=1;if(dx>dy&&dy>1){scale=dx;}if(dy>dx&&dy>1){scale=dy;}return scale;}
}
android图片压缩工具类相关推荐
- 一个好用的android图片压缩工具类
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- 整合的图片压缩工具类ImageTools
根据网上找的资料和自己用到的地方进行修改的图片压缩工具类,有什么不对的地方请见谅,源码如下: public final class ImageTools {/*** Transfer drawable ...
- java图片压缩工具类
java图片压缩工具类 PicCompressUtil.java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutput ...
- java图片压缩工具类(指定压缩大小)
1:先导入依赖 <!--thumbnailator图片处理--><dependency><groupId>net.coobird</groupId>&l ...
- Java图片压缩工具类(递归压缩到指定大小范围)
Java图片压缩工具 工具类使用场景 公司做人脸识别项目时候需要上传学生.家长.教师.访客的正面照图片,但是人脸识别机器有限制只接收200KB-1M的图片,所以必须做图片压缩到指定范围大少. APP上 ...
- Android CompressImage图片压缩工具类介绍
CompressImage是集成图片的质量压缩,像素压缩以及基于Luban算法的图片压缩三种压缩方式的工具 功能如下: 1. 质量压缩:从质量层面进行相关压缩(原图片可通过该工具压缩至小于期望大小的图 ...
- Android图片转换类 1. Bitmap去色,转换为黑白的灰度图, 2. Bitmap图片加圆角效果
/** * 处理图片的工具类. * */public class ImageTools { /***//** * 图片去色,返回灰度图片 * @param bmpOriginal 传入的图片 * @r ...
- Luban—— Android图片压缩工具
项目描述 目前做app开发总绕不开图片这个元素.但是随着手机拍照分辨率的提升,图片的压缩成为一个很重要的问题.单纯对图片进行裁切,压缩已经有很多文章介绍.但是裁切成多少,压缩成多少却很难控制好,裁切过 ...
- 安卓中鲁班图片压缩工具类
implementation 'top.zibin:Luban:1.1.3' implementation files('libs/autobahn-0.5.0.jar')//压缩图片 public ...
最新文章
- linux下history(历史)命令用法详解
- 怎么向女朋友解释什么叫区块链?
- SilverLight 双向绑定
- 浅析网站站内优化重要因素都有哪些?
- 有用就存档,没用就删除,当日清理当日的email
- mysql数据库技术基本操作_MySQL数据库的基础操作
- Leetcode题库 762.二进制表示中质数个计算置位(哈希数组 C实现)
- 利用JSP交互式打印表格
- console程序也有版本和图标
- JavaScript引用数据类型
- 服务器系统的安装方法,服务器的操作系统安装方法
- python time函数的功能_Python中Python时间模块的常用功能,Pythontime,函数
- 2012年九月六号阿里巴巴面试
- BZOJ4836: [Lydsy1704月赛]二元运算-分治FFT
- 技术分享 | 开发板网口热插拔自动获取IP地址
- Android开发自定义相机,自定义拍照界面
- excel一个表格分成多个的简单方法
- Java实现Sunday算法
- 怎么压缩图片200k以下?
- 35.静态链接库和动态链接库
热门文章
- Python小游戏-Las Vegas Black Jack- CASINO (21点)
- WEB在线预览PDF,WORD方案总结
- 【Quectel移远展锐平台5G模组RX500U/RG200U使用指南(二)-USB/TTL的使用】
- 微软游戏服务器断网,惊了!Win10出现断网问题,微软居然让你自己解决?
- Android中ExpandableListView中嵌套ListView
- 提供一个 无限存储 空间 免费网盘
- 精通正则表达式读书笔记
- [渝粤教育] 广东-国家-开放大学 21秋期末考试网络金融10248k2
- Csdn修改账户手机绑定问题
- 员工管理:人才九宫格,提低扩中保高