gpt4 book ai didi

android - 如何在android中将2d int数组转换为位图

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:00 28 4
gpt4 key购买 nike

我需要将二维整数数组 (subSrc) 转换为位图。有什么解决办法吗?

    private Bitmap decimation(Bitmap src){
Bitmap dest = Bitmap.createBitmap(
src.getWidth(), src.getHeight(), src.getConfig());

int bmWidth = src.getWidth();
int bmHeight = src.getHeight();`enter code here`

int[][] subSrc = new int[bmWidth/2][bmWidth/2];
for(int k = 0; k < bmWidth-2; k++){
for(int l = 0; l < bmHeight-2; l++){
subSrc[k][l] = src.getPixel(2*k, 2*l); <---- ??

最佳答案

我找了一个接收二维数组(int[][])并创建一个位图的方法,没有找到,所以我自己写了一个:

public static Bitmap bitmapFromArray(int[][] pixels2d){
int width = pixels2d.length;
int height = pixels2d[0].length;
int[] pixels = new int[width * height];
int pixelsIndex = 0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
pixels[pixelsIndex] = pixels2d[i][j];
pixelsIndex ++;
}
}
return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}

我还写了一个reverse的方法:

public static int[][] arrayFromBitmap(Bitmap source){
int width = source.getWidth();
int height = source.getHeight();
int[][] result = new int[width][height];
int[] pixels = new int[width*height];
source.getPixels(pixels, 0, width, 0, 0, width, height);
int pixelsIndex = 0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
result[i][j] = pixels[pixelsIndex];
pixelsIndex++;
}
}
return result;
}

希望你觉得有用!

关于android - 如何在android中将2d int数组转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12274170/

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