The QR code generated in Java is directly output to the page if it is not saved to the disk. Therefore, the generated QR code needs to be output directly to the page in the form of a stream, I use myeclipse and tomcat.
The principle is: when loading a page, according to the SRC (code. jspx) Find the corresponding servlet (codeservlet), edit the content in the QR code in the servlet, and call the encoderqrcoder method in the encoderhandler class that generates the QR code
1. First import the jar package (put the jar package in the lib directory)
Name of the imported jar package: qrcode_swetake
Download jar package connection http://download.csdn.net/detail/huakaihualuo1223/4527504
2. Generate a QR code in Java and output it as a stream
Package COM. kaishengit. util; import Java. AWT. color; import Java. AWT. graphics2d; import Java. AWT. image. bufferedimage; import javax. imageIO. imageIO; import javax. servlet. HTTP. httpservletresponse; import COM. swetake. util. qrcode; public class encoderhandler {public void encoderqrcoder (string content, httpservletresponse response) {try {qrcode handler = new qrcode (); handler. setqrcodeerrorcorrect ('M'); handler. setqrcodeencodemode ('B'); handler. setqrcodeversion (7); system. out. println (content); byte [] contentbytes = content. getbytes ("UTF-8"); bufferedimage bufimg = new bufferedimage (80, 80, bufferedimage. type_int_rgb); graphics2d GS = bufimg. creategraphics (); GS. setbackground (color. white); GS. clearrect (0, 0,140,140); // sets the image color: blackgs. setcolor (color. black); // If the offset is not set, the parsing error int pixoff = 2 may occur. // The output content is: QR code if (contentbytes. length> 0 & contentbytes. length <124) {Boolean [] [] codeout = handler. calqrcode (contentbytes); For (INT I = 0; I <codeout. length; I ++) {for (Int J = 0; j <codeout. length; j ++) {If (codeout [J] [I]) {Gs. fillrect (J * 3 + pixoff, I * 3 + pixoff, 3, 3) ;}}} else {system. err. println ("qrcode content bytes length =" + contentbytes. length + "not in [0,120]. ");} Gs. dispose (); bufimg. flush (); // generates the QR code qrcode image ImageIO. write (bufimg, "jpg", response. getoutputstream ();} catch (exception e) {e. printstacktrace ();}}}
3. Create a Servlet
Package COM. kaishengit. web; import Java. io. ioexception; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import COM. kaishengit. util. encoderhandler; public class codeservlet extends httpservlet {Private Static final long serialversionuid = 1l; @ overrideprotected void Service (httpservletrequest requset, httpservletresponse response) throws servletexception, ioexception {string content = "Name: maysnow Phone: 123687495 "; encoderhandler encoder = new encoderhandler (); encoder. encoderqrcoder (content, response );}}
4. configuration in Web. xml
<servlet> <servlet-name>CodeService</servlet-name> <servlet-class>com.kaishengit.web.CodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CodeService</servlet-name> <url-pattern>/code.jspx</url-pattern> </servlet-mapping>
5. IMG in JSP (the SRC of IMG is the URL of the corresponding codeservlet)
6. The QR code generated on the page