在java中为图片着色 [英] Colorize a picture in java
问题描述
对于一个java项目,我想用阴影和反射来改变头发模型的颜色(改变头发颜色)...其实我想知道有没有一个类可以用RGB代码改变图片的颜色.如果这可以帮助您,这是我需要着色的图片:
I would like, for a java project, to change the color of a hair modelisation (to change hair color) with shadows and reflects... In fact, I wondered if there's a class which can change the color of a picture with a RGB code. If this can help you, here's the picture I need to colorize :
推荐答案
我认为这个问题的目标不是盲目地用某种(固定)颜色替换某些像素,而是真正地染色"图像.一旦我编写了一个示例类,展示了如何做到这一点:
I assume that the question targeted NOT at blindly replacing certain pixels with a certain (fixed) color, but at really "dyeing" the image. Once I wrote a sample class showing how this could be done:
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;
class DyeImage
{
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
new DyeImage();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public DyeImage() throws Exception
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage image = ImageIO.read(new File("DRVpH.png"));
JPanel panel = new JPanel(new GridLayout(1,0));
panel.add(new JLabel(new ImageIcon(image)));
panel.add(new JLabel(new ImageIcon(dye(image, new Color(255,0,0,128)))));
panel.add(new JLabel(new ImageIcon(dye(image, new Color(255,0,0,32)))));
panel.add(new JLabel(new ImageIcon(dye(image, new Color(0,128,0,32)))));
panel.add(new JLabel(new ImageIcon(dye(image, new Color(0,0,255,32)))));
f.getContentPane().add(panel);
f.pack();
f.setVisible(true);
}
private static BufferedImage dye(BufferedImage image, Color color)
{
int w = image.getWidth();
int h = image.getHeight();
BufferedImage dyed = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = dyed.createGraphics();
g.drawImage(image, 0,0, null);
g.setComposite(AlphaComposite.SrcAtop);
g.setColor(color);
g.fillRect(0,0,w,h);
g.dispose();
return dyed;
}
}
给定图像和不同染色颜色的结果如下所示:
The result with the given image and different dyeing colors will look like this:
这篇关于在java中为图片着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!