我想将我的bytearray转换为二进制图像 但我不知道怎么做。 数组值只有0和1。
0 =黑色,1 =白色,
byte [] arr = new byte[32*32];
for(int i=0;i<arr.length;i++){
arr[i]= i%2==0?(byte)0:(byte)1
}
请帮助我,谢谢
答案 0 :(得分:0)
这取决于你要对二进制图像做什么。 如果您只需要它来进行计算,那么您的阵列可能会更好地为您完成工作, 虽然二维数组可能更方便使用。
如果要构造BufferedImage对象,可以将其指定为 每像素1位(见下文),并使用setRGB()方法填充其内容。 然后可以将这样的图像保存到文件或在GUI中显示,或者使用getRGB()方法访问。
这是一个工作示例(GenerateChecker.java):
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.io.File;
public class GenerateChecker
{
private static final int width = 32;
private static final int height = 32;
public static void main(String args[]) throws IOException
{
BufferedImage im = new BufferedImage(32, 32, BufferedImage.TYPE_BYTE_BINARY);
int white = (255 << 16) | (255 << 8) | 255;
int black = 0;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
im.setRGB(x, y, (((x + y)&1) == 0) ? black : white);
File outputfile = new File("checker.png");
ImageIO.write(im, "png", outputfile);
}
}
〜