gpt4 book ai didi

java - 如何高效地将一个Bitmap对象读入一个二维整型数组?

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

我需要将整个 Bitmap 对象读入我的 Android 应用程序中的二维整数数组。

目前,我正在单独读取每个像素,一次一个,如下所示:

for (int y = 0; y < coverImageHeight; y++)
{
for (int x = 0; x < coverImageWidth; x++)
{
coverImageIntArray[x][y] = coverImageBitmap.getPixel(x, y);
}
}

但是,这在大图像上需要很长时间(大约 15 秒)。

有没有一种方法可以一次性完成所有工作以提高效率?

最佳答案

我不熟悉 Android 开发人员,但通常对于图像对象,您可以只获取某个底层缓冲区的引用或副本。这个缓冲区通常是一维的,但你应该能够 covert it相当容易。在你的例子中,为了抓取像素,有一个函数 getPixels看起来很完美。

int[] coverImageIntArray1D = new int[coverImageWidth * coverImageHeight]
coverImageBitmap.getPixels(coverImageIntArray1D, 0, coverImageWidth,
0, 0, coverImageWidth, coverImageHeight)
// coverImageIntArray1D should now contain the entire image's pixels

仅供引用,您可以使用二维索引对这种类型的一维数组进行索引:

int pixel = coverImageIntArray1D[x + y*coverImageWidth]

这将为您提供 [x][y] 处的像素。因此,您仍然可以以二维方式使用它,而无需执行低效的转换。

关于java - 如何高效地将一个Bitmap对象读入一个二维整型数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43419458/

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