2

我正在尝试使用 Java 进行小型图像处理。用户应该能够加载图像并通过单击按钮对图像进行一些简单的修改。加载和显示图像没有问题,但是当我尝试从中制作二进制图像时, repaint() 方法使我在屏幕上显示为黑色图像。我认为问题出在 repaint() 方法上。我已经使用了搜索功能和谷歌,但我仍然不知道我的代码有什么问题。这就是我到目前为止所拥有的:

public class ImageProcessing extends JFrame implements ActionListener {

    private  JPanel imagePanel;
    private  JPanel buttonPanel;
    private JButton binaryButton;
    private  JButton loadButton;    
    private BufferedImage image;    
    private final String WINDOW_TITLE = "Image Processing";

    public ImageProcessing() {
        createWindow();
    }

    private void createWindow() {
        this.setTitle(WINDOW_TITLE);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
        this.setSize(500, 500);

        imagePanel = new ImagePanel();
        buttonPanel = new JPanel();
        this.add(imagePanel, BorderLayout.CENTER);

        loadButton = new JButton("Load image");
        loadButton.addActionListener(this);
        buttonPanel.add(loadButton);
        this.add(buttonPanel, BorderLayout.SOUTH);

        binaryButton = new JButton("binary");
        binaryButton.addActionListener(this);
        buttonPanel.add(binaryButton);

        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
       if(e.getSource() == this.loadButton) {
           String filePath = getImageFile();
        if (filePath != null) {
          try {
            image = ImageIO.read(new File(filePath));
           // imageBackup = image;
        } catch (IOException e1) {
            e1.printStackTrace();
        }
          this.repaint();
        }
       } else if (e.getSource() == this.binaryButton) {
           image = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
           imagePanel = new ImagePanel();
           this.repaint();
       }
    }

    private String getImageFile() {
        JFileChooser chooser = new JFileChooser();
        int result = chooser.showOpenDialog(null);
        File file = null;
        if (result == JFileChooser.APPROVE_OPTION) {
          file = chooser.getSelectedFile();
          return file.getPath();
        } else
          return null;
    }       

    class ImagePanel extends JPanel {
       public void paint(Graphics g) {
          g.drawImage(image, 0, 0, this);
       }        
    }
}

我希望你能帮助我并解释我做错了什么。提前致谢。

4

2 回答 2

3

目前尚不清楚您要进行哪种图像处理。编码..

image = new BufferedImage(
    image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);

..仅仅用字节二进制类型创建一个新的(空白)图像。你没有把任何东西拉进去。这就是为什么它是黑色的。

要在其中绘制(例如尝试复制原始图像),您可以获得图形上下文:

Graphics2D g = image.createGraphics();

然后复制类似:

g.drawImage(otherImage, 0, 0, this);

我不确定 Java 是否或如何将全深度 RGB 图像转换为TYPE_BYTE_BINARY. 你可能会得到一个例外。

于 2012-12-27T18:46:06.190 回答
1

您正在替换图像面板而不是图像。此外,您没有在二进制图像上执行实际绘画。这是一个如何将原始图像转换为二进制文件的示例,它基于提供的代码:

else if (e.getSource() == this.binaryButton) {
    BufferedImage mask = new BufferedImage(image.getWidth(),
            image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
    Graphics g = mask.getGraphics();
    g.drawImage(image, 0, 0, this);
    g.dispose();
    image = mask;
    this.repaint();
}
于 2012-12-27T18:46:24.833 回答