1 |
|
|
2 |
|
|
3 |
import java.awt.Color; |
import java.awt.Color; |
4 |
|
import java.awt.Font; |
5 |
import java.awt.Graphics2D; |
import java.awt.Graphics2D; |
6 |
import java.awt.RenderingHints; |
import java.awt.RenderingHints; |
7 |
|
import java.awt.font.LineMetrics; |
8 |
|
import java.awt.geom.Rectangle2D; |
9 |
import java.awt.image.BufferedImage; |
import java.awt.image.BufferedImage; |
10 |
|
import java.io.File; |
11 |
import java.io.IOException; |
import java.io.IOException; |
12 |
|
import java.util.logging.Level; |
13 |
|
import java.util.logging.Logger; |
14 |
|
|
15 |
import javax.imageio.ImageIO; |
import javax.imageio.ImageIO; |
16 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
23 |
// Image scaling :http://helpdesk.objects.com.au/java/how-do-i-scale-a-bufferedimage |
// Image scaling :http://helpdesk.objects.com.au/java/how-do-i-scale-a-bufferedimage |
24 |
public class ImageServlet extends HttpServlet { |
public class ImageServlet extends HttpServlet { |
25 |
private static final long serialVersionUID = 1L; |
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 { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
45 |
|
|
52 |
gfx.fillRect(0, 0, width, height); //Graphics2D has many various drawing methods |
gfx.fillRect(0, 0, width, height); //Graphics2D has many various drawing methods |
53 |
gfx.setColor( Color.blue ); |
gfx.setColor( Color.blue ); |
54 |
gfx.fillOval(10, 10, width-20, height-20); |
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 |
gfx.dispose(); //dispose when we are done with drawing |
74 |
|
|
75 |
|
|
80 |
gfx.drawImage(bufferedImage, 0, 0, width/2, height/2, null); |
gfx.drawImage(bufferedImage, 0, 0, width/2, height/2, null); |
81 |
gfx.dispose(); |
gfx.dispose(); |
82 |
|
|
83 |
|
response.setHeader("Cache-Control", "no-cache"); |
84 |
|
response.setDateHeader("Expires", 0); |
85 |
response.setContentType("image/png"); |
response.setContentType("image/png"); |
86 |
ImageIO.write(smallImage, "png", response.getOutputStream() ); // use imageio.read to load a file to a bufferedimage |
ImageIO.write(smallImage, "png", response.getOutputStream() ); // use imageio.read to load a file to a bufferedimage |
87 |
|
|