package com.liuyk.compress;
import java.io.FileInputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.widget.ImageView;
/**
* 压缩ImageView加载大图片到内存,解决OOM
*
* @author admin
*/
public class CompressImage {
public static Bitmap decodeBitmap(String path , int width, int height) {
final Options options = new Options();
//设置Options
options.inJustDecodeBounds = true;//只取大小,不占用内存
options.inSampleSize = computeSampleSize(options, width, height);
options.inJustDecodeBounds = false;//这里一定要将其设置回false,因为之前我们将其设置成了true
options.inDither = false;
options.inTempStorage = new byte[16 * 1024];
Bitmap bitmap = null;
try {
final FileInputStream inputStream = new FileInputStream(path);
final Bitmap bitmap1 = BitmapFactory.decodeFileDescriptor(inputStream.getFD(), null, options);
double scale = getScaling(options.outWidth * options.outHeight,
1024 * 600);
bitmap = Bitmap.createScaledBitmap(bitmap1,
(int)(options.outWidth*scale), (int)(options.outHeight*scale), true);
bitmap1.recycle();
inputStream.close();
} catch (Exception e) {
bitmap = null;
}
return bitmap;
}
public static void decodeBitmap(String path , int targetWidth, int targetHeight, ImageView target) {
Bitmap bitmap = decodeBitmap(path, targetWidth, targetHeight);
target.setImageBitmap(bitmap);
}
private static double getScaling(int src, int des) {
/**
* 48 目标尺寸÷原尺寸 sqrt开方,得出宽高百分比 49
*/
double scale = Math.sqrt((double) des / (double) src);
return scale;
}
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
/**
* 处理某些手机拍摄图片角度旋转问题
*
* @param bitmap
* @param path
* @return
*/
private Bitmap disposeImageRotate(Bitmap bitmap, String path){
final int degree = getImageDegree(path);
if (degree != 0){//旋转图片
bitmap = rotateImage(bitmap, degree);
}
return bitmap;
}
/**
* 得到图片角度
*
* @param path
* @return
*/
private int getImageDegree(String path){
int degree = 0;
try {
final ExifInterface exifInterface = new ExifInterface(path);
final 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;
default:
break;
}
} catch (IOException e) {
return degree;
}
return degree;
}
/**
* 旋转图片
*
* @param bitmap
* @param degree
* @return
*/
private Bitmap rotateImage(Bitmap bitmap, int degree){
if(bitmap == null){
return null;
}
//旋转图片
final Matrix matrix = new Matrix();
matrix.postRotate(degree);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return bitmap;
}
}
android图片压缩工具类
最新推荐文章于 2024-05-11 01:35:16 发布