图片压缩工具类
来源:互联网 发布:extjs4 获取grid数据 编辑:程序博客网 时间:2024/06/04 19:31
package edu.feicui.app.phone.base.util;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
/**
* BitmapUtil
*
* @author yuanc
*
*/
public class BitmapUtil {
public static Bitmap loadBitmap(String pathName, SizeMessage sizeMessage) {
// 加载的图像目标大小
int targetw = sizeMessage.getW();
int targeth = sizeMessage.getH();
Context context = sizeMessage.getContext();
Options options = new Options();
options.inJustDecodeBounds = true; // 打开"边界处理"
BitmapFactory.decodeFile(pathName, options);
int resw = options.outWidth;
int resh = options.outHeight;
if (resw <= targetw && resh <= targeth) {
options.inSampleSize = 1; // 设置加载时的资源比例
}
// 比例计算
else {
int scalew = resw / targetw;
int scaleh = resh / targeth;
int scale = scalew > scaleh ? scalew : scaleh;
options.inSampleSize = scale;// 设置加载时的资源比例
}
options.inJustDecodeBounds = false;// 关闭"边界处理"
Bitmap bitmap = BitmapFactory.decodeFile(pathName, options);
return bitmap;
}
public static Bitmap loadBitmap(int resID, SizeMessage sizeMessage) {
// 加载的图像目标大小
int targetw = sizeMessage.getW();
int targeth = sizeMessage.getH();
Context context = sizeMessage.getContext();
Options options = new Options();
options.inJustDecodeBounds = true; // 打开"边界处理"
BitmapFactory.decodeResource(context.getResources(), resID, options);
int resw = options.outWidth;
int resh = options.outHeight;
if (resw <= targetw && resh <= targeth) {
options.inSampleSize = 1; // 设置加载时的资源比例
}
// 比例计算
else {
int scalew = resw / targetw;
int scaleh = resh / targeth;
int scale = scalew > scaleh ? scalew : scaleh;
options.inSampleSize = scale;// 设置加载时的资源比例
}
options.inJustDecodeBounds = false;// 关闭"边界处理"
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID, options);
return bitmap;
}
public static class SizeMessage {
private int w;
private int h;
private Context context;
/**
* 大小信息
*
* @param context
* @param isPx
* 是否为像素单位
* @param w
* @param h
*/
public SizeMessage(Context context, boolean isPx, int w, int h) {
this.context = context;
if (!isPx) {
w = DeviceUtil.dp2px(context, w);
h = DeviceUtil.dp2px(context, h);
}
this.w = w;
this.h = h;
}
public Context getContext() {
return context;
}
public int getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
}
}
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
/**
* BitmapUtil
*
* @author yuanc
*
*/
public class BitmapUtil {
public static Bitmap loadBitmap(String pathName, SizeMessage sizeMessage) {
// 加载的图像目标大小
int targetw = sizeMessage.getW();
int targeth = sizeMessage.getH();
Context context = sizeMessage.getContext();
Options options = new Options();
options.inJustDecodeBounds = true; // 打开"边界处理"
BitmapFactory.decodeFile(pathName, options);
int resw = options.outWidth;
int resh = options.outHeight;
if (resw <= targetw && resh <= targeth) {
options.inSampleSize = 1; // 设置加载时的资源比例
}
// 比例计算
else {
int scalew = resw / targetw;
int scaleh = resh / targeth;
int scale = scalew > scaleh ? scalew : scaleh;
options.inSampleSize = scale;// 设置加载时的资源比例
}
options.inJustDecodeBounds = false;// 关闭"边界处理"
Bitmap bitmap = BitmapFactory.decodeFile(pathName, options);
return bitmap;
}
public static Bitmap loadBitmap(int resID, SizeMessage sizeMessage) {
// 加载的图像目标大小
int targetw = sizeMessage.getW();
int targeth = sizeMessage.getH();
Context context = sizeMessage.getContext();
Options options = new Options();
options.inJustDecodeBounds = true; // 打开"边界处理"
BitmapFactory.decodeResource(context.getResources(), resID, options);
int resw = options.outWidth;
int resh = options.outHeight;
if (resw <= targetw && resh <= targeth) {
options.inSampleSize = 1; // 设置加载时的资源比例
}
// 比例计算
else {
int scalew = resw / targetw;
int scaleh = resh / targeth;
int scale = scalew > scaleh ? scalew : scaleh;
options.inSampleSize = scale;// 设置加载时的资源比例
}
options.inJustDecodeBounds = false;// 关闭"边界处理"
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID, options);
return bitmap;
}
public static class SizeMessage {
private int w;
private int h;
private Context context;
/**
* 大小信息
*
* @param context
* @param isPx
* 是否为像素单位
* @param w
* @param h
*/
public SizeMessage(Context context, boolean isPx, int w, int h) {
this.context = context;
if (!isPx) {
w = DeviceUtil.dp2px(context, w);
h = DeviceUtil.dp2px(context, h);
}
this.w = w;
this.h = h;
}
public Context getContext() {
return context;
}
public int getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
}
}
阅读全文
0 0
- android 图片压缩工具类
- 多功能图片压缩工具类
- 多功能图片压缩工具 类
- Android图片压缩工具类
- java 图片压缩工具类
- Android图片压缩工具类
- 图片压缩工具类
- Android图片压缩工具类
- 上传图片压缩工具类
- android图片压缩工具类
- 上传图片压缩工具类
- 图片的压缩工具类
- 图片压缩工具类
- android 图片压缩工具类
- 图片压缩工具类
- 图片压缩工具类
- android图片压缩工具类
- java上传图片压缩工具类
- 错过刘自鸿的天使轮,让徐小平愧对天使投资人的骄傲
- 三道题+作业题
- LCD浮点数显示函数的探讨
- 设置Android模拟器上网的方法
- JSP获取绝对路径
- 图片压缩工具类
- Java 基础排序算法(冒泡,比较,插入,选择,希尔,堆,归并)
- dataframe数据之间求补集
- 17
- Android | 布局
- Python lambda表达式
- powershell 使用百度AI进行文本识别
- 页面加载后自动运行
- 计算机网络的性能