将2D二进制矩阵在Java黑/白图像 [英] Convert 2D binary matrix to black/white image in java
问题描述
我是新的Java。我只用1和0,现在2D二进制矩阵。我想将其保存为JPG格式(黑色和白色)与相同的宽度和高度。我怎么能知道呢?我试过code以下,但失败了,说:java.lang.IllegalArgumentException异常:图像== NULL请帮我与,或给我你的更好的解决方案。非常感谢你。
公共静态无效的主要(字串[] args)抛出IOException //结果是双[25] [33]只用1和0的二进制矩阵;
INT高度= result.length;
INT宽度=结果[0]。长度;;
字节[]数据=新的字节[高*宽]
INT K = 0;
的for(int i = 0; I<高度;我++){
对于(INT J = 0; J<宽度; J ++){
数据[K] =(字节)结果[I] [J]。
ķ++;
}
System.out.print(\\ n);
}
输入的InputStream =新ByteArrayInputStream的(数据);
BufferedImage的输出= ImageIO.read(输入);
ImageIO.write(输出中,JPG,新的文件(C:/result.jpg));}
这是创建一个30×30方格盒一个简单的例子:
公共静态无效的主要(字符串参数... args)抛出IOException
INT W = 30,H = 30; //创建的二进制映射
字节BLACK =(字节)0,白色=(字节)255;
字节[] =地图{黑,白};
IndexColorModel的ICM =新IndexColorModel的(1,map.length,地图,地图,地图); //创建格仔数据
INT []数据=新的INT [W * H]。
的for(int i = 0; I<瓦;我++)
对于(INT J = 0; J< 1H; J ++)
数据[I * H + J] =则i%4 2&放大器;&放大器; Ĵ%4 2 ||则i%4是H. = 2和;&放大器; Ĵ%4是H. = 2?黑,白; //创建颜色模型和数据图像
的WritableRaster光栅= icm.createCompatibleWritableRaster(W,H);
raster.setPixels(0,0,W,H,数据);
BufferedImage的双向=新的BufferedImage(ICM,光栅,假,空); //输出到文件
ImageIO.write(BI,JPG,新的文件(C:\\\\ \\\\用户\\\\用户\\\\桌面test.jpg放在));
}
编辑:
有关你在做什么,你其实并不需要创建自己的ImageColorModel,您可以使用内置的类型:BufferedImage.TYPE_BYTE_GRAY或TYPE_BYTE_BINARY。以下是一个更好的例子,并展示了如何使用灰度来获得一个方格框:
公共静态无效的主要(字符串参数... args)抛出IOException
INT W = 40,H = 40的div = 5; BufferedImage的双向=新的BufferedImage(W,H,BufferedImage.TYPE_BYTE_GRAY);
的WritableRaster光栅= bi.getRaster(); 的for(int i = 0; I<瓦;我++)
对于(INT J = 0; J< 1H; J ++)
raster.setSample(I,J,0.128 +(INT)(127 * Math.sin(Math.PI * I / W *的div)* Math.sin(Math.PI *焦耳/ H *的div))); ImageIO.write(BI,JPG,新的文件(C:\\\\ \\\\用户\\\\用户\\\\桌面test.jpg放在));
}
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 tried the code below but failed, saying "java.lang.IllegalArgumentException: image == null!" Please help me with that or give me your better solution. Thank you very much.
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"));
}
This is a simple example that creates a 30x30 checkered box:
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:
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"));
}
这篇关于将2D二进制矩阵在Java黑/白图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!