gpt4 book ai didi

java - Graphics2D 如何以灰度绘制颜色?

转载 作者:行者123 更新时间:2023-12-05 07:56:30 25 4
gpt4 key购买 nike

我看到您可以将 BufferedImage 对象转换为灰度。但是,即使您已将 Paint() 设置为非灰色,您能否设置一种模式使您的图形仅以灰度绘制“原始”线条、弧线、矩形等?

我想做的是:我们的绘图方法可以访问模式标志。如果此模式标志为真,则所有内容都应以全彩色绘制。如果它是假的,那么所有的东西都应该用从它们的颜色表示中派生的灰色阴影来绘制。灰色版本应该表示它被禁用。

我们希望避免加载和使用图像,并且出于各种原因更愿意只绘制东西。

最佳答案

用colorMode新建BufferedImage,把所有内容复制给他保存

例子:

BufferedImage grayImg = ImageIO.read(new File(path));
BufferedImage colorImage = new BufferedImage(grayImg .getWidth(), grayImg .getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = colorImage.createGraphics();
graphics.drawImage(grayImg ,0,0,null);
graphics.dispose();
grayImg.flush();
ImageIO.write(colorImage ,"jpg", newPath);
colorImage .flush();

关于java - Graphics2D 如何以灰度绘制颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28635997/

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