01: package com.knowgate.misc;
02:
03: import com.knowgate.debug.*;
04: import com.knowgate.misc.Gadgets;
05: import com.sun.image.codec.jpeg.*;
06: import java.awt.*;
07: import java.awt.image.*;
08: import java.io.*;
09: import javax.imageio.*;
10: import java.net.*;
11: import javax.servlet.*;
12:
13: /**
14: * Create Image thumbnails
15: * Requires Java 1.2+
16: * @deprecated Use com.knowgate.hipegate.Image
17: */
18:
19: public class Thumbnail {
20: /* public static void main(String[] args) throws Exception {
21: getThumb("c:\\prueba.jpg",System.out,"50","50","75");
22: }*/
23: public static synchronized void getThumb(String sInFile,
24: ServletOutputStream sOutputStream, String sWidth,
25: String sHeight, String sQuality) throws Exception {
26: System.setProperty("java.awt.headless", "true");
27: if (DebugFile.trace)
28: DebugFile.writeln("BEGIN getThumb");
29: // load image from INFILE
30: Image image = Toolkit.getDefaultToolkit().getImage(sInFile);
31: if (DebugFile.trace)
32: DebugFile.writeln("Image created");
33:
34: MediaTracker mediaTracker = new MediaTracker(new Frame());
35: mediaTracker.addImage(image, 0);
36: mediaTracker.waitForID(0);
37:
38: if (DebugFile.trace)
39: DebugFile.writeln("Image loaded");
40:
41: // determine thumbnail size from WIDTH and HEIGHT
42: int thumbWidth = Integer.parseInt(sWidth);
43: int thumbHeight = Integer.parseInt(sHeight);
44: double thumbRatio = (double) thumbWidth / (double) thumbHeight;
45: int imageWidth = image.getWidth(null);
46: int imageHeight = image.getHeight(null);
47: double imageRatio = (double) imageWidth / (double) imageHeight;
48: if (thumbRatio < imageRatio) {
49: thumbHeight = (int) (thumbWidth / imageRatio);
50: } else {
51: thumbWidth = (int) (thumbHeight * imageRatio);
52: }
53:
54: if (DebugFile.trace)
55: DebugFile.writeln("Image resized");
56:
57: // draw original image to thumbnail image object and
58: // scale it to the new size on-the-fly
59: BufferedImage thumbImage = new BufferedImage(thumbWidth,
60: thumbHeight, BufferedImage.TYPE_INT_RGB);
61: Graphics2D graphics2D = thumbImage.createGraphics();
62: graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
63: RenderingHints.VALUE_INTERPOLATION_BILINEAR);
64: graphics2D
65: .drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
66:
67: if (DebugFile.trace)
68: DebugFile.writeln("graphics2D created");
69:
70: // send thumbnail image to outputstream
71: String sOutFile = Gadgets.generateUUID() + ".tmp";
72: ServletOutputStream out = sOutputStream;
73: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
74: JPEGEncodeParam param = encoder
75: .getDefaultJPEGEncodeParam(thumbImage);
76: int quality = Integer.parseInt(sQuality);
77: quality = Math.max(0, Math.min(quality, 100));
78: param.setQuality((float) quality / 100.0f, false);
79: encoder.setJPEGEncodeParam(param);
80: encoder.encode(thumbImage);
81:
82: if (DebugFile.trace)
83: DebugFile.writeln("Encode ended");
84: }
85: }
|