Android 图片压缩之多种压缩方式结合使用

boolean optimize);

private static void saveBitmap(Bitmap bmp, int quality, String fileName, boolean optimize) {

compressBitmap(bit, bit.getWidth(), bit.getHeight(), quality, fileName.getBytes(), optimize);

}

4. 结合三种方式的终极压缩


首先通过尺寸压缩,压缩到手机常用的一个分辨率(1280*960 微信好像是压缩到这个分辨率),然后我们要把图片压缩到100KB以内,通过质量压缩来计算options需要设置为多少,最后调用JNI压缩,这边我测试了下,压缩出来的清晰度和原图几乎差不多,压缩时间大概1秒钟左右

public static int getRatioSize(int bitWidth, int bitHeight) {

// 图片最大分辨率

int imageHeight = 1280;

int imageWidth = 960;

// 缩放比

int ratio = 1;

// 缩放比,由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

if (bitWidth > bitHeight && bitWidth > imageWidth) {

// 如果图片宽度比高度大,以宽度为基准

ratio = bitWidth / imageWidth;

} else if (bitWidth < bitHeight && bitHeight > imageHeight) {

// 如果图片高度比宽度大,以高度为基准

ratio = bitHeight / imageHeight;

}

// 最小比率为1

if (ratio <= 0)

ratio = 1;

return ratio;

}

public static void compressBitmap(Bitmap image, String filePath) {

// 最大图片大小 100KB

int maxSize = 100;

// 获取尺寸压缩倍数

int ratio = NativeUtil.getRatioSize(image.getWidth(), image.getHeight());

// 压缩Bitmap到对应尺寸

Bitmap result = Bitmap.createBitmap(image.getWidth() / ratio, image.getHeight() / ratio, Config.ARGB_8888);

Canvas canvas = new Canvas(result);

Rect rect = new Rect(0, 0, image.getWidth() / ratio, image.getHeight() / ratio);

canvas.drawBitmap(image, null, rect, null);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

int options = 100;

result.compress(Bitmap.CompressFormat.JPEG, options, baos);

// 循环判断如果压缩后图片是否大于100kb,大于继续压缩

while (baos.toByteArray().length / 1024 > maxSize) {

// 重置baos即清空baos

baos.reset();

// 每次都减少10

options -= 10;

// 这里压缩options%,把压缩后的数据存放到baos中

result.compress(Bitmap.CompressFormat.JPEG, options, baos);

}

// JNI调用保存图片到SD卡 这个关键

NativeUtil.saveBitmap(result, options, filePath, true);

// 释放Bitmap

if (result != null && !result.isRecycled()) {

result.recycle();

result = null;

}

}

五. NativeUtil类的源码


16.9.29更新

1、添加getBitmapFromFile()方法,解决OOM和图片旋转的问题

2、添加compressBitmap()方法,传递当前图片本地路径和解压后图片保存路径两个参数,即可,实现压缩

package net.bither.util;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Matrix;

import android.graphics.Rect;

import android.media.ExifInterface;

/**

  • JNI图片压缩工具类

  • @Description TODO

  • @Package net.bither.util

  • @Class NativeUtil

  • @Copyright: Copyright © 2015

  • @version V1.0.0

*/

public class NativeUtil {

private static int DEFAULT_QUALITY = 95;

/**

  • @Description: JNI基本压缩

  • @param bit

  •        bitmap对象
    
  • @param fileName

  •        指定保存目录名
    
  • @param optimize

  •        是否采用哈弗曼表数据计算 品质相差5-10倍
    
  • @version V1.0.0

*/

public static void compressBitmap(Bitmap bit, String fileName, boolean optimize) {

saveBitmap(bit, DEFAULT_QUALITY, fileName, optimize);

}

/**

  • @Description: 通过JNI图片压缩把Bitmap保存到指定目录

  • @param image

  •        bitmap对象
    
  • @param filePath

  •        要保存的指定目录
    
  • @version V1.0.0

*/

public static void compressBitmap(Bitmap image, String filePath) {

// 最大图片大小 150KB

int maxSize = 150;

// 获取尺寸压缩倍数

int ratio = NativeUtil.getRatioSize(image.getWidth(),image.getHeight());

// 压缩Bitmap到对应尺寸

Bitmap result = Bitmap.createBitmap(image.getWidth() / ratio,image.getHeight() / ratio,Config.ARGB_8888);

Canvas canvas = new Canvas(result);

Rect rect = new Rect(0, 0, image.getWidth() / ratio, image.getHeight() / ratio);

canvas.drawBitmap(image,null,rect,null);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

int options = 100;

result.compress(Bitmap.CompressFormat.JPEG, options, baos);

// 循环判断如果压缩后图片是否大于100kb,大于继续压缩

while (baos.toByteArray().length / 1024 > maxSize) {

// 重置baos即清空baos

baos.reset();

// 每次都减少10

options -= 10;

// 这里压缩options%,把压缩后的数据存放到baos中

result.compress(Bitmap.CompressFormat.JPEG, options, baos);

}

// JNI保存图片到SD卡 这个关键

NativeUtil.saveBitmap(result, options, filePath, true);

// 释放Bitmap

if (!result.isRecycled()) {

result.recycle();

}

}

/**

  • @Description: 通过JNI图片压缩把Bitmap保存到指定目录

  • @param curFilePath

  •        当前图片文件地址
    
  • @param targetFilePath

  •        要保存的图片文件地址
    
  • @author XiaoSai

  • @date 2016年9月28日 下午17:43:15

  • @version V1.0.0

*/

public static void compressBitmap(String curFilePath, String targetFilePath) {

// 最大图片大小 150KB

int maxSize = 150;

//根据地址获取bitmap

Bitmap result = getBitmapFromFile(curFilePath);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

int quality = 100;

result.compress(Bitmap.CompressFormat.JPEG, quality, baos);

// 循环判断如果压缩后图片是否大于100kb,大于继续压缩

while (baos.toByteArray().length / 1024 > maxSize) {

// 重置baos即清空baos

baos.reset();

// 每次都减少10

quality -= 10;

// 这里压缩quality,把压缩后的数据存放到baos中

result.compress(Bitmap.CompressFormat.JPEG, quality, baos);

}

// JNI保存图片到SD卡 这个关键

NativeUtil.saveBitmap(result, quality, targetFilePath, true);

// 释放Bitmap

if (!result.isRecycled()) {

result.recycle();

}

}

/**

  • 计算缩放比

  • @param bitWidth 当前图片宽度

  • @param bitHeight 当前图片高度

  • @return int 缩放比

  • @version V1.0.0

*/

public static int getRatioSize(int bitWidth, int bitHeight) {

// 图片最大分辨率

int imageHeight = 1280;

int imageWidth = 960;

// 缩放比

int ratio = 1;

// 缩放比,由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

if (bitWidth > bitHeight && bitWidth > imageWidth) {

// 如果图片宽度比高度大,以宽度为基准

ratio = bitWidth / imageWidth;

} else if (bitWidth < bitHeight && bitHeight > imageHeight) {

// 如果图片高度比宽度大,以高度为基准

ratio = bitHeight / imageHeight;

}

// 最小比率为1

if (ratio <= 0)

ratio = 1;

return ratio;

}

/**

  • 通过文件路径读获取Bitmap防止OOM以及解决图片旋转问题

  • @param filePath

  • @return

*/

public static Bitmap getBitmapFromFile(String filePath){

BitmapFactory.Options newOpts = new BitmapFactory.Options();

newOpts.inJustDecodeBounds = true;//只读边,不读内容

BitmapFactory.decodeFile(filePath, newOpts);

int w = newOpts.outWidth;

int h = newOpts.outHeight;

// 获取尺寸压缩倍数

newOpts.inSampleSize = NativeUtil.getRatioSize(w,h);

newOpts.inJustDecodeBounds = false;//读取所有内容

newOpts.inDither = false;

newOpts.inPurgeable=true;

newOpts.inInputShareable=true;

newOpts.inTempStorage = new byte[32 * 1024];

Bitmap bitmap = null;

File file = new File(filePath);

FileInputStream fs = null;

try {

fs = new FileInputStream(file);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

try {

if(fs!=null){

bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(),null,newOpts);

//旋转图片

int photoDegree = readPictureDegree(filePath);

if(photoDegree != 0){

Matrix matrix = new Matrix();

matrix.postRotate(photoDegree);

// 创建新的图片

bitmap = Bitmap.createBitmap(bitmap, 0, 0,

bitmap.getWidth(), bitmap.getHeight(), matrix, true);

}

}

} catch (IOException e) {

e.printStackTrace();

} finally{

if(fs!=null) {

try {

fs.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return bitmap;

}

/**

  • 读取图片属性:旋转的角度

  • @param path 图片绝对路径

  • @return degree旋转的角度

*/

public static int readPictureDegree(String path) {

int degree = 0;

try {

ExifInterface exifInterface = new ExifInterface(path);

int orientation = exifInterface.getAttributeInt(

ExifInterface.TAG_ORIENTATION,

ExifInterface.ORIENTATION_NORMAL);

switch (orientation) {

case ExifInterface.ORIENTATION_ROTATE_90:

degree = 90;

break;

case ExifInterface.ORIENTATION_ROTATE_180:

degree = 180;

break;

case ExifInterface.ORIENTATION_ROTATE_270:

degree = 270;

break;

}

} catch (IOException e) {

e.printStackTrace();

}

return degree;

}

/**

  • 调用native方法

  • @Description:函数描述

  • @param bit

  • @param quality

  • @param fileName

  • @param optimize

  • @version V1.0.0

*/

private static void saveBitmap(Bitmap bit, int quality, String fileName, boolean optimize) {

compressBitmap(bit, bit.getWidth(), bit.getHeight(), quality, fileName.getBytes(), optimize);

}

/**

  • 调用底层 bitherlibjni.c中的方法

  • @Description:函数描述

  • @param bit

  • @param w

  • @param h

学习分享

在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2021最新上万页的大厂面试真题

七大模块学习资料:如NDK模块开发、Android框架体系架构…

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。

这份体系学习笔记,适应人群:
第一,学习知识比较碎片化,没有合理的学习路线与进阶方向。
第二,开发几年,不知道如何进阶更进一步,比较迷茫。
第三,到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。如果你有需要,我这里恰好有为什么,不来领取!说不定能改变你现在的状态呢!
由于文章内容比较多,篇幅不允许,部分未展示内容以截图方式展示 。

ze);

}

/**

  • 调用底层 bitherlibjni.c中的方法

  • @Description:函数描述

  • @param bit

  • @param w

  • @param h

学习分享

在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2021最新上万页的大厂面试真题

[外链图片转存中…(img-metGNH8p-1725885054654)]

七大模块学习资料:如NDK模块开发、Android框架体系架构…

[外链图片转存中…(img-Pzbjap8L-1725885054654)]

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。

这份体系学习笔记,适应人群:
第一,学习知识比较碎片化,没有合理的学习路线与进阶方向。
第二,开发几年,不知道如何进阶更进一步,比较迷茫。
第三,到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。如果你有需要,我这里恰好有为什么,不来领取!说不定能改变你现在的状态呢!
由于文章内容比较多,篇幅不允许,部分未展示内容以截图方式展示 。

Luban(鲁班)——Android图片压缩工具,仿微信朋友圈压缩策略。项目描述目前做app开发总绕不开图片这个元素。但是随着手机拍照分辨率的提升,图片的压缩成为一个很重要的问题。单纯对图片进行裁切,压缩已经有很多文章介绍。但是裁切成多少,压缩成多少却很难控制好,裁切过头图片太小,质量压缩过头则显示效果太差。于是自然想到app巨头“微信”会是怎么处理,Luban(鲁班)就是通过在微信朋友圈发送近100张不同分辨率图片,对比原图与微信压缩后的图片逆向推算出来的压缩算法。因为有其他语言也想要实现 Luban,所以描述了一遍算法步骤 因为是逆向推算,效果还没法跟微信一模一样,但是已经很接近微信朋友圈压缩后的效果,具体看以下对比!效果与对比内容原图LubanWechat截屏 720P720*1280,390k720*1280,87k720*1280,56k截屏 1080P1080*1920,2.21M1080*1920,104k1080*1920,112k拍照 13M(4:3)3096*4128,3.12M1548*2064,141k1548*2064,147k拍照 9.6M(16:9)4128*2322,4.64M1032*581,97k1032*581,74k滚动截屏1080*6433,1.56M1080*6433,351k1080*6433,482k导入compile 'io.reactivex:rxandroid:1.2.1' compile 'io.reactivex:rxjava:1.1.6' compile 'top.zibin:Luban:1.0.5'使用Listener方式Luban内部采用io线程进行图片压缩,外部调用只需设置好结果监听即可Luban.get(this)     .load(File)                     //传人要压缩的图片     .putGear(Luban.THIRD_GEAR)      //设定压缩档次,默认三挡     .setCompressListener(new OnCompressListener() { //设置回调         @Override         public void onStart() {             //TODO 压缩开始前调用,可以在方法内启动 loading UI         }         @Override         public void onSuccess(File file) {             //TODO 压缩成功后调用,返回压缩后的图片文件         }         @Override         public void onError(Throwable e) {             //TODO 当压缩过去出现问题时调用         }     }).launch();    //启动压缩RxJava方式RxJava 调用方式请自行随意控制线程Luban.get(this)         .load(file)         .putGear(Luban.THIRD_GEAR)         .asObservable()         .subscribeOn(Schedulers.io())         .observeOn(AndroidSchedulers.mainThread())         .doOnError(new Action1() {             @Override             public void call(Throwable throwable) {                 throwable.printStackTrace();             }         })         .onErrorResumeNext(new Func1>() {             @Override             public Observable<? extends File> call(Throwable throwable) {                 return Observable.empty();             }         })         .subscribe(new Action1() {             @Override             public void call(File file) {                 //TODO 压缩成功后调用,返回压缩后的图片文件             }         }); 标签:Luban(鲁班)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值