如何从android位图中找到像素数组中像素的xy坐标

发布于 2024-11-05 08:34:46 字数 1159 浏览 2 评论 0原文

我有一个使用 Bitmap.getPixels() 方法从位图派生的 int 数组。此方法使用位图中的像素填充数组。当我循环遍历该数组时,如何获取每个像素的 xy 坐标?预先感谢马特。

[更新] 谢谢你的数学。我已经尝试过以下代码。我有一个位图,我已将前 50000 个像素更改为白色。我现在想遍历位图并将所有白色像素更改为红色。 atm 位图顶部只有一条红线穿过白色像素块。你有什么想法吗?多谢。

int length = bgr.getWidth()*bgr.getHeight();
                    int[] pixels = new int[length];
                    bgr.getPixels(pixels,0,bgr.getWidth(),0,0,bgr.getWidth(),bgr.getHeight());
                    for (int i=0;i<50000;i++){
                    // If the bitmap is in ARGB_8888 format

                        pixels[i] = Color.WHITE;//0xffffffff;

                      }

                    bgr.setPixels(pixels,0,bgr.getWidth(),0,0,bgr.getWidth(),bgr.getHeight());




                        int t = 0;
                    int y  = t / bgr.getWidth();
                    int x = t - (y * bgr.getWidth());

                  for( t = 0; t < length; t++){

                      int pixel = bgr.getPixel(x,y);

                      if(pixel == Color.WHITE){

                          bgr.setPixel(x,y,Color.RED);
                          x++;y++;
                      }
                  }

i have an int array derived from a bitmap using the Bitmap.getPixels() method. This method populates the array with the pixels from the Bitmap. How can i get the xy coordinate of each pixel when i loop through that array? thanks in advance Mat.

[update]
Thanks for the math. i've tried the following code. i've a bitmap where i've changed the first 50000 pixels to white. i now want to iterate through the bitmap and change all the white pixels to red. atm there is just one red line through the block of white pixels at the top of the bitmap. have you any ideas? thanks alot.

int length = bgr.getWidth()*bgr.getHeight();
                    int[] pixels = new int[length];
                    bgr.getPixels(pixels,0,bgr.getWidth(),0,0,bgr.getWidth(),bgr.getHeight());
                    for (int i=0;i<50000;i++){
                    // If the bitmap is in ARGB_8888 format

                        pixels[i] = Color.WHITE;//0xffffffff;

                      }

                    bgr.setPixels(pixels,0,bgr.getWidth(),0,0,bgr.getWidth(),bgr.getHeight());




                        int t = 0;
                    int y  = t / bgr.getWidth();
                    int x = t - (y * bgr.getWidth());

                  for( t = 0; t < length; t++){

                      int pixel = bgr.getPixel(x,y);

                      if(pixel == Color.WHITE){

                          bgr.setPixel(x,y,Color.RED);
                          x++;y++;
                      }
                  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-11-12 08:34:46

这是一个代码示例,它可能会执行您所描述的操作。至少我了解你的目标;

int length = bgr.getWidth()*bgr.getHeight();
int[] pixels = new int[length];

bgr.getPixels(pixels,0,bgr.getWidth(),0,0,bgr.getWidth(),bgr.getHeight());

// Change first 50000 pixels to white. You most definitely wanted
// to check i < length too, but leaving it as-is.
for (int i=0;i<50000;i++){
    pixels[i] = Color.WHITE;
}

// I'm a bit confused why this is here as you have pixels[] to do
// modification on. And it would be a bit more efficient way to do all of
// these changes on pixels array before setting them back to bgr.
// But taken this is an experiment with Bitmaps (or homework, hopefully not ;)
// rather good idea actually.
bgr.setPixels(pixels, 0, bgr.getWidth(), 0, 0, bgr.getWidth(), bgr.getHeight());

for (int i=0; i < length; ++i) {
    int y = i / bgr.getWidth();
    int x = i - (y * bgr.getWidth());
    int pixel = bgr.getPixel(x, y);
    if(pixel == Color.WHITE){
        bgr.setPixel(x ,y , Color.RED);
    }
}

Here's a code example, which possibly does what you're describing. As I understood your goal at least;

int length = bgr.getWidth()*bgr.getHeight();
int[] pixels = new int[length];

bgr.getPixels(pixels,0,bgr.getWidth(),0,0,bgr.getWidth(),bgr.getHeight());

// Change first 50000 pixels to white. You most definitely wanted
// to check i < length too, but leaving it as-is.
for (int i=0;i<50000;i++){
    pixels[i] = Color.WHITE;
}

// I'm a bit confused why this is here as you have pixels[] to do
// modification on. And it would be a bit more efficient way to do all of
// these changes on pixels array before setting them back to bgr.
// But taken this is an experiment with Bitmaps (or homework, hopefully not ;)
// rather good idea actually.
bgr.setPixels(pixels, 0, bgr.getWidth(), 0, 0, bgr.getWidth(), bgr.getHeight());

for (int i=0; i < length; ++i) {
    int y = i / bgr.getWidth();
    int x = i - (y * bgr.getWidth());
    int pixel = bgr.getPixel(x, y);
    if(pixel == Color.WHITE){
        bgr.setPixel(x ,y , Color.RED);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文