Java generates and prints two-dimensional code

Source: Internet
Author: User

In this order-making system, it is necessary to generate a QR code in Java and print and output it on a JSP page, where a QR code is generated in the action.

The key code is as follows

1  Public voidReweima ()throwsexception{2         //Settings page does not cache3HttpServletResponse response =servletactioncontext.getresponse ();4HttpServletRequest Quest =servletactioncontext.getrequest ();5Domain domain=Domainservice.getip ();6         if(domain!=NULL){7String IP =Domain.getip ();8Response.setheader ("Pragma", "No-cache");9Response.setheader ("Cache-control", "No-cache");TenResponse.setdateheader ("Expires", 0); One  ABufferedImage image=NULL; -Servletoutputstream stream =NULL; -             //picture format of QR code theString format = "gif"; -String path= Quest.getscheme () + "://" +ip+ ":" + quest.getserverport () + quest.getcontextpath () + "/";  -String content=path+ "orderdetail_getmenumaterial?menuid=" +menuId; -                 intWidth2 = 200;  +                 intHEIGHT2 = 200;  -                  +Hashtable hints =NewHashtable (); A                 //the encoding used for the content atHints.put (Encodehinttype.character_set, "Utf-8");  -                  -                 Try{ -Bitmatrix Bitmatrix =NewMultiformatwriter (). Encode (Content,barcodeformat.qr_code, width2, height2, hints); -                     intwidth =bitmatrix.getwidth (); -                     intHeight =bitmatrix.getheight (); inImage =Newbufferedimage (width, height, bufferedimage.type_int_rgb); -                      for(intx = 0; x < width; X + +) {  to                          for(inty = 0; Y < height; y++) {  +Image.setrgb (x, Y, Bitmatrix.get (x, y)? 0xff000000:0xffffffff);//Two-dimensional code picture for black and white color -                         }  the                     } *                     //Imageio.write (image, "gif", Response.getoutputstream ()); $}Catch(Exception e) {Panax Notoginseng                     //Todo:handle Exception -                 } the             //only use this kind of decoding method to not appear garbled +String s= "Attachment;filename=" +NewString ("No" +menuid+ ". gif"); AResponse.AddHeader ("Content-disposition", s); theOutputStream os=NewBufferedoutputstream (Response.getoutputstream ()); +Response.setcontenttype ("Image/gif"); - Imageio.write (image,format,os); $ Os.flush (); $ os.close (); -}Else{ -String messige= "No domain name added, no QR code to print"; theRequest.put ("Messige", messige); -             }Wuyi}

JSP page generation call generation QR code method, return to the print QR code JSP, the code is as follows

1<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "2pageencoding= "Iso-8859-1"%>3<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >456<meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 ">7<title>insert title here</title>89<body>Ten<div style= "Text-align:center;" > One A</div> -  -<script type= "Text/javascript" > theWindow.onload =function () { - Window.print () - window.close ()//Return to the original interface after printing -     } +  -</script> +</body> A

Here the idea is the action generated by the two-dimensional code into the SRC tag, directly print the entire JSP, in this process, encountered a problem, originally called the Java internal Print method to print two-dimensional codes, but in this process encountered the system can not find the specified path, (QR code generated on the server , the browser invokes the server content) and, finally, uses an easy way to print the entire JSP page directly.

Java generates and prints two-dimensional code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.