Java 在Android中使用createScaledBitmap创建缩放位图
我想创建一个缩放位图,但我似乎得到了一个不成比例的图像。它看起来像个正方形,而我想变成长方形 我的代码:Java 在Android中使用createScaledBitmap创建缩放位图,java,android,bitmap,Java,Android,Bitmap,我想创建一个缩放位图,但我似乎得到了一个不成比例的图像。它看起来像个正方形,而我想变成长方形 我的代码: Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 960, 960, false); 我希望图像的最大值为960。我该怎么做?将宽度设置为null不会编译。这可能很简单,但我不能全神贯注。谢谢您的图像是方形的,因为您正在设置width=960和height=960 您需要创建一种方法,通过该方法传递所需图像的大小,如下所示
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 960, 960, false);
我希望图像的最大值为960。我该怎么做?将宽度设置为
null
不会编译。这可能很简单,但我不能全神贯注。谢谢您的图像是方形的,因为您正在设置width=960
和height=960
您需要创建一种方法,通过该方法传递所需图像的大小,如下所示:
在代码中,这看起来像:
public static Bitmap lessResolution (String filePath, int width, int height) {
int reqHeight = height;
int reqWidth = width;
BitmapFactory.Options options = new BitmapFactory.Options();
// First decode with inJustDecodeBounds=true to check dimensions
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
公共静态位图解决方案(字符串文件路径、整型宽度、整型高度){
int REQHEIGH=高度;
宽度=宽度;
BitmapFactory.Options=new-BitmapFactory.Options();
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
options.inJustDecodeBounds=true;
解码文件(文件路径,选项);
//计算样本大小
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeFile(文件路径,选项);
}
私有静态int-calculateInSampleSize(BitmapFactory.Options选项、int-reqWidth、int-reqHeight){
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
//计算高度和宽度与所需高度和宽度的比率
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
//选择最小比率作为采样值,这将保证
//最终图像的两个尺寸均大于或等于
//要求的高度和宽度。
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
如果您的内存中已经有原始位图,则不需要执行inJustDecodeBounds
、inSampleSize
等整个过程。您只需要确定使用的比例并相应地进行缩放
final int maxSize = 960;
int outWidth;
int outHeight;
int inWidth = myBitmap.getWidth();
int inHeight = myBitmap.getHeight();
if(inWidth > inHeight){
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, outWidth, outHeight, false);
如果此图像的唯一用途是缩放版本,最好使用Tobiel的答案,以最小化内存使用。Woah!感谢您的代码,但要确保图像保持其比例,还有很多工作要做。我希望能传递一些神奇的参数。谢谢你的代码片段!问题是,你可以得到OOM的原因有很多,用这段代码你可以阻止它。这是关于位图的android文档:OOM?还有,你觉得这里的答案怎么样?我很感激你的建议:OOM的意思是“内存不足”,这将停止你的应用程序,因为你超出了允许的内存量。我的建议是尝试你认为对你有效的解决方案。在回答中,不要尝试标记为解决方案的图像,因为他们正在从Base64字符串解码图像。请注意,此方法不会提供我们想要的确切大小,而是提供一个大小可以按2次方缩小的图像(如果您有一个10kx10k图像,并要求使用5001x5001图像,您将获得一个10kx10k图像).你能详细说明你的评论吗?“如果此图像的唯一用途是缩放版本,最好使用Tobiel的答案”?我的意思是,如果仅使用缩放版本,请使用其他答案。如果你只是这样使用它,效率会更高。如果你已经在加载/显示它的完整版本,这个方法很好,因为你无论如何都需要内存。我用这个方法摆脱了内存。我相信写
outHeight=inHeight*(向外/向内)代码>而不是outHeight=(inHeight*maxSize)/inWidth代码>将更易于阅读/理解
bmpimg = Bitmap.createScaledBitmap(srcimg, 100, 50, true);