整理文档,搜刮出一个Android图片实现压缩处理的实例代码,稍微整理精简一下做下分享。

详解:
1.获取本地图片File文件 获取BitmapFactory.Options对象 计算原始图片 目标图片宽高比 计算输出的图片宽高
2.根据宽高比计算options.inSampleSize值(缩放比例 If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.)得到bitmap位图 根据位图对象获取新的输出位图对象 Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)Creates a new bitmap, scaled from an existing bitmap, whenpossible.
3.获取图片方向调整、失量压缩图片保持在1024kb以下
//进行大小缩放来达到压缩的目的
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(srcImagePath, options);
//根据原始图片的宽高比和期望的输出图片的宽高比计算最终输出的图片的宽和高
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
float maxWidth = outWidth;
float maxHeight = outHeight;
float srcRatio = srcWidth / srcHeight; //原始图片宽高比
float outRatio = maxWidth / maxHeight; //目标图片宽高比
float actualOutWidth = srcWidth;
float actualOutHeight = srcHeight;
if (srcWidth > maxWidth || srcHeight > maxHeight) {
if(srcRatio>outRatio){ //原始宽高比大于目标宽高比
actualOutWidth = maxWidth;
actualOutHeight = actualOutWidth / srcRatio;
}else if(srcRatio<outRatio){ //原始宽高比小于目标宽高比
actualOutHeight = maxHeight;
actualOutWidth = actualOutHeight * srcRatio;
}
}else{
actualOutWidth = maxWidth;
actualOutHeight = maxHeight;
}
options.inSampleSize = computSampleSize(options, actualOutWidth, actualOutHeight);
options.inJustDecodeBounds = false;
Bitmap scaledBitmap = null;
try {
scaledBitmap = BitmapFactory.decodeFile(srcImagePath, options);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
if (scaledBitmap == null) {
return null;
}
//生成最终输出的bitmap
Bitmap actualOutBitmap = Bitmap.createScaledBitmap(scaledBitmap, (int) actualOutWidth, (int) actualOutHeight, true);
//释放原始位图资源
if(scaledBitmap!=actualOutBitmap){ //判断目标位图是否和原始位图指向栈目标相同
scaledBitmap.recycle();
scaledBitmap = null;
}
//处理图片旋转问题
ExifInterface exif = null;
try {
exif = new ExifInterface(srcImagePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix();
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
matrix.postRotate(90);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
matrix.postRotate(180);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
matrix.postRotate(270);
}
actualOutBitmap = Bitmap.createBitmap(actualOutBitmap, 0, 0,
actualOutBitmap.getWidth(), actualOutBitmap.getHeight(), matrix, true);
} catch (IOException e) {
e.printStackTrace();
return null;
}
//进行有损压缩
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options_ = 100;
actualOutBitmap.compress(Bitmap.CompressFormat.JPEG, options_, baos);//质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小)
int baosLength = baos.toByteArray().length;
while (baosLength / 1024 > maxFileSize) {//循环判断如果压缩后图片是否大于maxMemmorrySize,大于继续压缩
baos.reset();//重置baos即让下一次的写入覆盖之前的内容
options_ = Math.max(0, options_ - 10);//图片质量每次减少10
actualOutBitmap.compress(Bitmap.CompressFormat.JPEG, options_, baos);//将压缩后的图片保存到baos中
baosLength = baos.toByteArray().length;
if (options_ == 0)//如果图片的质量已降到最低则,不再进行压缩
break;
}
actualOutBitmap.recycle();
//将bitmap保存到指定路径
FileOutputStream fos = null;
String filePath = getOutputFileName(srcImagePath);
try {
fos = new FileOutputStream(filePath);
//包装缓冲流,提高写入速度
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos);
bufferedOutputStream.write(baos.toByteArray());
bufferedOutputStream.flush();
} catch (FileNotFoundException e) {
return null;
} catch (IOException e) {
return null;
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//获取位图缩放比例
private int computSampleSize(BitmapFactory.Options options, float reqWidth, float reqHeight) {
float srcWidth = options.outWidth;//20
float srcHeight = options.outHeight;//10
int sampleSize = 1;
if (srcWidth > reqWidth || srcHeight > reqHeight) {
int withRatio = Math.round(srcWidth / reqWidth);
int heightRatio = Math.round(srcHeight / reqHeight);
sampleSize = Math.min(withRatio, heightRatio);
}
return sampleSize;
}
压缩比例换算:
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
float widthScale = outWidth / srcWidth;//目标/原始 宽比例
float heightScale = outHeight / srcHeight; //目标原始 高比
//对比宽高比选择较大的一种比例
float scale = widthScale > heightScale ? widthScale : heightScale;
float actualOutWidth = srcWidth;
float actualOutHeight = srcHeight;
if (scale < 1) {
actualOutWidth = srcWidth * scale;
actualOutHeight = srcHeight * scale;
}
设置缩放比例--生成新的位图
Matrix matrix1 = new Matrix();
matrix1.postScale(scale, scale);// 放大缩小比例
//生成最终输出的bitmap
Bitmap actualOutBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix1, true);
if (actualOutBitmap != scaledBitmap) {
scaledBitmap.recycle();
scaledBitmap = null;
System.gc();
}
参考:https://github.com/guizhigang/LGImageCompressor
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Android图片压缩处理
# Android图片压缩
# android图片压缩的3种方法实例
# Android拍照得到全尺寸图片并进行压缩
# android 将图片压缩到指定的大小的示例
# Android WebP 图片压缩与传输
# Android实现简单图片压缩的方法
# Android图片压缩以及优化实例
# Android图片压缩(质量压缩和尺寸压缩)
# Android实现图片压缩示例代码
# 大家多多
# 文档
# srcImagePath
# float
# true
# decodeFile
# srcHeight
# outHeight
# srcWidth
# outWidth
# kb
# pre
# java
# inJustDecodeBounds
# class
# brush
# maxWidth
# catch
# OutOfMemoryError
# scaledBitmap
相关文章:
,制作一个手机app网站要多少钱?
,如何利用word制作宣传手册?
微信小程序 input输入框控件详解及实例(多种示例)
如何在万网ECS上快速搭建专属网站?
制作网站公司那家好,网络公司是做什么的?
开封网站制作公司,网络用语开封是什么意思?
威客平台建站流程解析:高效搭建教程与设计优化方案
南京做网站制作公司,南京哈发网络有限公司,公司怎么样,做网页美工DIV+CSS待遇怎么样?
如何选择高效响应式自助建站源码系统?
如何配置FTP站点权限与安全设置?
,在苏州找工作,上哪个网站比较好?
如何通过商城免费建站系统源码自定义网站主题?
网站制作的方法有哪些,如何将自己制作的网站发布到网上?
高防服务器如何保障网站安全无虞?
如何在万网主机上快速搭建网站?
移民网站制作流程,怎么看加拿大移民官网?
如何通过虚拟机搭建网站?详细步骤解析
建站之星安装失败:服务器环境不兼容?
广德云建站网站建设方案与建站流程优化指南
营销式网站制作方案,销售哪个网站招聘效果最好?
已有域名和空间,如何快速搭建网站?
SQL查询语句优化的实用方法总结
建站与域名管理如何高效结合?
如何快速重置建站主机并恢复默认配置?
代购小票制作网站有哪些,购物小票的简要说明?
网站视频制作书签怎么做,ie浏览器怎么将网站固定在书签工具栏?
高防网站服务器:DDoS防御与BGP线路的AI智能防护方案
武汉网站制作费用多少,在武汉武昌,建面100平方左右的房子,想装暖气片,费用大概是多少啊?
如何在阿里云香港服务器快速搭建网站?
建站主机与虚拟主机有何区别?如何选择最优方案?
制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?
如何通过主机屋免费建站教程十分钟搭建网站?
如何用腾讯建站主机快速创建免费网站?
东莞专业制作网站的公司,东莞大学生网的网址是什么?
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
如何通过wdcp面板快速创建网站?
韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南
如何在Golang中使用encoding/gob序列化对象_存储和传输数据
单页制作网站有哪些,朋友给我发了一个单页网站,我应该怎么修改才能把他变成自己的呢,请求高手指点迷津?
如何高效利用亚马逊云主机搭建企业网站?
实现虚拟支付需哪些建站技术支撑?
儿童网站界面设计图片,中国少年儿童教育网站-怎么去注册?
如何在香港免费服务器上快速搭建网站?
整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?
微信h5制作网站有哪些,免费微信H5页面制作工具?
如何在IIS服务器上快速部署高效网站?
如何通过FTP服务器快速搭建网站?
如何快速查询域名建站关键信息?
小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化
广东企业建站网站优化与SEO营销核心策略指南
*请认真填写需求信息,我们会在24小时内与您取得联系。