private void drawAWTImage(OutputStream outStr, int iThumbWidth, int iThumbHeight, float fQuality) throws IOException, InstantiationException, InterruptedException {
String sInputURI;
Frame awtFrame;
JPEGImageEncoder encoder;
JPEGEncodeParam param;
BufferedImage thumbImage;
Graphics2D graphics2D;
URL oURI;
if (DebugFile.trace) {
DebugFile.writeln("Begin Image.drawAWTImage([OutputStream], " + String.valueOf(iThumbWidth) + "," + String.valueOf(iThumbHeight) + "," + String.valueOf(fQuality));
DebugFile.incIdent();
}
sInputURI = getString(DB.path_image);
if (sInputURI.startsWith("http://") || sInputURI.startsWith("https://")) {
oURI = new URL(sInputURI);
if (DebugFile.trace) DebugFile.writeln("java.awt.Toolkit.getDefaultToolkit().getImage([java.net.URL])");
oImg = Toolkit.getDefaultToolkit().getImage(oURI);
}
else {
if (DebugFile.trace) DebugFile.writeln("java.awt.Toolkit.getDefaultToolkit().getImage(sInputURI)");
oImg = Toolkit.getDefaultToolkit().getImage(sInputURI);
}
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) {
if (DebugFile.trace) DebugFile.writeln("new java.awt.;Frame()");
try {
awtFrame = new Frame();
} catch (Exception e) { throw new InstantiationException("Cannot instantiate java.awt.Frame " + (e.getMessage()!=null ? e.getMessage() : "")); }
if (DebugFile.trace) DebugFile.writeln("new MediaTracker([Frame])");
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);
if (DebugFile.trace) DebugFile.writeln("java.awt.Graphics2D = thumbImage.createGraphics()");
graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(oImg, 0, 0, iThumbWidth, iThumbHeight, null);
graphics2D.dispose();
if (DebugFile.trace) DebugFile.writeln("com.sun.image.codec.jpeg.JPEGImageEncoder = JPEGCodec.createJPEGEncoder([OutputStream]);");
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);
if (DebugFile.trace) DebugFile.writeln("JPEGImageEncoder.encode([BufferedImage]);");