前言:大家都做过类似图片上传的app,也不免涉及到处理图片问题,在处理图片的时候就可能出现失真的效果,比如在网络不好的情况下大图上传导致的网络卡顿造成体验效果差,还有本身处理图片就要处理IO异常,在开发中也遇到过比较难处理的问题,比如大图,10M左右的长图,全景图。接下来让我们看一些这个这个方法的使用。
public class Compressor {
//max width and height values of the compressed image is taken as 612x816private int maxWidth = 612;
private int maxHeight = 816; //缩放比例,亲测不失真
private Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
private int quality = 90;
private String destinationDirectoryPath;
public Compressor(Context context) {
destinationDirectoryPath = context.getCacheDir().getPath() + File.pathSeparator + "Compressor";
}
public Compressor setMaxWidth(int maxWidth) {
this.maxWidth = maxWidth;
return this;
}
public Compressor setMaxHeight(int maxHeight) {
this.maxHeight = maxHeight;
return this;
}
public Compressor setCompressFormat(Bitmap.CompressFormat compressFormat) {
this.compressFormat = compressFormat;
return this;
}
public Compressor setQuality(int quality) {
this.quality = quality;
return this;
}
public Compressor setDestinationDirectoryPath(String destinationDirectoryPath) {
this.destinationDirectoryPath = destinationDirectoryPath;
return this;
}
public File compressToFile(File imageFile) throws IOException {
return compressToFile(imageFile, imageFile.getName());
}
public File compressToFile(File imageFile, String compressedFileName) throws IOException {
return ImageUtil.compressImage(imageFile, maxWidth, maxHeight, compressFormat, quality,
destinationDirectoryPath + File.pathSeparator + compressedFileName);
}
public Bitmap compressToBitmap(File imageFile) {
return ImageUtil.decodeSampledBitmapFromFile(imageFile, maxWidth, maxHeight);
}
}
//工具类
class ImageUtil {
private ImageUtil() {
}
static File compressImage(File imageFile, int reqWidth, int reqHeight, Bitmap.CompressFormat compressFormat, int quality, String destinationPath) throws IOException {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(destinationPath);
// write the compressed bitmap at the destination specified by destinationPath.
decodeSampledBitmapFromFile(imageFile, reqWidth, reqHeight).compress(compressFormat, quality, fileOutputStream);
} finally {
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}
return new File(destinationPath);
}
static Bitmap decodeSampledBitmapFromFile(File imageFile, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
}
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}
//使用方法,在这里我介绍一下,压缩多张图片,单张图片只传一张也可以,或者也可以循环多次压缩多张图片,各有千秋
private ArrayList<String> mImageList; //未 压缩 图片集合
private File compressedImage;//压缩过后的图片
for (int i = 0; i < mImageList.size(); i++) {
String image = mImageList.get(i);
file = new File(image); //单张图片只需要转一下类型传进来就可以了
try {
compressedImage = new Compressor(SendDynamicActivity.this)
.setCompressFormat(Bitmap.CompressFormat.JPEG)
.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath())
.compressToFile(file);
} catch (IOException e) {
e.printStackTrace();
}
压缩过的图片大概都不超过300K,试过5-10M的大图片,压缩过后不失真,感兴趣的小伙伴可以尝试一下。