BitmapUtils
来源:互联网 发布:edg那个知恩是谁 编辑:程序博客网 时间:2024/06/04 22:47
package com.zdsoft.testthree.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* Created by Administrator on 2017/3/24.
*/
public class BitmapUtils {
/**
*尺寸压缩
* @param imgPath 文件路径
* @param pixelW 压缩后的宽度
* @param pixelH 压缩后的高度
* @return
*/
public static Bitmap ratio(String imgPath, float pixelW, float pixelH) {
//是用来装载图片的属性的
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 开始读入图片,此时把options.inJustDecodeBounds 设回true,即只读边不读内容
newOpts.inJustDecodeBounds = true;
newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
// 获得图片的信息,但是返回的bitmap对象是空对象
Bitmap bitmap = BitmapFactory.decodeFile(imgPath,newOpts);
newOpts.inJustDecodeBounds = false;
//获得图片的宽度可高度
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// 想要缩放的目标尺寸
float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了
float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (w / ww);
} else if (w < h && h > hh) {//如果高度高的话根据高度固定大小缩放
be = (int) (h / hh);
}
if (be <= 0)
{
be = 1;
}
newOpts.inSampleSize = be;//设置缩放比例
// 开始压缩图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
// 压缩好比例大小后再进行质量压缩
// return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除
return bitmap;
}
/**
* 质量压缩
* @param path 文件路径
* @param maxSize 压缩后的文件大小 500kb
* @return
* @throws IOException
*/
public static ByteArrayOutputStream compress(String path, int maxSize) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
// 压缩比 100表示不压缩 100%
int options = 100;
Bitmap image = BitmapFactory.decodeFile(path);
// Store the bitmap into output stream(no compress)
image.compress(Bitmap.CompressFormat.JPEG, options, os);
// 循环压缩
while ( os.toByteArray().length / 1024 > maxSize) {
// Clean up os
os.reset();
// interval 10
options -= 10;
image.compress(Bitmap.CompressFormat.JPEG, options, os);
}
return os;
}
}
- bitMapUtils
- BitmapUtils
- BitmapUtils
- BitmapUtils源码
- bitmaputils方法
- android BitmapUtils
- xUtils系列之BitmapUtils
- 【Xutils-框架 BitmapUtils】解析!!!!!!!!!!!!!!!!!!!!!!!!
- BitmapUtils 的使用
- BitmapUtils功能详解
- 位图工具箱BitmapUtils
- BitmapUtils的相关知识
- Xutils之BitmapUtils使用
- BitmapUtils缓存图片
- ImageUtils(bitmapUtils封装)
- bitmaputils加载网络图片
- xUtils系列之BitmapUtils
- XUtils BitmapUtils 源码分析
- 树的遍历
- 如何实现OC与Swift混编
- 杂谈--敏捷开发
- 归纳与递归
- 笔试题:骰子是一个正方体,每个面有一个数字,初始为左1,右2,前3,后4,上5,下6,用123456表示这个状态,放置在平面上,可以向左翻转(用L表示向左翻转1次);可以向翻转(用R表示向左翻转
- BitmapUtils
- angularjs 学习
- 并集减去交集
- Qt实现搜索LAN设备
- 浅谈JavaScript模拟事件和自定义事件
- 团体程序设计天梯赛-练习集 L2-012关于堆的判断(25分)
- 直接插入排序
- 《剑指Offer》从尾到头打印链表
- 二叉树的遍历