如何用二进制图像处理绘制GUI?

时间:2018-12-01 05:58:42

标签: java image swing binary rgb

我正在进行图像处理,但是找不到用二进制图像读取绘制GUI RGB的方法。我陷在paintComponent区域。

我可以读取文件,但不能将RGB值绘制到GUI。有人可以指导我吗?

这是我到目前为止所做的:

private int ws;
private FileInputStream fis;

mybin(){
    try {
        fis = new FileInputStream("mybin.bin");

        String mn = getMagicNumber();
        System.out.println(mn);

        skipWhitespace();

        int width = readNumber();
        System.out.println(width);

        skipWhitespace();

        int height = readNumber();
        System.out.println(height);

        skipWhitespace();
        int maxNum = readNumber();

        System.out.println(maxNum);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch(IOException e2) {}
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(600,600);
    this.setVisible(true);
}
private String getMagicNumber() {
    byte [] magicNum = new byte[2];
    try {
        fis.read(magicNum);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new String(magicNum);
}
private void skipWhitespace() {
    try {
        ws = fis.read();
        while(Character.isWhitespace(ws))
            ws = fis.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private int readNumber() {
    String wstr = "";
    try {
        while(!Character.isWhitespace(ws)) {
            //while(Character.isDigit(ws))
                wstr = wstr + (ws-'0'/*48*/);
                ws = fis.read();
            }
    }catch(IOException e2) {}

    System.out.println(wstr);
    return Integer.parseInt(wstr);
}

class DrawingPanel extends JPanel{
    @Override
    public void paintComponent(Graphics g) {

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

2 个答案:

答案 0 :(得分:0)

绘画BufferedImage可以很简单:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImageFrame extends javax.swing.JFrame {

    public ImageFrame() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(new GraphicsPanel());
        pack();
        setVisible(true);
    }

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

class GraphicsPanel extends JPanel {

    private  BufferedImage image;
    //always use publicly accessible resources when posting mcve
    private final  String imagePath = "https://upload.wikimedia.org/wikipedia/commons/3/3f/Crystal_Project_bug.png";

    GraphicsPanel(){
          try {
              image = ImageIO.read(new URL(imagePath)); //or  image = ImageIO.read(new File(...));
          } catch(final IOException e) {e.printStackTrace(); }
        setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
    }
    @Override
    protected void paintComponent(final Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }
}

答案 1 :(得分:0)

如果您有一个数据结构来保存RGB值并想在屏幕上绘制它们:

首先,您应该首先创建一个image。像这样:

// Create an image, with given dimensions, and RGB palette...
final BufferedImage image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB);
// Paint the RGB values (EG from arrays) to the image
for (int x = 0; x < width; ++x)
  for (int y = 0; y < height; ++y)
  {
    // Convert the R,G,B values to a single int
    final int rgb = r[x,y]*0x10000 + g[x,y]*1x100 + b[x,y];
    // Color the pixel...
    image.setRGB(x, y, rgb);
  }

然后将其显示在您的GUI上。
可以完成此操作,创建一个特殊的组件并进行绘画,请参见c0der的答案。

或者您可以只创建一个Icon,并将其添加到任何JLabel中:

label.setIcon(new ImageIcon(image));