/**
* 图片按比例大小压缩方法
*
* @param srcPath (根据路径获取图片并压缩)
* @return
*/
public static File getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 开始读入图片,此时把options.inJustDecodeBounds 设回true了,只读取图片的大小,不分配内存
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// 现在主流手机比较多是1920*1080分辨率,所以高和宽我们设置为
float hh = 1920f;// 这里设置高度为1920f
float ww = 1080f;// 这里设置宽度为1080f
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;// be=1表示不缩放
if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// 设置缩放比例
// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
//检测并旋转图片
Bitmap bitmap1=changeImageLocate(srcPath,bitmap);
return compressImage(bitmap1);// 压缩好比例大小后再进行质量压缩
}
//检测并旋转图片
public static Bitmap changeImageLocate(String filepath, Bitmap bitmap) {
//根据图片的filepath获取到一个ExifInterface的对象
int degree = 0;
try {
ExifInterface exif = new ExifInterface(filepath);
if (exif != null) {
// 读取图片中相机方向信息
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.e("degree========ori====", ori + "");
// 计算旋转角度
switch (ori) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
default:
degree = 0;
break;
}
Log.e("degree============", degree + "");
if (degree != 0) {
Log.e("degree============", "degree != 0");
// 旋转图片
Matrix m = new Matrix();
// m.setScale(0.5f,0.5f);
m.postRotate(degree);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
return bitmap;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
//质量压缩
public static File compressImage(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 90;
while (baos.toByteArray().length / 1024 > 300) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset(); // 重置baos即清空baos
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
options -= 10;// 每次都减少10
}
File dirFile = new File(Environment.getExternalStorageDirectory() + "保存图片的路径");
if (!dirFile.exists()) {
dirFile.mkdirs();
}
File file = new File(dirFile.getPath(), System.currentTimeMillis() + ".jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
try {
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
recycleBitmap(bitmap);
return file;
}
//回收Bitmap
public static void recycleBitmap(Bitmap... bitmaps) {
if (bitmaps == null) {
return;
}
for (Bitmap bm : bitmaps) {
if (null != bm && !bm.isRecycled()) {
bm.recycle();
}
}
}
只需直接调用getimage(srcPath)即可。