Skip to content

Instantly share code, notes, and snippets.

@ulinkwo
Created March 18, 2010 05:55
Show Gist options
  • Save ulinkwo/336090 to your computer and use it in GitHub Desktop.
Save ulinkwo/336090 to your computer and use it in GitHub Desktop.
private void createImage(OutputStream out) {
int width = 100;
int height = 100;
BufferedImage buffer = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffer.createGraphics();
// set background:
g.setBackground(Color.BLUE);
g.clearRect(0, 0, width, height);
// set fore color:
g.setColor(Color.RED);
// start draw:
g.drawLine(0, 0, 99, 199);
// end draw:
g.dispose();
buffer.flush();
// encode:
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buffer);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(buffer);
} catch (IOException ioe) {
ioe.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment