图片压缩——压尺寸、压大小
2014-08-12 17:07
351 查看
图片的压缩主要有两种: 压尺寸,即调整平时所说的图片宽高。压大小,即对图片的容量大小进行压缩。
1、压尺寸
/**
* 根据路径获得图片并压缩返回bitmap用于显示(压尺寸)
*
* @param filePath 图片路径
* @return
*/
public Bitmap getSmallBitmap(final String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;// 当设为true,BitmapFactory.decodeFile(filePath,
// options),不生 成bitmap
BitmapFactory.decodeFile(filePath, options);
ImageSize srcSize = new ImageSize(options.outWidth, options.outWidth);
ImageSize targeSize = new ImageSize(180, 180);
options.inSampleSize = computeImageSampleSize(srcSize, targeSize);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
options.inPurgeable = true;// 如果 inPurgeable
// 设为True的话表示使用BitmapFactory创建的Bitmap
// 用于存储Pixel的内存空间在系统内存不足时可以被回收,
options.inInputShareable = true;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
// 将图片旋转正
Matrix matrix = new Matrix();
matrix.postRotate(readPictureDegree(filePath));
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
}
上面有段代码需要注意,市面有些手机拍完照片后生成的图片是旋转过的,这里需要根据旋转的角度对图片进行调正
/**
* 图片大小类,用于记录图片宽度和高度
**/
class ImageSize {
private final int width;
private final int height;
public ImageSize(int width, int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
/**
* 读取图片属性:旋转的角度
*
* @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;
}
/**
* 计算压缩值
* @param srcSize 根据源图片大小
* @param targetSize 目标压缩图片大小
* @return 返回压缩值
*/
public static int computeImageSampleSize(ImageSize srcSize, ImageSize targetSize) {
int srcWidth = srcSize.getWidth();
int srcHeight = srcSize.getHeight();
int targetWidth = targetSize.getWidth();
int targetHeight = targetSize.getHeight();
int scale = 1;
while (srcWidth / 2 >= targetWidth && srcHeight / 2 >= targetHeight) { // &&
srcWidth /= 2;
srcHeight /= 2;
scale *= 2;
}
if (scale < 1) {
scale = 1;
}
return scale;
}
// 2、压大小
/**
* @param imgPath
* @param bitmap
* @param imgFormat 图片格式
* @return
*/
public Bitmap compressBitmap(String imgPath, Bitmap bitmap) {
if (imgPath != null && imgPath.length() > 0) {
bitmap = getSmallBitmap(imgPath);
}
if (bitmap == null) {
// bitmap not found!!
}
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);//关键代码,根据压缩值50见图片压缩一半
out.flush();
out.close();
byte[] imgBytes = out.toByteArray();
return BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);
} catch (Exception e) {
// TODO Auto-generated catch block
return null;
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
1、压尺寸
/**
* 根据路径获得图片并压缩返回bitmap用于显示(压尺寸)
*
* @param filePath 图片路径
* @return
*/
public Bitmap getSmallBitmap(final String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;// 当设为true,BitmapFactory.decodeFile(filePath,
// options),不生 成bitmap
BitmapFactory.decodeFile(filePath, options);
ImageSize srcSize = new ImageSize(options.outWidth, options.outWidth);
ImageSize targeSize = new ImageSize(180, 180);
options.inSampleSize = computeImageSampleSize(srcSize, targeSize);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
options.inPurgeable = true;// 如果 inPurgeable
// 设为True的话表示使用BitmapFactory创建的Bitmap
// 用于存储Pixel的内存空间在系统内存不足时可以被回收,
options.inInputShareable = true;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
// 将图片旋转正
Matrix matrix = new Matrix();
matrix.postRotate(readPictureDegree(filePath));
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
}
上面有段代码需要注意,市面有些手机拍完照片后生成的图片是旋转过的,这里需要根据旋转的角度对图片进行调正
/**
* 图片大小类,用于记录图片宽度和高度
**/
class ImageSize {
private final int width;
private final int height;
public ImageSize(int width, int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
/**
* 读取图片属性:旋转的角度
*
* @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;
}
/**
* 计算压缩值
* @param srcSize 根据源图片大小
* @param targetSize 目标压缩图片大小
* @return 返回压缩值
*/
public static int computeImageSampleSize(ImageSize srcSize, ImageSize targetSize) {
int srcWidth = srcSize.getWidth();
int srcHeight = srcSize.getHeight();
int targetWidth = targetSize.getWidth();
int targetHeight = targetSize.getHeight();
int scale = 1;
while (srcWidth / 2 >= targetWidth && srcHeight / 2 >= targetHeight) { // &&
srcWidth /= 2;
srcHeight /= 2;
scale *= 2;
}
if (scale < 1) {
scale = 1;
}
return scale;
}
// 2、压大小
/**
* @param imgPath
* @param bitmap
* @param imgFormat 图片格式
* @return
*/
public Bitmap compressBitmap(String imgPath, Bitmap bitmap) {
if (imgPath != null && imgPath.length() > 0) {
bitmap = getSmallBitmap(imgPath);
}
if (bitmap == null) {
// bitmap not found!!
}
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);//关键代码,根据压缩值50见图片压缩一半
out.flush();
out.close();
byte[] imgBytes = out.toByteArray();
return BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);
} catch (Exception e) {
// TODO Auto-generated catch block
return null;
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- 巧用isee“压缩”限制图片的文件大小及其尺寸
- swift如何压缩图片到指定大小以内,尺寸和体积双压缩
- iOS 图片大小压缩 图片尺寸处理
- Linux系统下批量压缩图片尺寸大小的方法
- 有关于图片压缩大小--尺寸裁剪 和 压缩系数
- 图片压缩到规定大小和规定尺寸之内
- PHP 实现等比压缩图片尺寸和大小实例代码
- 图片压缩的两种形式(压缩到指定尺寸和压缩到指定大小)
- 图片的尺寸 大小压缩 和转化为base64
- 压缩图片大小尺寸
- iOS上传照片:图片尺寸压缩,上传时大小压缩,QBImagePickerController多选照片出现的问题
- iOS 图片大小压缩 图片尺寸处理
- 网页图片的压缩——保持尺寸不变,减小图片大小
- 图片压缩到指定尺寸和大小
- 图片上传之前检查大小、尺寸、格式并预览(Validate the file size/file type/image size/ before upload)
- 用JS检测图片尺寸和图片文件的大小
- java按比例压缩图片的源代码,用java如何把图片处理到指定大小
- 前台js限制上传图片质量大小和尺寸
- 图片上传之前检查大小、尺寸、格式并预览
- 图片上传之前检查大小、尺寸、格式并预览的js代码