Java examples for 2D Graphics:GIF
convert RGBA To GIF
//package com.java2s; import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.awt.image.IndexColorModel; import java.awt.image.WritableRaster; public class Main { public static BufferedImage convertRGBAToGIF(final BufferedImage src, final int transColor) { BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); final Graphics g = dst.getGraphics(); g.setColor(new Color(transColor)); g.fillRect(0, 0, dst.getWidth(), dst.getHeight()); {//from w w w .jav a2s .c o m final IndexColorModel indexedModel = (IndexColorModel) dst .getColorModel(); final WritableRaster raster = dst.getRaster(); final int sample = raster.getSample(0, 0, 0); final int size = indexedModel.getMapSize(); final byte[] rr = new byte[size]; final byte[] gg = new byte[size]; final byte[] bb = new byte[size]; indexedModel.getReds(rr); indexedModel.getGreens(gg); indexedModel.getBlues(bb); final IndexColorModel newModel = new IndexColorModel(8, size, rr, gg, bb, sample); dst = new BufferedImage(newModel, raster, dst.isAlphaPremultiplied(), null); } dst.createGraphics().drawImage(src, 0, 0, null); return dst; } }