public class BitmapUtil
{
/**
* 图片大于300k则压缩。
*/
private static final int imageMaxSize = 300 * 1024;
/**
*
* <图片按比例大小压缩方法(根据路径获取图片并压缩)>
* <功能详细描述>
* @param srcPath
* @param isNeedTime
* @return
* @see [类、类#方法、类#成员]
*/
public static ByteArrayOutputStream getImageByPath(String srcPath, boolean isNeedTime)
{
Bitmap bitmap = null;
String time = null;
try
{
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
newOpts.inJustDecodeBounds = false;
int width = newOpts.outWidth;
int height = newOpts.outHeight;
// 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float minHeight = 800f;
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;// be=1表示不缩放
if (width > height && width > minHeight)
{
// 如果宽度大的话根据宽度固定大小缩放
be = (int)(newOpts.outWidth / minHeight);
}
else if (width < height && height > minHeight)
{
// 如果高度高的话根据宽度固定大小缩放
be = (int)(newOpts.outHeight / minHeight);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// 设置缩放比例
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
//获取图片的生成时间
ExifInterface exif = new ExifInterface(srcPath);
time = exif.getAttribute(ExifInterface.TAG_DATETIME);
if (time == null || "".equals(time))
{
//取不到照片的创建时间,则取照片的最后修改时间
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
File file = new File(srcPath);
Date date = new Date(file.lastModified());
time = GeneralUtils.splitToSecond(dateFormat.format(date));
}
else
{
time = GeneralUtils.splitToPhotoTime(time);
}
}
catch (IOException e)
{
e.printStackTrace();
}
return compressImage(bitmap, time, isNeedTime);// 压缩好比例大小后再进行质量压缩
}
/**
*
* <图片按比例大小压缩方法(根据Bitmap图片压缩) >
* <功能详细描述>
* @param data
* @param time
* @return
* @see [类、类#方法、类#成员]
*/
public static ByteArrayOutputStream getImageByStream(byte[] data, String time)
{
Bitmap bitmap = null;
ByteArrayInputStream isBm = new ByteArrayInputStream(data);
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
newOpts.inJustDecodeBounds = false;
int width = newOpts.outWidth;
int height = newOpts.outHeight;
// 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float minHeight = 800f;// 这里设置高度为800f
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;// be=1表示不缩放
if (width > height && width > minHeight)
{// 如果宽度大的话根据宽度固定大小缩放
be = (int)(newOpts.outWidth / minHeight);
}
else if (width < height && height > minHeight)
{// 如果高度高的话根据宽度固定大小缩放
be = (int)(newOpts.outHeight / minHeight);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// 设置缩放比例
isBm = new ByteArrayInputStream(data);
//获取图片的生成时间
bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
return compressImage(bitmap, time, true);// 压缩好比例大小后再进行质量压缩
}
/**
*
* <质量压缩方法,并且添加时间水印>
* <功能详细描述>
* @param image
* @param date
* @param isNeedTime 是否需要添加水印
* @return
* @see [类、类#方法、类#成员]
*/
public static ByteArrayOutputStream compressImage(Bitmap image, String date, boolean isNeedTime)
{
if (image != null)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitMap = image;
if (isNeedTime)
{
bitMap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.RGB_565);
String time = "拍摄于:" + date;
Canvas canvasTemp = new Canvas(bitMap);
canvasTemp.drawColor(Color.WHITE);
Paint p = new Paint();
String familyName = "宋体";// 设置水印字体
Typeface font = Typeface.create(familyName, Typeface.BOLD);
p.setColor(Color.RED);// 设置水印字体颜色
p.setTypeface(font);
p.setTextSize(30);
canvasTemp.drawBitmap(image, 0, 0, p);
canvasTemp.drawText(time, image.getWidth() / 2 - p.measureText(time) / 2, image.getHeight() * 9 / 10, p);
canvasTemp.save(Canvas.ALL_SAVE_FLAG);
canvasTemp.restore();
image.recycle();
// 添加水印文字
}
bitMap.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length > imageMaxSize)
{ // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset();// 重置baos即清空baos
bitMap.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
options -= 5;// 每次都减少5
}
bitMap.recycle();
return baos;
}
else
{
return null;
}
}
/**
*
* <生成缩略图>
* <功能详细描述>
* @param nowPhoto
* @param zoomToPitmap
* @return
* @see [类、类#方法、类#成员]
*/
public static Bitmap zoomPhoto(Bitmap nowPhoto, Bitmap zoomToPitmap)
{
Bitmap newbmp = ThumbnailUtils.extractThumbnail(nowPhoto, zoomToPitmap.getWidth(), zoomToPitmap.getHeight());
nowPhoto.recycle();
return newbmp;
}
}
public final class GeneralUtils
{
/**
* <将YYYYMMDDHHmmss 转换为 YYYY-MM-DD hh:mm:ss> <功能详细描述>
*
* @param str
* @return
* @see [类、类#方法、类#成员]
*/
public static String splitToSecond(String str)
{
if (isNullOrZeroLenght(str) || str.length() != 14)
{
return str;
}
String strs = "";
strs =
str.substring(0, 4) + "-" + str.substring(4, 6) + "-" + str.substring(6, 8) + " " + str.substring(8, 10)
+ ":" + str.substring(10, 12) + ":" + str.substring(12, 14);
return strs;
}
public static String splitToPhotoTime(String time)
{
String date = "";
if (time != null)
{
String[] sfm = time.split(" ");
if (sfm.length > 1)
{
String[] nyr = sfm[0].split(":");
for (int i = 0; i < nyr.length; i++)
{
date += nyr;
if (i != nyr.length - 1)
date += "-";
}
date += " ";
date += sfm[1];
}
}
return date;
}
}