import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/** * Image压缩工具类 * * @author yzy * */
public class ImageUtils {
/** * 按压缩比进行压缩:实际大小不一定 FixHeight: 指定不超过高度 FixWidth: 指定不超过宽度 FixAll: 不超过指定大小 * 固定宽高:直接设置宽高(待测试) FixFixWidth FixFixHeight FixFixAll * * @author yzy * */
public enum ZoomType { FixHeight, FixWidth, FixAll, FixFixWidth, FixFixHeight, FixFixAll }
/** 获取压缩image之后的bitmap **/
public static Bitmap zoomImage(String srcPath, int width, int height, ZoomType zoomType) {
switch (zoomType) {
case FixHeight: { }
case FixWidth: { }
case FixAll: {
return compressImage(getZoomBitmap(srcPath, width, height, zoomType));
}
case FixFixWidth: { }
case FixFixHeight: { }
case FixFixAll: {
return compressImage(zoomImageBitmapFix(srcPath, width, height, zoomType));
}
default:
return null;
}
}
/** 获取压缩image之后的beyt **/
public static byte[] zoomImageBytes(String srcPath, int width, int height, ZoomType zoomType) {
switch (zoomType) {
case FixHeight: { }
case FixWidth: { }
case FixAll: {
return getCompressImageByte(getZoomBitmap(srcPath, width, height, zoomType));
}
case FixFixWidth: { }
case FixFixHeight: { }
case FixFixAll: {
return getCompressImageByte(zoomImageBitmapFix(srcPath, width, height, zoomType));
}
default: return null;
}
}
/** 压缩质量 **/
private static Bitmap compressImage(Bitmap image) {
ByteArrayInputStream isBm = new ByteArrayInputStream( getCompressImageByte(image));// 把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
// 把ByteArrayInputStream数据生成图片
return bitmap;
}
/** 获取压缩之后的字节 **/
private static byte[] getCompressImageByte(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length / 1024 > 100) {
baos.reset(); options -= 10;
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
return baos.toByteArray();
}
/** 获取压缩后的bitmap **/
private static Bitmap getZoomBitmap(String srcPath, int width, int height, ZoomType zoomType) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
// 开始读入图片,options.inJustDecodeBounds // 设回true // 只读出图片的长和宽,不会真正返回bitmap,设置为false,才会真正返回bitmap
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
newOpts.inSampleSize = getSampleSize(getSampleSize(newOpts, width, height, zoomType));
// 只能取值2的幂
newOpts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return bitmap;
}
/** * 计算压缩比 * *
* @param options 图片原始大小参数
* @param maxWidth 最大宽度
* @param maxHeight 最大高度
* * @return */
private static int getSampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight, ZoomType zoomType) {
int width = options.outWidth;
int height = options.outHeight;
int inSampleSize = 1;
switch (zoomType) {
case FixWidth: {
if (width > maxWidth) {
inSampleSize = width % maxWidth == 0 ? width / maxWidth : width / maxWidth + 1; }
break;
}
case FixHeight: {
if (height > maxHeight) {
inSampleSize = height % maxHeight == 0 ? height / maxHeight : height / maxHeight + 1;
}
break;
}
case FixAll: {
if (height > maxHeight || width > maxWidth) {
// 当压缩后的大小小于当前大小时,计算压缩比,否则原大小显示
int heightScale = height % maxHeight == 0 ? height / maxHeight : height / maxHeight + 1;
int widthScale = width % maxWidth == 0 ? width / maxWidth : width / maxWidth + 1;
inSampleSize = heightScale > widthScale ? heightScale : widthScale;
// 按长宽压缩比大的比例来进行压缩
} break;
}
default: break;
}
return inSampleSize;
}
public static Bitmap zoomImage(String srcPath, int width, int height) {
return zoomImage(srcPath, width, height, ZoomType.FixAll);
}
public static byte[] getZoomImageBytes(String srcPath, int width, int height) {
return zoomImageBytes(srcPath, width, height, ZoomType.FixAll);
}
public static Bitmap zoomImage(String srcPath) {
return zoomImage(srcPath, 480, 800, ZoomType.FixAll);
}
public static byte[] getZoomImageBytes(String srcPath) {
return zoomImageBytes(srcPath, 480, 800, ZoomType.FixAll);
}
/** 固定长宽计算方式 **/
private static Bitmap zoomImageBitmapFix(String srcPath, int width, int height, ZoomType zoomType) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, options);
int outWidth = options.outWidth; int outHeight = options.outHeight;
options.inJustDecodeBounds = false;
int afterWidth = 0;
int afterHeight = 0;
switch (zoomType) {
case FixFixWidth: {
afterWidth = width; afterHeight = width * outHeight / outWidth;
break;
}
case FixFixHeight: {
afterHeight = height; afterWidth = height * outWidth / outHeight;
break;
}
case FixFixAll: {
afterWidth = width; afterHeight = height;
break;
}
default: break;
}
options.outHeight = afterHeight;
options.outWidth = afterWidth;
bitmap = BitmapFactory.decodeFile(srcPath, options);
return bitmap;
}
/** 调整设置SampleSize **/
private static int getSampleSize(int size) {
if ((size & (size - 1)) == 0) {// 表示该数为2的指数
return size;
}
return size * 2;
}
}