content:
Let's take a look at the problem we have to solve HTML how to display a picture to achieve graphic through servlet to combine the actual application to continue to improve the appendix reference information about the author
Huanglin @xdevelop.net) Shenzhen Yingyuan Technology in November 2001
Building a dynamic website, flexibility and beauty often become a contradiction. Web designers have considered visual perspective, in many places, and sometimes even in the dynamic output content, such as the barum of the website, various titles, etc. These contents often want to transform frequently, and the script for the web page is required to output in real time according to the content in the database. Traditional use of pictures clearly unable to compete for the location of frequent transformation, usually use a trade-off approach, or reduce the requirements for visual effects, let designers use text design, or require maintenance personnel to re-create and replace the actual content according to actual content Picture, etc. In this regard, this article will provide a more flexible solution.
If you are a web developer, more or less will encounter such a situation: web designers use pictures when designing web pages, such as: and "hot point focus" name, maybe One or two days requested to change to "focus interviews" and other words, and have to re-create a picture replacement. And using text plus background, sometimes it is easy to achieve good results. Table background diagram is used, you need to carefully adjust the size of the table, and other changes will not intentionally affect it, you need to be carefully debugged. I encountered a web page in multiple project development, and the programmer and the art are often ultimately selected to avoid and compromise. Although it usually has little effect, it is a gap between after all, and fulfilling the pursuit. So finally produced a solution. Let's take a look at the problem we have to solve our problem. We can simply summarize: There is a picture, such as: Now we have to dynamically output the text, such as "Hot Point" to the above, and get similar to the following display: HTML Display a picture is displayed in the HMTL is simple: . In addition, we also know that the file type in the SRC property is not limited, that is, the writing method of is also legal, the same reference servlet: SRC property, such as background image path, output text, text output, font, size, etc. The servlet is processed and returned to the processed image data, thereby displaying an image of the text on the web page. By the Servlet Configuring Picture Breaking Output Writes a simple servlet implementation code according to the above principle, the servlet can output text to the picture according to the passing parameter requirements and return the browser to the image data after the graphic, and The image after the call is called (Note: This servlet only implements processing of the JPG format image file, does not support GIF): package net.xdevelop.merge;
Import javax.servlet. *;
Import javax.servlet.http. *;
Import java.io. *;
Import java.util. *;
Import java.awt. *;
Import java.awt.image. *; import com.sun.Image.codec.jpeg. *;
Import net.xdevelop.util.Paramutil;
/ **
* Use the specified font, color, and size, embed the specified location of the specified image, call parameters:
* text: 要 嵌 文
* ImageFile: Virtual path for JPG
* x: Start X coordinate position of text output
* Y: The start y coordinate position of the text output
* FontColor: Font Color (Example FontColor = ffffff)
* Fontsize: Font size
* FontStyle: font style (bevel, bold, etc.)
* FontName: Font Name (such as imitation Song, Song body, etc.)
* /
Public class textintoImage extends httpservlet {
Private static final string content_type = "image / jpeg; charSet = GB2312";
Public void init () throws servletexception {
}
/ ** Process the http get request * /
Public void doget (httpservletRequest request, httpservletResponse response)
Throws servletexception, ioException {
DOPOST (Request, Response);
}
/ / -------------------------------------------------------------------------------------------- ---------------------------------------------
/ ** Process the http post request * /
Public void dopost (httpservletRequest Request, httpservletResponse response)
Throws servletexception, ioException {
Response.setContentType (Content_Type);
String text = ""; // 要 嵌 文
String imagefile = ""; // The virtual path of the embedded picture
INT x = 0; // coordinate
INT Y = 0;
String fontcolor = ""; // font color
INT FONTSIZE = 0; // Font size
String fontstyle = ""; // font style (bevel, bold, etc.)
String fontname = ""; // font name
Try {
// Get parameters (paramutil class, please see the paramutil classified code attached to the back)
Text = paramutil.getParameter (Request, "Text");
ImageFile = paramutil.getParameter (Request, "ImageFile");
x = paramutil.getInetInetParameter (Request, "X", 0);
Y = paramutil.getIneTParameter (Request, Y ", 0);
FontColor = paramutil.getParameter (Request, "FontColor"); FontSize = paramutil.getInetParameter (Request, "Fontsize", 16);
FontStyle = paramutil.getParameter (Request, "FontStyle");
FontName = paramutil.getParameter (Request, "FontName");
}
Catch (Exception E) {
E.PrintStackTrace ();
}
ServletOutputStream output = response.getOutputStream ();
IF (imagefile.tolowercase (). endswith (". jpeg") || imagefile.tolowercase (). endswith (". jpg")) {
ImageFile = getServletContext (). getRealPath (ImageFile);
InputStream Imagein = New FileInputStream (New File (Imagefile);
JPEGIMAGEDECODER DECODER = JPEGCODEC.CREATEJPEGDECODER (Imagein);
BufferedImage Image = decoder.decodeasbufferedImage ();
Graphics g = image.getgraphics ();
// Set color
G.SetColor (New Color (Integer.Parseint (FontColor, 16)));
// Set the font
Font mfont = new font (fontname, font.plain, fontsize); // default font
IF (FontStyle.equalsignorecase ("Italic")) MFONT = New Font (FontName, Font.Ilic, Fontsize);
IF (FontStyle.equalsignore ")) MFont = New Font (FontName, Font.Bold, FontSize);
IF (FontStyle.equalsignorecase ("Plain")) MFONT = New Font (FontName, Font.Plain, Fontsize);
g.setfont (mfont);
// Output text
g.drawstring (Text, X, Y);
// Output data stream
JPEGIMAGEENCODER ENCODER = JPEGCODEC.CREATEJPEGENCODER (OUTPUT);
Encoder.encode (image);
Imagein.close ();
}
Output.close ();
}
} //
The code to get the parameters above uses a tool class, which is an extension of the request.getParameter () feature: package net.xdevelop.util;
Import javax.servlet. *;
Public class paramutil {
/ **
* Get the parameter value of the specified name in the request, if there is a Chinese garble, please add the transfer section.
* @Param Request ServletRequest object
* @Param paramname parameter name
* @return Returns this value if the variable value exists, otherwise return ""
* /
Public Static String getParameter (ServletRequest Request, String Paramname) {string temp = request.getParameter (paramname);
IF (Temp! = null &&! temp.equals (")) {
// If there is a Chinese problem, add a transcoding code here, an example: Temp = new string (Temp.getbytes ("8859_1"), "GB2312");
Return Temp;
}
Else {
""; "
}
}
/ **
* Get int type parameter in Request
* @Param Request ServletRequest object
* @Param paramname parameter name
* @Param DefaultNum default, if this value is not returned
* @RETURN Returns to the INT type value if the parameter value exists, otherwise returns defaultnum
* /
Public Static Int GetInTparameter (ServletRequest Request, String Paramname, Int defaultnum) {
String Temp = Request.GetParameter (paramname);
IF (Temp! = null &&! temp.equals (")) {
INT NUM = DefaultNum;
Try {
Num = integer.parseint (TEMP);
}
Catch (exception ignored) {
}
Return Num;
}
Else {
Return Defaultnum DEFAULTNUM
}
}
} ///
Actual application
Declare the servlet in Web.xml
servlet>
servlet-maping>
Place the Net.xDevelop.mege.textintoImage class and Net.xDevelop.util.Paramutil class into the web-inf / class / jsp page call. In this case, put the bg.jpg file into the root directory, sample code: 转载请注明原文地址:https://www.9cbs.com/read-92194.html