Android把图片压缩到一定大小并不失真

2023-05-16

                                                      本文转载只供参考

一.图片压缩方式


/**
 * 图片按比例大小压缩方法
 * @param srcPath (根据路径获取图片并压缩)
 * @return
 */
public static Bitmap getimage(String srcPath) {

    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    // 开始读入图片,此时把options.inJustDecodeBounds 设回true了
    newOpts.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
    float hh = 800f;// 这里设置高度为800f
    float ww = 480f;// 这里设置宽度为480f
    // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
    int be = 1;// 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;// 设置缩放比例
    // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
    bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
    return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩
}  

/**
 * 图片按比例大小压缩方法
 * @param image (根据Bitmap图片压缩)
 * @return
 */
public static Bitmap compressScale(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    // 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
    if (baos.toByteArray().length / 1024 > 1024) {
        baos.reset();// 重置baos即清空baos
        image.compress(Bitmap.CompressFormat.JPEG, 80, baos);// 这里压缩50%,把压缩后的数据存放到baos中
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    // 开始读入图片,此时把options.inJustDecodeBounds 设回true了
    newOpts.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    Log.i("", w + "---------------" + h);
    // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
    // float hh = 800f;// 这里设置高度为800f
    // float ww = 480f;// 这里设置宽度为480f
    float hh = 512f;
    float ww = 512f;
    // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
    int be = 1;// 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.RGB_565;//降低图片从ARGB888到RGB565
    // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
    isBm = new ByteArrayInputStream(baos.toByteArray());
    bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
    return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩
    //return bitmap;
}  

二.图片质量压缩


/**
 * 质量压缩方法
 * @param image
 * @return
 */
public static Bitmap compressImage(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
    int options = 90;
    while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
        baos.reset(); // 重置baos即清空baos
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
        options -= 10;// 每次都减少10
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
    return bitmap;
}  

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android把图片压缩到一定大小并不失真 的相关文章

随机推荐

  • Gson转换报错com.google.gson.JsonSyntaxException

    转载请标明出处 xff1a http blog csdn net xiejinquan article details 52002196 Gson将jsonobject的字符转化为Bean类或者将jsonarray的字符串转化为List l
  • MTK6580调试IMX132流程分析

    MTK6580调试IMX132流程分析 一开始不了解 MTK 的点亮流程 怎么办呢 1 MTK 开机是 首先是 CameraService 先起来 然后就通过获取 HAL 中的 sensorList 中的信息 CameraManager与C
  • 双线性插值算法

    图像的缩放很好理解 就是图像的放大和缩小 传统的绘画工具中 有一种叫做 放大尺 的绘画工具 xff0c 画家常用它来放大图画 当然 xff0c 在计算机上 xff0c 我们不再需要用放大尺去放大或缩小图像了 xff0c 把这个工作交给程序来
  • JAVA字符串判空的方法

    1 记录自己工作中的问题 xff1a 针对某些字符串进行判空时 xff0c 出现的BUG StringUtils hasText StringUtils hasText null 61 false StringUtils hasText 3
  • 出现次数最多的小写字母

    出现次数最多的小写字母 题目描述 输入一个由小写字母组成的字符串 xff08 字符数量 lt 61 100 xff09 xff0c 输出出现次数最多的小写字母 注意 xff1a 如果有多个小写字母出现的次数一样多 xff0c 则输出ASCI
  • EFCore 从入门到精通-6(详谈查询)

    目录 1 初始准备1 1 工具准备1 2 程序准备1 3 准备数据 2 基础回顾以及探寻2 1 单个查询2 2 查询所有的数据 2 3 筛选和过滤查询2 4 探究原理 3 客户端评估和服务端评估3 1 IEnumerable And IQu
  • 【Android解决方案】在onResume里调用getIntent()得到的是上一次数据

    我有四个媒体分类 xff08 Record xff0c Music xff0c Video xff0c Picture xff09 xff0c 里面除了数据不同 xff0c 界面都是相似的 xff0c 所以我把它们用一个MediaActiv
  • pycharm运行停止快捷键

    运行 shift 43 f10 停止 ctrl 43 f2
  • RecyclerView预加载

    private boolean isLoadingMore 61 false 是否预加载 recyclerView addOnScrollListener new RecyclerView OnScrollListener 64 Overr
  • 自定义右侧弹出dialog并填充状态栏

    DialogUtil xff1a public class DialogUtil private Dialog dialog private View inflate public void showRightDialog Context
  • Android监听横竖屏切换

    偶然在项目中用到播放视频时 xff0c 需要横屏将视频全屏播放 xff0c 所以需要监听屏幕的横竖屏切换事件 ConfigChanges xff0c 用于捕获手机状态的改变 xff0c 当横竖屏切换 xff0c 屏幕尺寸变化 xff0c 弹
  • SVN利用 AS 进行代码对比的方法

    第 1 种 xff1a 如果我们是从 SVN 检出的项目 xff0c 并且想比较本地代码与从 SVN 检出时的代码相比都有那些区别 xff0c 可以按如下步骤操作 如上图所示 xff0c 在代码编辑区 xff0c 右键唤出功能菜单 xff0
  • ADB操作命令详解大全

    ADB 操作命令详解及用法大全 Lucas liu的博客 CSDN博客
  • Android Studio build下面找不见assembleDebug选项解决办法

    在开发Android的AAR库时 xff0c 习惯点击右侧gradle面板的Task任务进行编译 xff0c 如选择assembleDebug或assembleRelease进行编译 xff0c 如下 xff1a 说明 xff1a 其中as
  • android 注销到登陆界面实现

    code class java span class hljs keyword public span span class hljs class span class hljs keyword class span span class
  • 中兴2016校招软件在线笔试题

    面试经验可以参考我的另一篇文章 xff0c 是7月初参加openday面试的 xff0c 文章链接http blog csdn net dandelion1314 article details 47009585 招聘群里有人发的招聘时间安
  • 设置AndroidStudio左侧和右侧的字体

    1 File Settings Appearance amp Behavior Appearance xff0c 右边Override default fonts by not recommended 2 设置代码大小 xff1a File
  • Android下载网络资源文件

    直接上代码 xff1a lt uses permission android name 61 34 android permission WRITE EXTERNAL STORAGE 34 gt lt uses permission and
  • 出现:trying to draw too large(138078000bytes) bitmap:错误时

    这里就不翻译了 xff0c 意思就是说你将高分辨率图片放在了低分辨率文件夹下 例如 xff1a 图片的分辨率是属于xxhdpi的 xff0c 而你将这张图片放在了drawable xhdpi或者比这个还低的文件夹下 xff0c 就会报这个错
  • Android把图片压缩到一定大小并不失真

    本文转载只供参考 一 图片压缩方式 图片按比例大小压缩方法 64 param srcPath xff08 根据路径获取图片并压缩 xff09 64 return public static Bitmap getimage String sr