我最近写了一个程序将图像缩放/裁剪到特定分辨率并进行压缩处理:
public static void scaleImageToResolution(Context context, File image, int dstWidth, int dstHeight) {
if (dstHeight > 0 && dstWidth > 0 && image != null) {
Bitmap result = null;
try {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);
int photoH = bmOptions.outHeight;
int photoW = bmOptions.outWidth;
bmOptions.inJustDecodeBounds = false;
bmOptions.inPurgeable = true;
bmOptions.inPreferredConfig = Bitmap.Config.RGB_565;
ScalingLogic scalingLogic = getScalingLogic(photoW, photoH,dstWidth, dstHeight);
bmOptions.inSampleSize = calculateScalingSampleSize(photoW, photoH, dstWidth, dstHeight, scalingLogic);
result = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);
result = scaleImageToResolution(context, result, scalingLogic);
if (result != null) {
saveImageWithQuality(context, result, image);
}
} finally {
if (result != null)
result.recycle();
}
}
}
public static void saveImageWithQuality(Bitmap bitmap, String path, int compressQuality) {
try {
FileOutputStream fOut;
fOut = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, fOut);
fOut.flush();
fOut.close();
} catch (IOException ex) {
if (Logger.getRootLogger() != null)
Logger.getRootLogger().error(ex);
else
Log.e("saveImageWithQuality", "Error while saving compressed Picture: " + ex.getMessage() + StringUtils.newLine() + ex.getStackTrace().toString());
}
}
public static void saveImageWithQuality(Context context, Bitmap bitmap, File file) {
saveImageWithQuality(bitmap, file.getAbsolutePath(), getCompressQuality());
}
public static void saveImageWithQuality(Context context, Bitmap bitmap, String path) {
saveImageWithQuality(bitmap, path, getCompressQuality());
}
private static int calculateScalingSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect) {
return srcWidth / dstWidth;
} else {
return srcHeight / dstHeight;
}
} else {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect) {
return srcHeight / dstHeight;
} else {
return srcWidth / dstWidth;
}
}
}
private static Bitmap scaleImageToResolution(Context context, Bitmap unscaledBitmap, ScalingLogic scalingLogic, int dstWidth, int dstHeight) {
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
unscaledBitmap.recycle();
return scaledBitmap;
}
private static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.CROP) {
if (srcWidth >= srcHeight) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect < dstAspect || isResolutionEqual(srcAspect, dstAspect)) {
final int srcRectHeight = (int) (srcWidth / dstAspect);
final int scrRectTop = (srcHeight - srcRectHeight) / 2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
} else {
final int srcRectWidth = (int) (srcHeight * dstAspect);
final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight);
}
} else {
final float srcAspect = (float) srcHeight / (float) srcWidth;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect < dstAspect || isResolutionEqual(srcAspect, dstAspect)) {
final int srcRectWidth = (int) (srcHeight / dstAspect);
final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight);
} else {
final int srcRectHeight = (int) (srcWidth * dstAspect);
final int scrRectTop = (srcHeight - srcRectHeight) / 2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
}
}
} else {
return new Rect(0, 0, srcWidth, srcHeight);
}
}
private static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
if (srcWidth > srcHeight) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect < dstAspect || isResolutionEqual(srcAspect, dstAspect)) {
return new Rect(0, 0, (int) (dstHeight * srcAspect), dstHeight);
} else {
return new Rect(0, 0, dstWidth, (int) (dstWidth / srcAspect));
}
} else {
final float srcAspect = (float) srcHeight / (float) srcWidth;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect < dstAspect || isResolutionEqual(srcAspect, dstAspect)) {
return new Rect(0, 0, (int) (dstHeight / srcAspect), dstHeight);
} else {
return new Rect(0, 0, dstWidth, (int) (dstWidth * srcAspect));
}
}
} else {
if (srcWidth >= srcHeight)
return new Rect(0, 0, dstWidth, dstHeight);
else
return new Rect(0, 0, dstHeight, dstWidth);
}
}
private static ScalingLogic getScalingLogic(int imageWidth, int imageHeight, int dstResolutionWidth, int dstResolutionHeight) {
if (imageWidth >= imageHeight) {
final float srcAspect = (float) imageWidth / (float) imageHeight;
final float dstAspect = (float) dstResolutionWidth / (float) dstResolutionHeight;
if (!isResolutionEqual(srcAspect, dstAspect)) {
return ScalingLogic.CROP;
} else {
return ScalingLogic.FIT;
}
} else {
final float srcAspect = (float) imageHeight / (float) imageWidth;
final float dstAspect = (float) dstResolutionWidth / (float) dstResolutionHeight;
if (!isResolutionEqual(srcAspect, dstAspect)) {
return ScalingLogic.CROP;
} else {
return ScalingLogic.FIT;
}
}
}
public enum PictureQuality {
High,
Medium,
Low
}
public enum ScalingLogic {
CROP,
FIT
}
private static boolean isResolutionEqual(float v1, float v2) {
return v1 == v2 || Math.abs(v1 - v2) / Math.max(Math.abs(v1), Math.abs(v2)) < 0.01;
}
public int getCompressQuality() {
if (Quality == PictureQuality.High)
return 100;
else if (Quality == PictureQuality.Medium)
return 50;
else if (Quality == PictureQuality.Low)
return 25;
else return 0;
}
它没有使用你提到的库,但它可以工作,我很满意。也许你也会喜欢。
Bitmap
使用Paint
: https://developer.android.com/reference/android/graphics/Paint (查找FILTER_BITMAP_FLAG
- 启用缩放位图的双线性采样的 Paint 标志。) - Jolinar