// ----------------------------------------------------------
private void drawAWTImage(byte[] aImgSource, OutputStream outStr, int iThumbWidth, int iThumbHeight, float fQuality) throws IOException, InstantiationException, InterruptedException {
Frame awtFrame;
JPEGImageEncoder encoder;
JPEGEncodeParam param;
BufferedImage thumbImage;
Graphics2D graphics2D;
oImg = Toolkit.getDefaultToolkit().createImage(aImgSource);
int iImageWidth = oImg.getWidth(null);
int iImageHeight = oImg.getHeight(null);
double thumbRatio = ((double) iThumbWidth) / ((double) iThumbHeight);
double imageRatio = ((double) iImageWidth) / ((double) iImageHeight);
if (thumbRatio < imageRatio)
iImageHeight = (int)(iThumbWidth / imageRatio);
else
iThumbWidth = (int)(iThumbHeight * imageRatio);
if (null==mediaTracker) {
try {
awtFrame = new Frame();
} catch (Exception e) { throw new InstantiationException("Cannot instantiate java.awt.Frame " + (e.getMessage()!=null ? e.getMessage() : "")); }
mediaTracker = new MediaTracker(awtFrame);
} // fi (mediaTracker)
mediaTracker.addImage(oImg, 0);
mediaTracker.waitForID(0);
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
thumbImage = new BufferedImage(iThumbWidth, iThumbHeight, BufferedImage.TYPE_INT_RGB);
graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(oImg, 0, 0, iThumbWidth, iThumbHeight, null);
graphics2D.dispose();
encoder = JPEGCodec.createJPEGEncoder(outStr);
param = encoder.getDefaultJPEGEncodeParam(thumbImage);
// javax.imageio.plugins.jpeg.JPEGHuffmanTable t;
// javax.imageio.plugins.jpeg.JPEGQTable q;
fQuality = Math.max(0, Math.min(fQuality, 100));
if (fQuality>1)
param.setQuality(fQuality / 100.0f, false);
else
param.setQuality(fQuality, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
thumbImage.flush();
mediaTracker.removeImage(oImg, 0);