gpt4 book ai didi

java - 试图缩小 Android 中的位图不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:26 26 4
gpt4 key购买 nike

我正在尝试缩小位图。简而言之,图像最初来自宽度为4016的ByteArray。我使用工厂选项缩小图像后,它仍然报告宽度为4016的图像。

这是我的代码的两个 fragment :

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;
}

更新:

这是我的代码的两个 fragment :

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.outHeightoptions.outWidth在您的代码中可能为零。

调用BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);检查前outHeightoutWidth .

编辑

看看这个来自 Google's Android Dev site 的例子:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;

注意 options.outHeight调用decodeResources(...)后使用.这个你必须在你的代码中修复。

关于java - 试图缩小 Android 中的位图不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21032840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com