Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate QR Code and add logo to QR Code by java

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how java generates the QR code and adds logo to the QR code. The article is very detailed and has a certain reference value. Interested friends must read it!

Java generates a QR code, as shown below:

Package com.bus.wx.action.code;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.Hashtable;import java.util.Map;import javax.imageio.ImageIO;import javax.servlet.ServletOutputStream Import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;import com.bus.plugin.wx.action.WxAction;import com.bus.wx.util.Limits;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter Import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.QRCodeWriter;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import com.lys.sys.log.Log / * barcode, QR code * @ author hwj * * / @ Scope (value= "prototype") @ Controller ("Bar_Qr_Code_Action") @ RequestMapping (value= "plug/wx/wwz/ {bcflag} / barqrcode") public class Bar_Qr_Code_Action extends WxAction {private static final int LogoPart = 4; private static final int BLACK = 0xFF000000ramp / the color used to set the pattern private static final int WHITE = 0xFFFFFFFF; / / for the background color String format = "png" Public static BufferedImage toBufferedImage (BitMatrix matrix) {int width = matrix.getWidth (); int height = matrix.getHeight (); BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x +) {for (int y = 0; y < height; yearly +) {image.setRGB (x, y, (matrix.get (x, y)? BLACK: WHITE); / / image.setRGB (x, y, (matrix.get (x, y)? Color.YELLOW.getRGB (): Color.CYAN.getRGB ());}} return image;} public BufferedImage LogoMatrix (BufferedImage matrixImage) throws IOException {/ * read the QR code picture and build the drawing object * / Graphics2D G2 = matrixImage.createGraphics (); int matrixWidth = matrixImage.getWidth (); int matrixHeigh = matrixImage.getHeight () / * read Logo image * / String path=request.getSession () .getServletContext () .getRealPath ("/ images/logo/dzjkklog.png"); BufferedImage logo = ImageIO.read (new File (path)); / / start drawing g2.drawImage (logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null) / / draw BasicStroke stroke = new BasicStroke; g2.setStroke (stroke); / / set stroke object / / rounded rectangle with specified radians RoundRectangle2D.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 a circular rectangle / / set logo to have a gray border BasicStroke stroke2 = new BasicStroke (1Magnetics BasicStroke.JOINGROUND); g2.setStroke (stroke2); / / set stroke object RoundRectangle2D.Float round2 = new RoundRectangle2D.Float (matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4 20); g2.setColor (new Color (128128128)); g2.draw (round2) / / draw arc rectangle g2.dispose (); matrixImage.flush (); return matrixImage;} / * * generate QR code: transfer the generated QR code directly to the foreground page * @ param bcflag * / @ RequestMapping (value= "createQrCodes", method = RequestMethod.GET) public void createQrCodes (@ PathVariable String bcflag,String dastid) {String url= "scan the content of the QR code" If {int width=430; int height=430; stream=response.getOutputStream (); QRCodeWriter writer=new QRCodeWriter (); Hashtable hints = new Hashtable (); hints.put (EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put (EncodeHintType.CHARACTER_SET, "utf-8"); hints.put (EncodeHintType.MARGIN, 1) / / sets the emptiness of the edge of the QR code, non-negative BitMatrix bitMatrix = new MultiFormatWriter (). Encode (url,// content to be encoded BarcodeFormat.QR_CODE, width, / / the width of the bar code height, / / the height of the bar code hints); / / BitMatrix m=writer.encode (url, BarcodeFormat.QR_CODE, height,width); BufferedImage image = toBufferedImage (bitMatrix); image = LogoMatrix (image) If (! ImageIO.write (image, format, stream)) {throw new IOException ("Could not write an image of format" + format);}} catch (Exception e) {Log.in.info (e.getMessage ());} finally {if (streamlined null) {try {stream.flush (); stream.close ();} catch (IOException e) {Log.in.info (e.getMessage ()) The above is all the contents of the article "how java generates a QR code and adds logo to a QR code". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report