gpt4 book ai didi

java - 为什么 Bitmap.createBitmap() 返回与源位图不同的可变位图?

转载 作者:太空狗 更新时间:2023-10-29 16:21:51 25 4
gpt4 key购买 nike

根据文档 Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) 方法:

Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap. If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.

我有一个将方向应用于现有位图的方法:

private Bitmap getOrientedPhoto(Bitmap bitmap, int orientation) {
int rotate = 0;
switch (orientation) {
case ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ORIENTATION_ROTATE_90:
rotate = 90;
break;
default:
return bitmap;
}

int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

我在这里调用它:

Bitmap tmpPhoto = BitmapFactory.decodeFile(inputPhotoFile.getAbsolutePath(), tmpOpts);
Bitmap orientedPhoto = getOrientedPhoto(tmpPhoto, orientation);

我检查过 tmpPhoto 是不可变的,但 getOrientedPhoto() 仍然返回可变图像,它是 tmpPhoto 的副本。有谁知道如何在不创建新位图对象的情况下使用 Bitmap.createBitmap() 以及我的代码有什么问题?

最佳答案

似乎只是关于此方法的文档不明确。我在 Bitmap crateBitmap() 方法中找到了这段代码:

// check if we can just return our argument unchanged
if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
height == source.getHeight() && (m == null || m.isIdentity())) {
return source;
}

这意味着源位图只有在不可变且不需要转换的情况下才会返回。

关于java - 为什么 Bitmap.createBitmap() 返回与源位图不同的可变位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12511117/

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