用java绘制像素图像_用Java在灰度图像中写入像素数据

我正在开发一个有关图像处理的项目,在该项目中,我必须填充破裂绘画的数字化图像。我必须将彩色图像转换为灰度图像,对灰度图像的2D数组执行一些计算,然后将其写回灰度图像。此代码是:

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

for(int j=0;j

arrOriginal[i][j]=imgOriginal.getRGB(j,i)& 0xFF;

for(int i=0;i

for(int j=0;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

for(int j=0;j

raster.setSample(j,i,0,arrOriginal[i][j]);

`

但这也不会给我原始图像。

谁能提供我这个问题的解决方案?提前致谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值