是否有任何方法或外部库可以使用Lanczos (理想情况下)或至少双三次alg来调整图像大小。在Android下?(当然,越快越好,但质量是第一位的,处理时间是第二位的)
到目前为止,我得到的所有东西是:
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
然而,它使用双线性滤波器,输出质量很差。尤其是如果你想保留细节(比如细线或易读的文本)。
这里有很多很好的Java库,例如:Java - resize image without losing quality
然而,它总是依赖于Java awt类,比如java.awt.image.BufferedImage
,所以它不能在安卓系统中使用。
有没有办法改变Bitmap.createScaledBitmap()
方法或像Morten Nobel's lib这样的库中的默认(双线性)过滤器,这些库可以使用android.graphics.Bitmap
类(或者像@Tron在评论中指出的那样使用一些原始表示)?
发布于 2016-07-02 03:38:42
最有希望的IMO是使用libswscale (来自FFmpeg),它提供了Lanczos和许多其他过滤器。要从本机代码访问Bitmap
buffer,可以使用jnigraphics。这种方法保证了良好的性能和可靠的结果。
编辑
Here你可以找到粗糙的演示应用程序,它使用了所提出的方法。目前的表现糟糕得令人沮丧,因此应该进行调查,以决定我们是否要做些什么来改善它。
发布于 2016-06-20 22:10:54
不幸的是,安卓使用了java中不存在的android.graphics.Bitmap,而java使用了安卓中不存在的java.awt.image.BufferedImage :-(
我没有a ready to use library for android
,但有一条路径可以将java-awt特定的库移植到平台独立的java库中,该库由android和awt/j2se的特定处理程序提供。
在java rescale库中,你必须将所有java-awt特定的类(比如BufferedImage)隐藏在一个接口IBitmap
后面,并为j2se实现该接口,为Android独立实现该接口。
我已经成功地完成了exif/icc/ipc元数据处理,并在j2se pixymeta-j2se-lib/.../j2se/BitmapNative.java和android pixymeta-android-lib/.../android/BitmapNative.java上实现了pixymeta-lib/.../IBitmap.java接口。
所以我有这些包
IBitmap
#en0# lib替换
IBitmap
的
IBitmap
的
发布于 2016-06-20 21:30:00
我最近写了这篇文章来缩放/裁剪图像到特定的分辨率,并以高质量压缩它:
public static void scaleImageToResolution(Context context, File image, int dstWidth, int dstHeight) {
if (dstHeight > 0 && dstWidth > 0 && image != null) {
Bitmap result = null;
try {
//Get Image Properties
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;
//Smaller Image Size in Memory with Config
bmOptions.inPreferredConfig = Bitmap.Config.RGB_565;
//Is resolution not the same like 16:9 == 4:3 then crop otherwise fit
ScalingLogic scalingLogic = getScalingLogic(photoW, photoH,dstWidth, dstHeight);
//Get Maximum automatic downscaling that it's still bigger then this requested resolution
bmOptions.inSampleSize = calculateScalingSampleSize(photoW, photoH, dstWidth, dstHeight, scalingLogic);
//Get unscaled Bitmap
result = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);
//Scale Bitmap to requested Resolution
result = scaleImageToResolution(context, result, scalingLogic);
if (result != null) {
//Save Bitmap with quality
saveImageWithQuality(context, result, image);
}
} finally {
//Clear Memory
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) {
//Do Rectangle of original picture when crop
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
//Do Rectangle to fit in the source rectangle
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
//insert source rectangle into new one
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));
//Recycle the unscaled Bitmap afterwards
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) {
//Horizontal
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 {
//Vertikal
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) {
//Vertikal
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 {
//Horizontal
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) {
//Bild horizontal
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 {
//Bild vertikal
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
}
//Does resolution match
private static boolean isResolutionEqual(float v1, float v2) {
// Falls a 1.999999999999 and b = 2.000000000000
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;
}
它没有使用你提到的库,但它可以工作,我对它很满意。也许你也是。
https://stackoverflow.com/questions/37763257
复制相似问题