Convert 2D array to Image in Java? -


I have a 2D array (this is only 1 -0 value), but its type is int [] [].

Now I want to convert that array into an image (binary image black and white). But I did not find the appropriate answer for my question. I have searched Google and this website can someone help me ??

I used the following code, but it

  string path = "c: \\ user \\ cyrus \\ desktop \\ test .jpg"; BufferedImage image = new BufferedImage (b.length, b [0]. Length, BufferedImage.TYPE_INT_RGB); For (int x = 0; x & lt; b.length; x ++) {// b for my 2D array (int y = 0; y & lt; b [x] length; y ++ ) {Image.setRGB (x, y, b [x] [y]); }} File FileFile = New File (Path); Try {imageIO.write (image, "jpg", ImageFile); } Hold (IOException e) {e.printStackTrace (); }   

// after modifying my code

  string path = "c: \\ user \\ cyrus \\ desktop \\ test. jpg "; BufferedImage image = new BufferedImage (a.length, a [0]. Length, BufferedImage.TYPE_INT_RGB); For (int x = 0; x & lt; a.length; x ++) {for (int y = 0; y & lt; a [x] length; y ++) {int value; If (a [x] [y] == 1) value = new color (255,255,255) .getRGB (); Other values ​​= new color (0,0,0) .getRGB (); Image.setRGB (x, y, value); }} File FileFile = New File (Path); Try {imageIO.write (image, "jpg", ImageFile); } Hold (IOException e) {e.printStackTrace (); }   

but return the wrong image

Can you get specific results? Did it throw some exception? Or the program ended normally, but the results you did not achieve?

You should note that here you want to work with a binary image, then what is the reason for choosing a GPG? As I know, JPG color triplet is not a native representation of RGB (for example, as BMP). A JPEG file is a "container" of the data sequence, which has a lot of information in its title (XIF, QT, ...), marked by marker. To deal with the "straight" pixel value as your array, the data sequence should be "decode" in the JPG file, after which you will play this image, the main image data will be "encoded" again to the JPG stream.

To play directly with pixel value, I suggest that you work with other formats (like TGA, PNG, ...)

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -