【问题标题】:How to use byte array convert to binary image( 0bits,1bits )?如何使用字节数组转换为二进制图像(0位,1位)?
【发布时间】:2015-01-30 03:43:12
【问题描述】:

我想将我的字节数组转换成二进制图像 但我不知道该怎么做。 数组值只有 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
}

请帮帮我,谢谢

【问题讨论】:

    标签: java image binary


    【解决方案1】:

    这取决于您要如何处理该二进制图像。 如果您只需要它来进行计算,那么您的数组可能会更好地为您完成这项工作, 虽然二维数组使用起来可能更方便。

    如果要构造一个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);
        }
    }
    

    ~

    【讨论】:

    • 对不起,我看不懂这行(下)你能解释一下吗,谢谢 "int white = (255
    • 它只是构建完全白色的颜色 - 其中红色=255(23-16 位)、绿色=255(15-8 位)和蓝色=255(7-0 位)。 java.awt.BufferedImage.TYPE_BYTE_BINARY ( [link]docs.oracle.com/javase/7/docs/api/java/awt/image/… ) 的描述说,在 1 位图像的情况下,颜色模型只允许两种颜色 - 0,0,0 和 255,255,255。
    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 2013-07-29
    相关资源
    最近更新 更多