babz said:
AoA ..
does any one know how to create PNG images in Palette mode in java??
PNGQUANT is a software wriiten in C which does the conversion. But i
need java code for that..
Any ideas???
Thanks
Babz
It is rather straight-forward in java. Create a BufferedImage with an
IndexColorModel, and write it to a PNG file using javax.imageio.
The example below writes a simple PNG image (100x100 pixel, red square
on transparent background):
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class Test {
public static void main(String args[]) throws IOException {
int cmap[] = {
0x00000000, /*transparent*/
0xFF000000, /*black*/
0xFFFF0000, /*red*/
0xFFFFFF00, /*yellow*/
0xFF00FF00, /*green*/
//......
};
IndexColorModel colorModel = new IndexColorModel(8,
cmap.length, cmap, 0, true, -1, DataBuffer.TYPE_BYTE);
BufferedImage image = new BufferedImage(100, 100,
BufferedImage.TYPE_BYTE_INDEXED, colorModel);
Graphics2D g = image.createGraphics();
g.setBackground(new Color(0,0,0,0)); /*transparent*/
g.clearRect(0 , 0, image.getWidth(), image.getHeight());
g.setColor(Color.red);
g.draw(new Rectangle(10, 10, 40, 40));
ImageIO.write(image, "PNG", new File("example.png"));
}
}