gpt4 book ai didi

android - 如何为图像的某些像素设置颜色

转载 作者:行者123 更新时间:2023-11-29 02:01:46 24 4
gpt4 key购买 nike

我在 ImageView 中有一张图片。我想将某些像素设置为红色。我取得了一些进步,但创建的图像已经失去了色彩。

 iv.setImageBitmap(processingBitmap(bitmap));

private Bitmap processingBitmap(Bitmap src){

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

for(int x = 0; x < src.getWidth(); x++){
for(int y = 0; y < src.getHeight(); y++){
int pixelColor = src.getPixel(x, y);
int newPixel= Color.rgb(pixelColor, pixelColor, pixelColor);
dest.setPixel(x, y, newPixel);
}
}

for (int i=5; i<50; i++)
{
dest.setPixel(i, i, Color.rgb(255, 0, 0));
}

return dest;
}

enter image description here

如果我使用 Bitmap dest = src.copy(src.getConfig(), src.isMutable()); 而不是 Bitmap dest = Bitmap.createBitmap(src.getWidth() , src.getHeight(), src.getConfig()); 我收到 iv.setImageBitmap(processingBitmap(bitmap)); 行的错误:

09-06 18:03:37.226: E/AndroidRuntime(811): Caused by: java.lang.IllegalStateException 09-06 18:03:37.226: E/AndroidRuntime(811): at android.graphics.Bitmap.setPixel(Bitmap.java:847)

我也不知道为什么我必须复制粘贴所有像素,然后将这些像素设置为我想要的红色。如果我只使用

 for (int i=5; i<50; i++)
{
dest.setPixel(i, i, Color.rgb(255, 0, 0));
}

我得到一张带有红线的黑色图像。

感谢任何帮助!

最佳答案

Color.rgb() 占用 3 个字节,分别为红色、绿色、蓝色。您正在尝试为它们中的每一个设置像素颜色。最好尝试这样的事情

byte blue = (byte) ((pixelColor & 0xff));
byte green = (byte) (((pixelColor >> 8) & 0xff));
byte red = (byte) (((pixelColor >> 16) & 0xff));
int newPixel= Color.rgb(red , green , blue);

关于android - 如何为图像的某些像素设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12306202/

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