加速/面具模糊

时间:2017-04-05 13:46:48

标签: java performance swing blur

我正在使用this website中的代码在Java应用程序中模糊我的照片。

问题在于它不够快,无法实时模糊效果。这意味着当模糊区域改变大小时,会出现一个大的黑盒子,同时呈现新的模糊。有没有办法掩盖这个差距,同时它会呈现新的模糊?

示例GUI

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.LayoutManager;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.RadialGradientPaint;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import blur.GaussianFilter;

@SuppressWarnings("serial")
public class GUI extends JFrame {
    private GUI() {
        super("Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());
        Overlay layers = new Overlay();
        add(layers);

        pack();
        setMinimumSize(getSize());

        getContentPane().setBackground(Color.WHITE);
        setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new GUI());
    }

    private class Overlay extends JLayeredPane {
        JPanel base;
        JPanel overlay;
        BufferedImage bi, blurredImage;
        BufferedImageOp op = new GaussianFilter(4);

        private Overlay() {
            addComponentListener(new Resize());
            setPreferredSize(new Dimension(800, 100));

            createBase();
            createOverlay();

            add(base, new Integer(0));
            bi = ComponentPainter.paintComponent(base);
            blurredImage = op.filter(bi, null);
            add(overlay, new Integer(1));
        }

        private void createBase() {
            base = new JPanel();
            base.setLocation(0, 0);
            base.setPreferredSize(new Dimension(800, 100));
            base.setSize(800, 100);
            base.setBackground(Color.WHITE);
            base.setVisible(false);

            base.add(new JLabel("Hello"));
            base.add(new JButton("Hello"));
        }

        private void createOverlay() {
            overlay = new Over(new GridBagLayout());
            overlay.setLocation(0, 0);
            overlay.setPreferredSize(new Dimension(800, 100));
            overlay.setSize(800, 100);

            overlay.add(new JLabel("Hi"));
        }

        private class Resize extends ComponentAdapter {
            @Override
            public void componentResized(ComponentEvent e) {
                base.setSize(new Dimension(getParent().getWidth(), getParent().getHeight()));
                overlay.setSize(new Dimension(getParent().getWidth(), getParent().getHeight()));
                bi = ComponentPainter.paintComponent(base);
                blurredImage = op.filter(bi, null);
            }
        }

        private class Over extends JPanel {
            private Over() {
                super();
            }

            private Over(LayoutManager layout) {
                super(layout);
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);

                Graphics2D g2d = (Graphics2D) g;
                g2d.drawImage(blurredImage, 0, 0, this);

                int alpha = 128;
                Point2D center = new Point2D.Float(getWidth()/2f, getHeight()/2f);
                float radius = (getWidth() / 16F) * 13;
                float[] dist = {0f, 1f};
                Color[] colors = {new Color(245, 251, 255, alpha), new Color(255, 255, 255, alpha)};
                RadialGradientPaint p = new RadialGradientPaint(center, radius,
                        dist, colors,
                        CycleMethod.NO_CYCLE);

                g2d.setPaint(p);
                g2d.fillRect(0, 0, getWidth(), getHeight());
            }
        }

        @Override
        public boolean isOptimizedDrawingEnabled() {
            return true;
        }
    }

    private static class ComponentPainter {
        public static BufferedImage paintComponent(Component c) {
            layoutComponent(c);

            BufferedImage img = new BufferedImage(c.getWidth(), c.getHeight(),
                    BufferedImage.TYPE_INT_RGB);

            c.paint(img.createGraphics());
            return img;
        }

        private static void layoutComponent(Component c) {
            synchronized (c.getTreeLock()) {
                c.doLayout();
                if (c instanceof Container)
                    for (Component child : ((Container) c).getComponents())
                        layoutComponent(child);
            }
        }
    }
}

示例GUI中使用的其他代码:

  1. SO Answer
  2. Blur Download

0 个答案:

没有答案