【问题标题】:How to show original image and blurred image in JFrame?如何在 JFrame 中显示原始图像和模糊图像?
【发布时间】:2014-10-30 18:55:00
【问题描述】:

我的 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 应该将两个图像并排放置,中间留有空格。

【问题讨论】:

    标签: java jframe bufferedimage blur convolution


    【解决方案1】:
    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);
    
            }
    

    【讨论】:

      猜你喜欢
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多