Image 为什么图像会从顶部剪掉?
代码可以工作,但图像会从顶部修剪下来。我什么都试过了,但还是想不出来 有人能看一下吗? 提前谢谢 代码:Image 为什么图像会从顶部剪掉?,image,bitmapfactory,android-bitmap,bitmapencoder,Image,Bitmapfactory,Android Bitmap,Bitmapencoder,代码可以工作,但图像会从顶部修剪下来。我什么都试过了,但还是想不出来 有人能看一下吗? 提前谢谢 代码: 非常感谢……在CalculatesSampleSize中,您返回的是int。根据图像的比率,此返回值很可能为0,options.inSampleSize将为0,因此不会进行缩放 然后解码将使用您传递的大小值进行解码,因此将发生裁剪。我认为你需要用密度参数来代替。请查看您的计算有多个代码路径。你能描述一下你用的是哪一种吗?目标与源大小相同还是存在缩放?我使用decode_imagePath_S
非常感谢……在CalculatesSampleSize中,您返回的是int。根据图像的比率,此返回值很可能为0,options.inSampleSize将为0,因此不会进行缩放
然后解码将使用您传递的大小值进行解码,因此将发生裁剪。我认为你需要用密度参数来代替。请查看您的计算有多个代码路径。你能描述一下你用的是哪一种吗?目标与源大小相同还是存在缩放?我使用decode_imagePath_String();感谢您图像的大小取决于用户的选择。因此,图像可以是3000px x4000px。将发生OOM。我从其他地方下载了代码,我一直在尝试修改它,但由于某些原因,它只会从顶部修剪,而不是从其他地方修剪。您应该尝试在返回unscaledBitmap时设置断点,并验证inSampleSize是否大于1。非常感谢您的帮助。我对Android还是新手。你能给我一些代码吗?谢谢您应该学习使用调试器。我不是突然说的。掌握调试器是开发人员能够掌握的最好的技能之一。好的,谢谢你的提示。我有时间一定会调查的。关于发展,有很多东西需要学习。
public class ScalingUtilities {
public static Bitmap decodeResource(Resources res, int resId, int ReqWidth, int ReqHeight, ScalingLogic scalingLogic) {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, ReqWidth,
ReqHeight, scalingLogic);
Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options);
return unscaledBitmap;
}
public static Bitmap decode_imagePath_String(String image_path, int ReqWidth, int ReqHeight, ScalingLogic scalingLogic) {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image_path, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, ReqWidth,
ReqHeight, scalingLogic);
Bitmap unscaledBitmap = BitmapFactory.decodeFile(image_path, options);
return unscaledBitmap;
}
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int ReqWidth, int ReqHeight,
ScalingLogic scalingLogic) {
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
ReqWidth, ReqHeight, scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
ReqWidth, ReqHeight, scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(),
Config.RGB_565);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
public static enum ScalingLogic {
CROP, FIT
}
public static int calculateSampleSize(int srcWidth, int srcHeight, int ReqWidth, int ReqHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)ReqWidth / (float)ReqHeight;
if (srcAspect > dstAspect) {
return srcWidth / ReqWidth;
} else {
return srcHeight / ReqHeight;
}
} else {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)ReqWidth / (float)ReqHeight;
if (srcAspect > dstAspect) {
return srcHeight / ReqHeight;
} else {
return srcWidth / ReqWidth;
}
}
}
public static Rect calculateSrcRect(int srcWidth, int srcHeight, int ReqWidth, int ReqHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.CROP) {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)ReqWidth / (float)ReqHeight;
if (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 = (int)(srcHeight - srcRectHeight) / 2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
}
} else {
return new Rect(0, 0, srcWidth, srcHeight);
}
}
public static Rect calculateDstRect(int srcWidth, int srcHeight, int ReqWidth, int ReqHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)ReqWidth / (float)ReqHeight;
if (srcAspect > dstAspect) {
return new Rect(0, 0, ReqWidth, (int)(ReqWidth / srcAspect));
} else {
return new Rect(0, 0, (int)(ReqHeight * srcAspect), ReqHeight);
}
} else {
return new Rect(0, 0, ReqWidth, ReqHeight);
}
}
}