使用setRGB后如何在java中显示灰度图像?
我在做一些图像处理程序时遇到了显示灰度图像的问题。我所做的是,我使用 getRGB 提取像素值,然后尝试更改这些像素值。但是在更改这些像素值后,我仍然得到之前缓冲的相同图像。我的代码部分如下: -
int [] rgbArray=new int[width*height];
BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
buffer = ImageIO.read(new File(file));
//Before changing the pixel values
buffer.getRGB(0, 0, width, height, rgbArray , 0,width );
int a = (0xff000000 & rgbArray[0]) >>> 24;
int r = (0x00ff0000 & rgbArray[0]) >> 16;
int g = (0x0000ff00 & rgbArray[0]) >> 8;
int b = (0x000000ff & rgbArray[0]);
System.out.println("a " + a + " r " + r + " g " + g + " b " + b);
System.out.println("rgbArray["+0+"] = "+ rgbArray[0]);
for(int i = 0 ; i<width*height; i++)
{
rgbArray[0]=0x808080; //Changing pixel values to grey
}
buffer.setRGB(0, 0, width, height, rgbArray , 0,width);
//After changing the pixel values
buffer.getRGB(0, 0, width, height, rgbArray , 0,width );
a = (0xff000000 & rgbArray[0]) >>> 24;
r = (0x00ff0000 & rgbArray[0]) >> 16;
g = (0x0000ff00 & rgbArray[0]) >> 8;
b = (0x000000ff & rgbArray[0]);
System.out.println("a " + a + " r " + r + " g " + g + " b " + b);
System.out.println("rgbArray["+0+"] = "+ rgbArray[0]);
像素值的输出对于原始像素和新像素都是正确的,但我无法查看新图像,它显示旧图像,即原始图像显示而不是改变了图像。
M working on some image processing program and encountered a problem of displaying the greyscale image. What i did was, i extracted the pixel values using getRGB and then tried to change these pixel values. But after changing these pixel value i'm still getting the same image which was buffered before. My part of code is given below:-
int [] rgbArray=new int[width*height];
BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
buffer = ImageIO.read(new File(file));
//Before changing the pixel values
buffer.getRGB(0, 0, width, height, rgbArray , 0,width );
int a = (0xff000000 & rgbArray[0]) >>> 24;
int r = (0x00ff0000 & rgbArray[0]) >> 16;
int g = (0x0000ff00 & rgbArray[0]) >> 8;
int b = (0x000000ff & rgbArray[0]);
System.out.println("a " + a + " r " + r + " g " + g + " b " + b);
System.out.println("rgbArray["+0+"] = "+ rgbArray[0]);
for(int i = 0 ; i<width*height; i++)
{
rgbArray[0]=0x808080; //Changing pixel values to grey
}
buffer.setRGB(0, 0, width, height, rgbArray , 0,width);
//After changing the pixel values
buffer.getRGB(0, 0, width, height, rgbArray , 0,width );
a = (0xff000000 & rgbArray[0]) >>> 24;
r = (0x00ff0000 & rgbArray[0]) >> 16;
g = (0x0000ff00 & rgbArray[0]) >> 8;
b = (0x000000ff & rgbArray[0]);
System.out.println("a " + a + " r " + r + " g " + g + " b " + b);
System.out.println("rgbArray["+0+"] = "+ rgbArray[0]);
Output for the pixel value is coming out to be correct for both original and new pixels but i'm not able to view the new image, it is showing the old i.e original image is shown not the changed image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需将第一个像素更改为灰色即可。你可能想要做什么:
这会将每个像素更改为灰色。
// 编辑:
当然,那么每个像素都是完全相同的(灰色),这可以用更少的代码来完成,例如:
如果您想要一张黑白图片而不是彩色图片,请尝试:
there you are only changig the first pixel to gray. what you probably meant to do:
which would change every pixel to gray.
// edit:
Of course, then every pixel is exactly the same (grey), which can be done with way less code, e.g.:
If you want a black and white picture instead of a color one, try: