作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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;
}
如果我使用 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/
我是一名优秀的程序员,十分优秀!