将java applet转换为黑白图像

时间:2021-04-14 00:24:01

Im creating a Java applet for my website where users can draw an image by clicking and dragging the mouse. I then want to convert this image to a black and white image, in the form of a two dimensional array with 1's and 0's. I have created the drawing part but I'm not sure how to go from the applet to the two dimensional array. Any suggestions?

我为我的网站创建了一个Java小程序,用户可以通过单击并拖动鼠标来绘制图像。然后我想将这个图像转换为黑白图像,形式为二维数组,1和0。我创建了绘图部分,但我不确定如何从applet转到二维数组。有什么建议?

1 个解决方案

#1


0  

The first thing you need to do is convert the screen to a BufferedImage, this will allow you access to the pixel data.

您需要做的第一件事是将屏幕转换为BufferedImage,这将允许您访问像素数据。

Something like...

BufferedImage buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = buffer.createGraphics();
printAll(g2d);
g2d.dispose();

Next, you need to convert that to a black and white image...

接下来,您需要将其转换为黑白图像...

BufferedImage bwImage = new BufferedImage(
        buffer .getWidth(),
        buffer .getHeight(),
        BufferedImage.TYPE_BYTE_BINARY);

Graphics2D g2d = bwImage.createGraphics();
g2d.drawImage(buffer , 0, 0, null);
g2d.dispose();

Now, you can access the raw data...

现在,您可以访问原始数据......

for (int y = 0; y < bwImage.getHeight(); y++) {
    for (int x = 0; x < bwImage.getWidth(); x++) {

        Color color = new Color(bwImage.getRGB(x, y));
        // You should only be interested in colors of 0,0,0 RGB

    }
}

UPDATED with Example

用例子更新

This is an example I adopted from a previous question

这是我从前一个问题中采用的一个例子

将java applet转换为黑白图像

将java applet转换为黑白图像

public class MyPaint {

    public static void main(String[] args) {
        new MyPaint();
    }

    public MyPaint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(500, 600);
                frame.getContentPane().setBackground(Color.DARK_GRAY);
                frame.setLocationRelativeTo(null);
                frame.setLayout(new GridBagLayout());

                final PaintSurfacePane surface = new PaintSurfacePane();
                JPanel pnlButtons = new JPanel(new GridBagLayout());
                pnlButtons.setOpaque(false);

                JButton saveFromImage = new JButton("Save From Image");
                JButton saveFromGraphics = new JButton("Save From Grpahics");

                pnlButtons.add(saveFromImage);
                pnlButtons.add(saveFromGraphics);

                saveFromGraphics.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        BufferedImage buffer = new BufferedImage(surface.getWidth(), surface.getHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics2D g2d = buffer.createGraphics();
                        surface.printAll(g2d);
                        g2d.dispose();

                        renderBlackWhite(buffer);
                    }

                });

                saveFromImage.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        BufferedImage buffer = new BufferedImage(surface.getWidth(), surface.getHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics2D g2d = buffer.createGraphics();
                        g2d.drawImage(surface.getPaintSurface(), 0, 0, surface);
                        g2d.dispose();

                        renderBlackWhite(buffer);
                    }

                });

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.weightx = 1;
                gbc.weighty = 1;

                frame.add(surface, gbc);

                gbc.gridy++;
                gbc.weighty = 0;
                frame.add(pnlButtons, gbc);

                frame.setVisible(true);
            }
        });
    }

    public static void renderBlackWhite(BufferedImage buffer) {
        BufferedImage bwImage = new BufferedImage(
                        buffer.getWidth(),
                        buffer.getHeight(),
                        BufferedImage.TYPE_BYTE_BINARY);

        Graphics2D g2d = bwImage.createGraphics();
        g2d.drawImage(buffer, 0, 0, null);
        g2d.dispose();

        JFrame frame = new JFrame("Black and White");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new JLabel(new ImageIcon(bwImage)));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public class PaintSurfacePane extends JPanel {

        private BufferedImage paintSurface;

        public PaintSurfacePane() {    
            // This will produce a buffered image that will render faster...
            GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
            paintSurface = gc.createCompatibleImage(400, 400, Transparency.OPAQUE);
            Graphics2D g2d = paintSurface.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, 400, 400);
            g2d.dispose();

            MouseHandler handler = new MouseHandler();
            addMouseListener(handler);
            addMouseMotionListener(handler);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(paintSurface, 0, 0, this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        @Override
        public Dimension getMaximumSize() {
            return getPreferredSize();
        }

        public BufferedImage getPaintSurface() {
            return paintSurface;
        }

        protected class MouseHandler extends MouseAdapter implements MouseMotionListener {

            private Point startPoint;

            @Override
            public void mouseReleased(MouseEvent e) {
                startPoint = null;
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                Point endPoint = e.getPoint();
                if (startPoint != null) {
                    Graphics2D g2d = paintSurface.createGraphics();
                    g2d.setColor(Color.RED);
                    g2d.draw(new Line2D.Float(startPoint, endPoint));
                    g2d.dispose();

                    repaint();
                }

                startPoint = endPoint;
            }

            @Override
            public void mouseMoved(MouseEvent e) {
            }
        }
    }
}

Essentially it uses the "direct from buffer" approach and the "paint" approach to convert the graphics to black and white.

基本上它使用“直接从缓冲区”方法和“绘画”方法将图形转换为黑白。

#1


0  

The first thing you need to do is convert the screen to a BufferedImage, this will allow you access to the pixel data.

您需要做的第一件事是将屏幕转换为BufferedImage,这将允许您访问像素数据。

Something like...

BufferedImage buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = buffer.createGraphics();
printAll(g2d);
g2d.dispose();

Next, you need to convert that to a black and white image...

接下来,您需要将其转换为黑白图像...

BufferedImage bwImage = new BufferedImage(
        buffer .getWidth(),
        buffer .getHeight(),
        BufferedImage.TYPE_BYTE_BINARY);

Graphics2D g2d = bwImage.createGraphics();
g2d.drawImage(buffer , 0, 0, null);
g2d.dispose();

Now, you can access the raw data...

现在,您可以访问原始数据......

for (int y = 0; y < bwImage.getHeight(); y++) {
    for (int x = 0; x < bwImage.getWidth(); x++) {

        Color color = new Color(bwImage.getRGB(x, y));
        // You should only be interested in colors of 0,0,0 RGB

    }
}

UPDATED with Example

用例子更新

This is an example I adopted from a previous question

这是我从前一个问题中采用的一个例子

将java applet转换为黑白图像

将java applet转换为黑白图像

public class MyPaint {

    public static void main(String[] args) {
        new MyPaint();
    }

    public MyPaint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(500, 600);
                frame.getContentPane().setBackground(Color.DARK_GRAY);
                frame.setLocationRelativeTo(null);
                frame.setLayout(new GridBagLayout());

                final PaintSurfacePane surface = new PaintSurfacePane();
                JPanel pnlButtons = new JPanel(new GridBagLayout());
                pnlButtons.setOpaque(false);

                JButton saveFromImage = new JButton("Save From Image");
                JButton saveFromGraphics = new JButton("Save From Grpahics");

                pnlButtons.add(saveFromImage);
                pnlButtons.add(saveFromGraphics);

                saveFromGraphics.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        BufferedImage buffer = new BufferedImage(surface.getWidth(), surface.getHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics2D g2d = buffer.createGraphics();
                        surface.printAll(g2d);
                        g2d.dispose();

                        renderBlackWhite(buffer);
                    }

                });

                saveFromImage.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        BufferedImage buffer = new BufferedImage(surface.getWidth(), surface.getHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics2D g2d = buffer.createGraphics();
                        g2d.drawImage(surface.getPaintSurface(), 0, 0, surface);
                        g2d.dispose();

                        renderBlackWhite(buffer);
                    }

                });

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.weightx = 1;
                gbc.weighty = 1;

                frame.add(surface, gbc);

                gbc.gridy++;
                gbc.weighty = 0;
                frame.add(pnlButtons, gbc);

                frame.setVisible(true);
            }
        });
    }

    public static void renderBlackWhite(BufferedImage buffer) {
        BufferedImage bwImage = new BufferedImage(
                        buffer.getWidth(),
                        buffer.getHeight(),
                        BufferedImage.TYPE_BYTE_BINARY);

        Graphics2D g2d = bwImage.createGraphics();
        g2d.drawImage(buffer, 0, 0, null);
        g2d.dispose();

        JFrame frame = new JFrame("Black and White");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new JLabel(new ImageIcon(bwImage)));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public class PaintSurfacePane extends JPanel {

        private BufferedImage paintSurface;

        public PaintSurfacePane() {    
            // This will produce a buffered image that will render faster...
            GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
            paintSurface = gc.createCompatibleImage(400, 400, Transparency.OPAQUE);
            Graphics2D g2d = paintSurface.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, 400, 400);
            g2d.dispose();

            MouseHandler handler = new MouseHandler();
            addMouseListener(handler);
            addMouseMotionListener(handler);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(paintSurface, 0, 0, this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        @Override
        public Dimension getMaximumSize() {
            return getPreferredSize();
        }

        public BufferedImage getPaintSurface() {
            return paintSurface;
        }

        protected class MouseHandler extends MouseAdapter implements MouseMotionListener {

            private Point startPoint;

            @Override
            public void mouseReleased(MouseEvent e) {
                startPoint = null;
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                Point endPoint = e.getPoint();
                if (startPoint != null) {
                    Graphics2D g2d = paintSurface.createGraphics();
                    g2d.setColor(Color.RED);
                    g2d.draw(new Line2D.Float(startPoint, endPoint));
                    g2d.dispose();

                    repaint();
                }

                startPoint = endPoint;
            }

            @Override
            public void mouseMoved(MouseEvent e) {
            }
        }
    }
}

Essentially it uses the "direct from buffer" approach and the "paint" approach to convert the graphics to black and white.

基本上它使用“直接从缓冲区”方法和“绘画”方法将图形转换为黑白。