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

时间:2014-10-30 18:55:00

标签: java jframe bufferedimage blur convolution

我的JFrame中有两张图片。第一个是原版,我希望第二个图像模糊。我有两个图像都反映在帧中,但第二个没有模糊。如何更好地编写此程序以反映模糊图像?

 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 个答案:

答案 0 :(得分:1)

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

    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);
    
            }