0

ユーザーがマウスをクリックしてドラッグすることで画像を描画できるWebサイト用のJavaアプレットを作成しています。次に、この画像を1と0の2次元配列の形式で白黒画像に変換します。描画パーツを作成しましたが、アプレットから2次元配列に移動する方法がわかりません。助言がありますか?

4

1 に答える 1

0

最初に行う必要があるのは、画面を に変換するBufferedImageことです。これにより、ピクセル データにアクセスできるようになります。

何かのようなもの...

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

次に、それを白黒画像に変換する必要があります...

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

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

これで、生データにアクセスできます...

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

    }
}

例で更新

これは、前の質問から採用した例です

色

黒と白

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

基本的に、「バッファから直接」アプローチと「ペイント」アプローチを使用して、グラフィックを白黒に変換します。

于 2012-10-16T01:27:54.483 に答える