Use zxing to generate a QR code in servlet format

Source: Internet
Author: User
Because the project has a function that requires two-dimensional code printing, because I prefer html + css + js implementation, the first thing that comes to mind is jquery. qrcode. js plug-in. This plug-in can generate two-dimensional codes using canvas and table. The effect is good, but there is a problem with Chinese support. This plug-in uses ca by default... syntaxHighlighter. a. Because the project has a function that requires two-dimensional code printing, because I prefer html + css + js implementation, the first thing that comes to mind is jquery. qrcode. js plug-in. This plug-in can use canvas and table to generate two-dimensional codes. The effect is good, but there is a problem with Chinese support. This plug-in uses canvas by default, so when using IE, you need to specify the render parameter. As long as the parameter value is not canvas, it will be generated using table. Due to this problem, I made one at github and fork and made the following changes:
//true if support          function canvasSupport() {              return !!document.createElement('canvas').getContext;          }          return this.each(function(){              //if the browser not support canvas,then table.              if(!canvasSupport()){                  options.render = "table";              }              var element = options.render == "canvas" ? createCanvas() : createTable();              $(element).appendTo(this);          });  

 

After modification, you do not need to specify the render parameter. If canvas is not supported, table is used. the disadvantage of using canvas is that it cannot be displayed during webpage printing... there seems to be a solution to this problem. I didn't find it. I used the table directly. However, printing still seems problematic. Js has a problem, so zxing can only be used to output the QR code and write the following servlet code:
@ SuppressWarnings ("serial") public class QrCodeServlet extends HttpServlet {private static final int BLACK =-16777216; private static final int WHITE =-1; private BufferedImage toBufferedImage (BitMatrix matrix) {int width = matrix. getWidth (); int height = matrix. getHeight (); BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_ARGB); for (int x = 0; x <width; x ++) {(Int y = 0; y 
  
Hints = new Hashtable
  
   
(); Hints. put (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. h); byteMatrix = new MultiFormatWriter (). encode (new String (content. getBytes ("UTF-8"), "ISO-8859-1"), BarcodeFormat. QR_CODE, imgWidth, imgHeight, hints); BufferedImage image = toBufferedImage (byteMatrix); resp. setContentType ("image/png"); ImageIO. write (image, "png", resp. getOutputStream ();} catch (Exception e ){}}}
  
 

 

The m parameter is required. The h and w parameters are optional. The default width and height are 110px. Because zxing's default encoding is ISO-8859-1, garbled characters may occur when using other encoding, even if other encoding methods are executed, there is still a problem, if the conversion is ISO-8859-1 there is no garbled code. Another important thing is to export the QR code in png format when using zxing. When exporting the jpg format, the color is not black or white and confusing, I hope someone can give the reason.
Related Article

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.