java - How to use byte array convert to binary image( 0bits,1bits )? -


i want transfer bytearray binary image don't how it. array value have 0 , 1.

0 = black , 1 = white,

byte [] arr = new byte[32*32]; for(int i=0;i<arr.length;i++){   arr[i]= i%2==0?(byte)0:(byte)1 } 

please me , thanks

it depends on going binary image. if need computation, array may job better, although 2-dimensional array may more convenient use.

if want construct bufferedimage object, can specify 1-bit per pixel type (see below), , fill content using setrgb() method. such image can saved file or shown in gui, or accessed getrgb() method.

here working example (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;              (int y = 0; y < height; y++)                     (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);     } } 

~


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -