Convert 2D binary matrix to black/white image in java

后端 未结 2 493
予麋鹿
予麋鹿 2021-01-13 01:03

I am new for java. I have 2D binary matrix with only 1s and 0s now. I want to save it as jpg image(black and white) with same width and height. How could I realize that? I t

相关标签:
2条回答
  • 2021-01-13 01:40

    This is a simple example that creates a 30x30 checkered box:

    enter image description here

    public static void main(String... args) throws IOException {
        int w = 30, h = 30;
    
        // create the binary mapping
        byte BLACK = (byte)0, WHITE = (byte)255;
        byte[] map = {BLACK, WHITE};
        IndexColorModel icm = new IndexColorModel(1, map.length, map, map, map);
    
        // create checkered data
        int[] data = new int[w*h];
        for(int i=0; i<w; i++)
            for(int j=0; j<h; j++)
                data[i*h + j] = i%4<2 && j%4<2 || i%4>=2 && j%4>=2 ? BLACK:WHITE;
    
        // create image from color model and data
        WritableRaster raster = icm.createCompatibleWritableRaster(w, h);
        raster.setPixels(0, 0, w, h, data);
        BufferedImage bi = new BufferedImage(icm, raster, false, null);
    
        // output to a file
        ImageIO.write(bi, "jpg", new File("C:\\Users\\user\\Desktop\\test.jpg"));
    }
    

    EDIT:

    For what you are doing you actually don't need to create your own ImageColorModel, you can use a built in type: BufferedImage.TYPE_BYTE_GRAY or TYPE_BYTE_BINARY. Here is a better example and shows how to use grayscale to get a checkered box:

    enter image description here

    public static void main(String... args) throws IOException {
        int w = 40, h = 40, divs = 5;
    
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
        WritableRaster raster = bi.getRaster();
    
        for(int i=0; i<w; i++)
            for(int j=0; j<h; j++)
                raster.setSample(i,j,0,128+(int)(127*Math.sin(Math.PI*i/w*divs)*Math.sin(Math.PI*j/h*divs)));
    
        ImageIO.write(bi, "jpg", new File("C:\\Users\\user\\Desktop\\test.jpg"));
    }
    
    0 讨论(0)
  • 2021-01-13 01:53

    JHeatChart does this job as well without you having to create a custom image library.

    http://www.javaheatmap.com/

    // Create some dummy data.
    double[][] data = new double[][]{{3,2,3,4,5,6},
                                     {2,3,4,5,6,7},
                                     {3,4,5,6,7,6},
                                     {4,5,6,7,6,5}};
    
    // Step 1: Create our heat map chart using our data.
    HeatChart map = new HeatChart(data);
    
    // Step 2: Customise the chart.
    map.setTitle("This is my heat chart title");
    map.setXAxisLabel("X Axis");
    map.setYAxisLabel("Y Axis");
    
    // Step 3: Output the chart to a file.
    map.saveToFile(new File("java-heat-chart.png"));
    

    What you are essentially trying to do is make a heat map. And instead of a range of values ranging from 0 to whatever, you have a range of 0 and 1.

    Replace double[][] data = new double[][](//etc); with your boolean array.

    0 讨论(0)
提交回复
热议问题