Writing GIF file in Java

So, I have this GIF on my desktop (this is a 52-card deck of poker). I am working on a program that shrinks it down to a small amount of acm.graphics.GImages of each map. Now, however, I want to write these GImages or pixel arrays to a file so I can use them later. I thought it would be as easy as writing .txt files, but a few Google searches later I am more confused than before.

So what should I do about generating .gif files from pixel arrays or GImages (I have a bunch of both)?

+1


source to share


3 answers


Something along these lines should do the trick (modify the image type, dimensions and pixel array size):

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

WritableRaster raster = image.getRaster();
for ( i=0; i<width; i++ ) {
    for (  j=0; j<height; j++ ) {
        int[] colorArray = getColorForPixel(pixels[i][j]);
        raster.setPixel(i, j, colorArray);
    }
}

ImageIO.write(image, "gif", new File("CardImage"));

      



'getColorForPixel' should return an array representing the color for this pixel. In this case, using RGB, the colorArray will have three integers [red] [green] [blue].

Relevant javadoc: WritableRaster , BufferedImage and ImageIO .

+7


source


I needed to create GIF from Java images for a university project and I found this. I would recommend the Acme Open-Source GifEncoder Class. Nice and easy to use, I still remember it over two years later. Here's the link: http://www.acme.com/java/software/Acme.JPM.Encoders.GifEncoder.html



And here's the G-Link: http://www.google.com/search?hl=en&q=acme+java+gif&btnG=Search

+1


source


This doesn't answer your question directly, but isn't it easier to use ImageMagick ? It has Java bindings .

-1


source







All Articles