1 |
torben |
610 |
|
2 |
|
|
|
3 |
|
|
import java.awt.Color; |
4 |
torben |
611 |
import java.awt.Font; |
5 |
torben |
610 |
import java.awt.Graphics2D; |
6 |
|
|
import java.awt.RenderingHints; |
7 |
torben |
611 |
import java.awt.font.LineMetrics; |
8 |
|
|
import java.awt.geom.Rectangle2D; |
9 |
torben |
610 |
import java.awt.image.BufferedImage; |
10 |
torben |
611 |
import java.io.File; |
11 |
torben |
610 |
import java.io.IOException; |
12 |
torben |
611 |
import java.util.logging.Level; |
13 |
|
|
import java.util.logging.Logger; |
14 |
torben |
610 |
|
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 |
torben |
611 |
|
27 |
|
|
Logger logger = Logger.getLogger(ImageServlet.class.getName()); |
28 |
torben |
610 |
|
29 |
torben |
611 |
protected Font loadFont() throws IOException { |
30 |
|
|
File fontFile= new File( getServletContext().getRealPath("/Comic_Sans_MS.ttf") ); |
31 |
|
|
logger.info( "Font:" + fontFile.getAbsolutePath() ); |
32 |
torben |
610 |
|
33 |
torben |
611 |
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 |
torben |
610 |
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 |
torben |
611 |
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); |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
torben |
610 |
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 |
|
|
|
84 |
|
|
|
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 |
|
|
} |