Zxing generates a QR code nested logo and outputs it to the browser, zxinglogo
After reading so many blogs and carrying so many code, I wrote my own blog for the first time. I found that even if I used some technology, it is still not easy to express and restate it. I hope I can stick to it.
Why is Zxing used? I don't know anything about Google.
It is quite easy to get started with the introduction of more than 10 pieces of code after the jar package.
1. Introduce dependency in pom. xml
<! -- Generate barcode and QR code --> <dependency> <groupId> com. google. zxing </groupId> <artifactId> core </artifactId> <version> 3.0.0 </version> </dependency> <groupId> com. google. zxing </groupId> <artifactId> javase </artifactId> <version> 3.0.0 </version> </dependency>
2. The two-dimensional code @ RequestMapping ("createQRCode") is drawn at the backend controler layer ")Public String createQRCode (HttpServletRequest request, HttpServletResponse response ){
// If the current user has a channel, the QR code String id = UserUtils. getUser (). getId (); LoanChannel channelByUserId = loanChannelService. getChannelByUserId (id); if (channelByUserId! = Null) {JSONObject json = new JSONObject (); json. put ("channelName", channelByUserId. getName (); json. put ("channelNo", channelByUserId. getChannelNo (); String content = json. toJSONString (); // content int width = 210; // Image width int height = 200; // Image height String format = "png"; // image type Map <EncodeHintType, object> hints = new HashMap <EncodeHintType, Object> (); hints. put (EncodeHintType. CHARACTER_SET, "UTF-8 "); // Set the error correction level hints. put (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. h); BitMatrix bitMatrix = null; // generate the matrix try {bitMatrix = new MultiFormatWriter (). encode (content, BarcodeFormat. QR_CODE, width, height, hints);/** define a blank buffer Stream image */BufferedImage image = new BufferedImage (bitMatrix. getWidth (), bitMatrix. getHeight (), BufferedImage. TYPE_INT_RGB);/** convert some matrices in the byte conversion object of the circular QR code to image */for (int x = 0; x <BitMatrix. getWidth (); x ++) {for (int y = 0; y <bitMatrix. getHeight (); y ++) {/** get one of them and determine whether it is black or white. true: Black, false: White */int, rgb = bitMatrix. get (x, y )? 0x000000: 0 xffffff;/** draw a bit in the blank image */image. setRGB (x, y, rgb );}}
// Load the logo InputStream stream = this. getClass (). getResourceAsStream ("/images/logo.png"); BufferedImage logo = ImageIO. read (stream );
// Nested logo BufferedImage logoMatrix = LogoConfig. logoMatrix (image, logo); ImageIO. write (logoMatrix, format, response. getOutputStream ();} catch (WriterException e) {logger.info ("error in QR code output", e); e. printStackTrace ();} catch (IOException e) {logger.info ("error in output QR code", e); e. printStackTrace ();} String data = channelByUserId. getName () + "-" + channelByUserId. getChannelNo (); return data;} return "";}
3. QR code nested logo processing class
Public class LogoConfig {/*** set logo * @ param matrixImage source QR code image * @ return returns a QR code image with a logo * @ throws IOException * @ author Administrator sangwenhao */public static BufferedImage logoMatrix (BufferedImage matrixImage, bufferedImage logo) throws IOException {/*** read the QR code image and build the Drawing Object */Graphics2D g2 = matrixImage. createGraphics (); int matrixWidth = matrixImage. getWidth (); int matrixHeigh = matrixImage. getHeight (); // start to draw the image g2.drawImage (logo, matrixWidth/5*2, matrixheweigh/5*2, matrixWidth/5, matrixheweigh/5, null ); // draw BasicStroke stroke = new BasicStroke (5, BasicStroke. CAP_ROUND, BasicStroke. JOIN_ROUND); g2.setStroke (stroke); // set the stroke object // RoundRectangle2D of the rounded corner of the specified radian. float round = new RoundRectangle2D. float (matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, 20, 20); g2.setColor (Color. white); g2.draw (round); // draw an arc rectangle // set the logo to have a gray border BasicStroke stroke2 = new BasicStroke (1, BasicStroke. CAP_ROUND, BasicStroke. JOIN_ROUND); g2.setStroke (stroke2); // you can specify RoundRectangle2D as the stroke object. float round2 = new RoundRectangle2D. float (matrixWidth/5*2 + 2, matrixHeigh/5*2 + 2, matrixWidth/5-4, matrixHeigh/5-, 20 ); g2.setColor (new Color (128,128,128); g2.draw (round2); // draw an arc rectangle */g2.setRenderingHint (RenderingHints. KEY_ANTIALIASING, RenderingHints. VALUE_ANTIALIAS_ON); matrixImage. flush (); g2.dispose (); return matrixImage ;}}
4. The front-end uses the tag href and uses the image address to directly download images. The download attribute is used to specify the saved file name.
Img src is output by the background
<A id = "fileName" href = "$ {ctx}/loan/loanChannel/createQRCode" download = "channel .png"> </a>
5. Effect