It's all built into Java........ I don't have the exact code, but it should help you find exactly what you need.
--------------------------------------------------------
// need to created a BufferedImage
Color[] colorArray = new Color[]{Color.RED};
BufferedImage bufferedImage = new BufferedImage(getXWidth(), getYWidth(),
BufferedImage.TYPE_INT_RGB);
// Create a graphics contents on the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
for (int x = 0; x < getXWidth(); x++) {
for (int y = 0; y < getYWidth(); y++) {
g2d.setColor(colorArray[x][y]);
g2d.drawLine(x, y, x + 1, y + 1);
}
}
g2d.dispose();
-----------------------------------------------------------------------
Then you need to write the image
/** Creates a new instance of ImageWriter */
public RenderedImageWriter() {
super();
}
private static final String defaultFormat = "png";
private ImageWriter findImageWriter(String format) {
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName(format);
if (iter.hasNext()) {
writer = (ImageWriter) iter.next();
}
if(writer == null && format.equalsIgnoreCase(defaultFormat) == false) {
findImageWriter(defaultFormat);
}
return writer;
}
/**
*
* the save method
*
* @param rendImage
* @param outputFile
* @param format
* @throws IOException
*/
public void save(RenderedImage rendImage, File outputFile, String format)
throws IOException {
// Find a writer
ImageWriter writer = findImageWriter(format);
// Prepare output file
ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile);
writer.setOutput(ios);
// Set the compression quality
ImageWriteParam iwparam = new MyImageWriteParam();
iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
float compressionQuality = 0;
iwparam.setCompressionQuality(compressionQuality);
// Write the image
writer.write(null, new IIOImage(rendImage, null, null), iwparam);
// Cleanup
ios.flush();
writer.dispose();
ios.close();
}
-----------------------------------------------------
You'll need this subclass
import java.util.Locale;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
/**
* This class overrides the setCompressionQuality() method to workaround a
* problem in compressing JPEG images using the javax.imageio package.
*/
public class MyImageWriteParam extends JPEGImageWriteParam {
/**
*
* Constructs a new
MyImageWriteParam
object.
*
*/
public MyImageWriteParam() {
super(Locale.getDefault());
}
/**
* This method accepts quality levels between 0 (lowest) and 1 (highest) and
* simply converts it to a range between 0 and 256; this is not a correct
* conversion algorithm. However, a proper alternative is a lot more
* complicated. This should do until the bug is fixed.
*/
@Override
public void setCompressionQuality(float quality) {
if (quality < 0.0F || quality > 1.0F) {
throw new IllegalArgumentException("Quality out-of-bounds!");
}
this.compressionQuality = 256 - (quality * 256);
}
}
-----------------------------------------------------------------------------