Skip to content

Instantly share code, notes, and snippets.

@JetXing
Created September 24, 2015 00:58
Show Gist options
  • Save JetXing/a77118615126cd3c61d5 to your computer and use it in GitHub Desktop.
Save JetXing/a77118615126cd3c61d5 to your computer and use it in GitHub Desktop.
加载大图,压缩图片,旋转图片
public final static String PICTRUE_SAVE_PATH = Environment.getExternalStorageDirectory() + "/jet/.photo/";
public static String compressImage(String url) {
String path = PICTRUE_SAVE_PATH + String.valueOf(UUID.randomUUID()) + ".jpg";
try {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
Bitmap image = BitmapFactory.decodeFile(url, newOpts);//此时返回bm为空
newOpts.inJustDecodeBounds = false;
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int height = newOpts.outHeight;
int width = newOpts.outWidth;
int size = 1;
if (height > width) {
if (height > 1280) {
size = height / 1280;
}
} else {
if (width > 1280) {
size = width / 1280;
}
}
newOpts.inSampleSize = size;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
newOpts.inJustDecodeBounds = false;
image = BitmapFactory.decodeFile(url, newOpts);
image = compressImage(image);
// image.recycle();
//图片旋转度数
int degree = ImageUtils.getBitmapDegree(url);
image = rotateBitmapByDegree(image, degree);
File file = new File(PICTRUE_SAVE_PATH);
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream out = new FileOutputStream(path);
image.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
Log.i("zhumingze", "已经保存");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return path;
}
private static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 50;
while (baos.toByteArray().length / 1024 > 200) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset();//重置baos即清空baos
if (options > 10) {
options -= 10;
}
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
if (options == 10) {
break;
}
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
return bitmap;
}
/**
* 读取图片的旋转的角度
*
* @param path 图片绝对路径
* @return 图片的旋转角度
*/
public static int getBitmapDegree(String path) {
int degree = 0;
try {
// 从指定路径下读取图片,并获取其EXIF信息
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 bm 需要旋转的图片
* @param degree 旋转角度
* @return 旋转后的图片
*/
public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
Bitmap returnBm = null;
// 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(degree);
try {
// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
}
if (returnBm == null) {
returnBm = bm;
}
if (bm != returnBm) {
bm.recycle();
}
return returnBm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment