我目前正处于一个项目的中间,在该项目中,必须按下JButton来对灰度,蓝色滤镜,红色滤镜,绿色滤镜进行着色,并淡化猫的图片边缘。对图片进行灰度和彩色过滤很容易,但是我似乎无法弄清楚如何将图像的边缘淡化为黑色。
我的意思是,我有这张需要更换的猫的照片:
我需要将其更改为类似以下内容:
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class GUIKittenPicJC {
public static class KittenChanger extends JPanel {
BufferedImage img = null;
File Kitten = null;
ImageIcon imagetransformed = null;
JButton grayscale = new JButton("Grayscale Image");
JButton rgbB = new JButton("Blue filter this image");
JButton rgbR = new JButton("Red filter this image");
JButton rgbG = new JButton("Green filter this image");
JButton fader = new JButton("Fade this image");
{
try
{
Kitten = new File("C:\\Users\\Jarrod\\Desktop\\Lab 3\\Lab 3\\kitten.bmp");
img = ImageIO.read(Kitten);
}
catch(IOException e)
{
System.out.println(e);
}
ImageIcon image = new ImageIcon(img);
JLabel imageLabel = new JLabel(image);
add(imageLabel);
grayscale.addActionListener(e->{
imagetransformed = new ImageIcon(Grayscale(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
rgbB.addActionListener(e->{
imagetransformed = new ImageIcon(Bluify(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
rgbG.addActionListener(e->{
imagetransformed = new ImageIcon(Greenify(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
rgbR.addActionListener(e->{
imagetransformed = new ImageIcon(Redify(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
add(grayscale);
add(rgbB);
add(rgbG);
add(rgbR);
}
private void imgReset() {
try {
img = ImageIO.read(Kitten);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
public static void main(String[] args)
{
createGUI();
}
private static void createGUI() {
JFrame frame = new JFrame("Kitten Changer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
KittenChanger newContentPane = new KittenChanger();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.setSize(400, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//Grayscale and rgb filter methods follow, not important to question
}
我将如何为这种过滤器创建一种方法,并将其像其他方法一样正确地应用于按钮?
最佳答案
您将必须分别编辑每个像素。此解决方案的主要思想是,您拥有图像的中心,并且每个像素随到中心的距离而变化。
更简单地说,距离中心较远的像素将变暗。
这是您的代码如下所示:
BufferedImage cat; //assuming it is assigned
for(int i = 0; i < cat.getWidth(); i++) { // i is the x coord
for(int j = 0; j < cat.getHeight(); j++) { // j is the y coord
int color = cat.getRGB(i, j);
int r = (color >> 16) & 0xff; //extract red value
int g = (color >> 8) & 0xff;
int b = color & 0xff;
double scale = 0.75; /**** Change this to change the resulting effect ****/
//pixel's distance from center
double dist = Math.sqrt( Math.pow(i - cat.getWidth()/2, 2) + Math.pow(j - cat.getHeight()/2, 2) );
r = (int) Math.max(0, r - dist*scale); //r - dist*scale makes px darker
g = (int) Math.max(0, g - dist*scale); //Math.max makes sure r is always >= 0
b = (int) Math.max(0, b - dist*scale);
int newRGB = (r << 16) + (g << 8) + b; //convert r,g,b to single int
cat.setRGB(i, j, newRGB); //finally, update rgb value
}
}
而且,当我运行这段代码时:
请记住,您始终可以通过更改上面代码中的
scale
变量来更改效果。