gpt4 book ai didi

java - 如何在JFrame中显示原始图像和模糊图像?

转载 作者:行者123 更新时间:2023-11-30 11:12:55 26 4
gpt4 key购买 nike

我的 JFrame 中有两个图像。第一个是原始图像,我希望第二个图像模糊。我有两个图像都反射(reflect)在框架中,但第二个没有模糊。我如何才能更好地编写此程序以反射(reflect)模糊图像?

 public static void main(String[] args) throws IOException {
String path = "src/logo.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(0,200);
f.setVisible(true);

String path2 = "src/logo.jpg";
File file2 = new File(path2);
BufferedImage image2 = ImageIO.read(file2);
JLabel label2 = new JLabel(new ImageIcon(image2));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label2);
f.pack();
f.setLocation(200,200);
f.setVisible(true);

float[] matrix = new float[400];
for (int i = 0; i < 400; i++)
matrix[i] = 1.0f/400.0f;

BufferedImageOp op = new ConvolveOp( new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null );
image2 = op.filter(image, null);

}}

另外,我对第二张图片的位置不对,甚至连第一张图片都没有。我的 setLocation 应该让两张图片并排放置,中间留有空格。

最佳答案

  1. 将第一个标签添加到 BorderLayout.WEST 位置,将第二个标签添加到 BorderLayout.EAST 位置
  2. 将模糊操作的结果添加到第二个标签,而不是原始图像。模糊操作创建新图像

例如

public static void main(String[] args) throws IOException {
String path = "src/logo.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label, BorderLayout.WEST);

String path2 = "src/logo.jpg";
File file2 = new File(path2);
BufferedImage image2 = ImageIO.read(file2);

float[] matrix = new float[400];
for (int i = 0; i < 400; i++)
matrix[i] = 1.0f/400.0f;

BufferedImageOp op = new ConvolveOp( new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null );
image2 = op.filter(image, null);
JLabel label2 = new JLabel(new ImageIcon(image2));
f.getContentPane().add(label, BorderLayout.WEST);

f.pack();
f.setVisible(true);

}

关于java - 如何在JFrame中显示原始图像和模糊图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26661152/

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