1 |
|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Font; |
5 |
import java.awt.Graphics2D; |
6 |
import java.awt.RenderingHints; |
7 |
import java.awt.font.LineMetrics; |
8 |
import java.awt.geom.Rectangle2D; |
9 |
import java.awt.image.BufferedImage; |
10 |
import java.io.File; |
11 |
import java.io.IOException; |
12 |
import java.util.logging.Level; |
13 |
import java.util.logging.Logger; |
14 |
|
15 |
import javax.imageio.ImageIO; |
16 |
import javax.servlet.ServletException; |
17 |
import javax.servlet.http.HttpServlet; |
18 |
import javax.servlet.http.HttpServletRequest; |
19 |
import javax.servlet.http.HttpServletResponse; |
20 |
|
21 |
|
22 |
|
23 |
// Image scaling :http://helpdesk.objects.com.au/java/how-do-i-scale-a-bufferedimage |
24 |
public class ImageServlet extends HttpServlet { |
25 |
private static final long serialVersionUID = 1L; |
26 |
|
27 |
Logger logger = Logger.getLogger(ImageServlet.class.getName()); |
28 |
|
29 |
protected Font loadFont() throws IOException { |
30 |
File fontFile= new File( getServletContext().getRealPath("/Comic_Sans_MS.ttf") ); |
31 |
logger.info( "Font:" + fontFile.getAbsolutePath() ); |
32 |
|
33 |
Font font = null; |
34 |
try { |
35 |
font = Font.createFont(Font.TRUETYPE_FONT, fontFile); |
36 |
} catch (Exception ex) { |
37 |
logger.log(Level.SEVERE, "Font load failed", ex); |
38 |
} |
39 |
|
40 |
|
41 |
return font.deriveFont(48.0f); |
42 |
} |
43 |
|
44 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
45 |
|
46 |
final int width = 400; |
47 |
final int height = 400; |
48 |
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
49 |
|
50 |
Graphics2D gfx = bufferedImage.createGraphics(); |
51 |
gfx.setColor( Color.white ); |
52 |
gfx.fillRect(0, 0, width, height); //Graphics2D has many various drawing methods |
53 |
gfx.setColor( Color.blue ); |
54 |
gfx.fillOval(10, 10, width-20, height-20); |
55 |
gfx.setColor( Color.black ); |
56 |
|
57 |
///////////////////// |
58 |
///Text draw section |
59 |
String text = "Text to draw"; |
60 |
Font f = loadFont(); |
61 |
gfx.setFont( f ); |
62 |
Rectangle2D bounds = f.getStringBounds(text, gfx.getFontRenderContext()); |
63 |
int textX = (int) (width-bounds.getWidth())/2; |
64 |
|
65 |
|
66 |
logger.info("bWidth=" + bounds.getWidth() + ", bHeight=" + bounds.getHeight()); |
67 |
|
68 |
|
69 |
gfx.drawString(text, textX, 210);//note that the Y denotes the text's baseline ! |
70 |
|
71 |
|
72 |
|
73 |
gfx.dispose(); //dispose when we are done with drawing |
74 |
|
75 |
|
76 |
//Downscale image to create a smoother looking one |
77 |
BufferedImage smallImage = new BufferedImage(width/2, height/2, BufferedImage.TYPE_INT_RGB); |
78 |
gfx = smallImage.createGraphics(); |
79 |
gfx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
80 |
gfx.drawImage(bufferedImage, 0, 0, width/2, height/2, null); |
81 |
gfx.dispose(); |
82 |
|
83 |
response.setHeader("Cache-Control", "no-cache"); |
84 |
response.setDateHeader("Expires", 0); |
85 |
response.setContentType("image/png"); |
86 |
ImageIO.write(smallImage, "png", response.getOutputStream() ); // use imageio.read to load a file to a bufferedimage |
87 |
|
88 |
} |
89 |
|
90 |
} |