尝试在Android中缩小位图不起作用
我试图缩小一个位图。 简而言之,图像最初来自宽度为4016的ByteArray。在用工厂选项缩小图像后,它仍然报告宽度为4016的图像。
这里是我的代码的两个剪辑:
Bitmap myBitmap = null;
@Override
protected byte[] doInBackground(Object... params) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
if (options.outHeight > options.outWidth) {
options.inSampleSize = calculateInSampleSize(options, 640, 960);
} else {
options.inSampleSize = calculateInSampleSize(options, 960, 640);
}
options.inJustDecodeBounds = false;
//myImageByteArray is 4016 wide
myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);
//This log statement outputs 4016!!! Shouldn't it be smaller since I just decoded the byteArray with options?
Log.d("bitmap", myBitmap.getWidth()+"");
}
public 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) {
// 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;
}
更新:
这里是我的代码的两个剪辑:
Bitmap myBitmap = null;
@Override
protected byte[] doInBackground(Object... params) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
//myImageByteArray is 4016 wide
myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);
if (options.outHeight > options.outWidth) {
options.inSampleSize = calculateInSampleSize(options, 640, 960);
} else {
options.inSampleSize = calculateInSampleSize(options, 960, 640);
}
options.inJustDecodeBounds = false;
//myImageByteArray is 4016 wide
myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);
//This log statement outputs around 1000 now.
Log.d("bitmap", myBitmap.getWidth()+"");
}
public 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) {
// 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;
}
你需要调用.decodeByteArray(..)
两次! 一旦获得宽度和高度,将.inJustDecodeBounds
设置为true
,然后再使用.inSampleSize
获取实际缩放的位图,则代码中的options.outHeight
和options.outWidth
可能为零。
调用BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);
在检查出outHeight
和outWidth
之前。
编辑
看一下谷歌Android Dev站点的这个例子:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
注意在调用decodeResources(...)
之后使用了options.outHeight
。 这你必须修复你的代码。
您没有用设置其“outHeight”和“outWidth”的任何内容填充bitmapOptions。
你应该看看谷歌的位图教程:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
为了决定缩放到什么值,您需要知道与目标大小相比的图像大小。 这就是为什么你需要做两次解码。
顺便说一句,还有另一种缩小图像的方法,正如我在这里展示的 。
链接地址: http://www.djcxy.com/p/18277.html上一篇: Trying to scale down a Bitmap in Android not working
下一篇: Android ActionBar/ActionBarSherlock multiple choices spinner