我正在开发一个图像处理项目,在这个项目中我必须填充破裂绘画的数字化图像。我必须将彩色图像转换为灰度图像,对灰度图像的二维数组执行一些计算并将其写回灰色图像。这个代码是:在java中的灰度图像中写入像素数据
BufferedImage colorImage=ImageIO.read(new File(strImagePath));
BufferedImage image = new BufferedImage(colorImage.getWidth(),colorImage.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(colorImage, 0, 0, null);
g.dispose();
ImageIO.write(image,"PNG",new File("Image.PNG"));
BufferedImage imgOriginal=ImageIO.read(new File("Image.PNG"));
int width=image.getWidth();
int height=image.getHeight();
BufferedImage im=new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
int arrOriginal[][]=new int[height][width];
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
arrOriginal[i][j]=imgOriginal.getRGB(j,i)& 0xFF;
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
im.setRGB(j,i,arrOriginal[i][j]);
ImageIO.write(im,"PNG",new File("Image1.PNG"));
但输出图像很暗得多,我没有得到原始图像回(我没有做任何更改)。
我认为setRGB()语句应该有一些变化,但我不知道是什么。
要写入图像后,我也曾尝试:
`
BufferedImage im = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = im.getRaster();
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
raster.setSample(j,i,0,arrOriginal[i][j]);
`
但是这也没有给我原始图像回来。
任何人都可以提供我解决这个问题? 在此先感谢。
你应该知道firefox和chrome不会渲染相同颜色的png,它也可以来自java渲染引擎。 – Kiwy
@Kiwy我正在使用javax.swing,因为我的项目是一个独立的项目。 –
另请参阅http://stackoverflow.com/questions/3106269/how-to-use-type-byte-gray-to-efficiently-create-a-grayscale-bufferedimage-using – halfbit