Skip to main content

Writing One Dimensional Array as Image ( Java )

Welcome back ,I'm still writing the application from the last post, I founded that I must redraw the image to show the result , I faced Many problems while writing the Method write Image first the image was totally black which is not correct , I searched the web for a solution especially stack overflow site , I'm gonna tell you what I gain , I hope the post help you in shaa Allah .





public void write_image(int[] image_1d, int width, int height) throws IOException {   
        WritableRaster raster= Raster.createWritableRaster
        (new PixelInterleavedSampleModel(0, width,height, 1,
         1920,new int[] {0}), new Point(0,0));

        int k =0 ;
        for(int i = 0 ; i < width ; i++ )
         {
         for(int j = 0 ; j < height ; j++)
         {
             raster.setSample(i,j,0,image_1d[k]);
             k++;
         }
         }
        BufferedImage image =new BufferedImage(width,height,
                                 BufferedImage.TYPE_BYTE_BINARY);
        
        image.setData(raster);
        File output=new File("ImageName.jpg");
        ImageIO.write(image,"jpg",output);
    }


Please share if you like.
Thanks for reading my blog post. 

Ahmed Ghazey.


Comments