java中二维二进制矩阵到黑白图像的转换

java中二维二进制矩阵到黑白图像的转换,java,image,matrix,binary,Java,Image,Matrix,Binary,我是java新手。我有一个二维二进制矩阵,现在只有1和0。我想保存为jpg图像(黑色和白色),具有相同的宽度和高度。我怎么会意识到这一点?我尝试了下面的代码,但失败了,说“java.lang.IllegalArgumentException:image==null!”请帮助我,或者给我更好的解决方案。多谢各位 public static void main(String[] args) throws IOException { //result is double[25][33] bi

我是java新手。我有一个二维二进制矩阵,现在只有1和0。我想保存为jpg图像(黑色和白色),具有相同的宽度和高度。我怎么会意识到这一点?我尝试了下面的代码,但失败了,说“java.lang.IllegalArgumentException:image==null!”请帮助我,或者给我更好的解决方案。多谢各位

public static void main(String[] args) throws IOException {

    //result is double[25][33] binary matrix with only 1s and 0s;
    int height=result.length;
    int width=result[0].length;;
    byte[] data = new byte[height*width];
    int k=0;
    for(int i = 0;i < height;i++){
        for(int j = 0; j < width; j++){
            data[k]=(byte)result[i][j];
            k++;
        }
        System.out.print("\n");
    }
    InputStream input = new ByteArrayInputStream(data);
    BufferedImage output = ImageIO.read(input);
    ImageIO.write(ouput, "jpg", new File("c:/result.jpg"));

}
publicstaticvoidmain(字符串[]args)引发IOException{
//结果是只有1和0的双[25][33]二元矩阵;
int height=result.length;
int width=结果[0]。长度;;
字节[]数据=新字节[高度*宽度];
int k=0;
对于(int i=0;i
这是一个创建30x30方格框的简单示例:

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"));
}
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"));
}

publicstaticvoidmain(String…args)抛出IOException{
int w=30,h=30;
//创建二进制映射
字节黑色=(字节)0,白色=(字节)255;
字节[]映射={黑,白};
IndexColorModel icm=新的IndexColorModel(1,map.length,map,map,map);
//创建棋盘格数据
int[]数据=新的int[w*h];

for(int i=0;i
JHeatChart
也可以执行此任务,而无需创建自定义图像库

基本上你要做的是做一个热图,而不是一个从0到任何值的范围,而是一个0到1的范围


用您的布尔数组替换
double[][]data=new double[][](//等);
“我怎么会意识到这一点?”我怀疑一种方法是将每个像素绘制到a并保存它。但不能作为JPG,因为JPG不能准确地保留颜色。如果有任何疑问,请发布一个(只需要一些导入,并将其包装到
类中
)。谢谢您的评论,但如果我想将矩阵输出到图像范围0到255,该怎么办?这样写对吗:byte[]map=new byte[256];for(int I=0;I)
// 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"));