Android图片工具类:获取Bitmap,压缩
2017-09-14 20:50
337 查看
获取本地图片的Bitmap
/**
* 加载本地图片
* @param url
* @return
*/
public static Bitmap getLocalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 1;//此参数为1表示质量不损失
Bitmap btp =BitmapFactory.decodeStream(fis,null,options);
return btp; ///把流转化为Bitmap图片
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
将本地图片文件压缩至1M以下,保持原尺寸,损失清晰度
/**
* compress img to less than 1M
* @param bmp source Bitmap
* @param file target File specified
*/
public static void compressBmpToFile(Bitmap bmp,File file){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 99;//设置开始的compress系数
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
while (baos.toByteArray().length / 1024 > 1024) {
baos.reset();
options -= 1;
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加载本地图片
* @param url
* @return
*/
public static Bitmap getLocalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 1;//此参数为1表示质量不损失
Bitmap btp =BitmapFactory.decodeStream(fis,null,options);
return btp; ///把流转化为Bitmap图片
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
将本地图片文件压缩至1M以下,保持原尺寸,损失清晰度
/**
* compress img to less than 1M
* @param bmp source Bitmap
* @param file target File specified
*/
public static void compressBmpToFile(Bitmap bmp,File file){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 99;//设置开始的compress系数
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
while (baos.toByteArray().length / 1024 > 1024) {
baos.reset();
options -= 1;
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- Android ImageUtils 图片工具类,包括:小米从相册调图像空指针,三星旋转照片,压缩图片,保存Bitmap到本地,获取本地视频的缩略图,毛化玻璃效果,代码以后会更新
- android 通过uri获取bitmap图片并压缩
- android 通过uri获取bitmap图片并压缩
- android处理图片工具类,图片压缩,把图片互换转换成base64和bitmap
- Android 工具类系列-BitmapUtil(图片动态压缩工具类)
- android 图片工具类 (图片压缩 图片长按缓存 Bitmap转Base64 Bitmap转File File转Bitmap 打开系统相册解析URI)
- Android 把bitmap转换成String,计算图片的缩放值,根据路径获得突破并压缩返回bitmap用于显示,获取保存 隐患检查的图片文件夹名称,获取保存图片的目录,根据路径删除图片,添加到图库
- android 根据图片url获取bitmap或者drawable,然后再进行压缩处理
- android 通过uri获取bitmap图片并压缩
- android 通过uri获取bitmap图片并压缩
- android 通过uri获取bitmap图片并压缩
- android 通过uri获取bitmap图片并压缩
- android 通过uri获取bitmap图片并压缩
- Android中图片处理(压缩 保存 显示 截屏 视图View转为图片Bitmap)函数工具类
- 详解android 通过uri获取bitmap图片并压缩
- android 通过uri获取bitmap图片并压缩
- android 通过uri获取bitmap图片并压缩
- android 通过uri获取bitmap图片并压缩
- Android 图片压缩即生成缩略图方法-BitmapFacotry.Options。
- Bitmap压缩原理解析与Android 7.0之前通过NDK使用libjpeg库高质量压缩图片