How to realize the Generation of QR Code and background Image by JAVA
Editor to share with you how to achieve JAVA QR code generation plus background map, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Pom.xml dependence
Com.google.zxing core 3.0.1 / * Class name: QRCodeMax * Class description: generate QR code picture + background + text description tool class * creator: one except handsome Useless man * creation time: December 2018 x day x points x minutes x seconds * modification time: February 2019 x days x points x minutes x seconds * revision remarks: update parametric structure * @ version: 2.0 * / public class QRCodeMax {/ / text display private static final int QRCOLOR = 0x201f1f / / QR code color: black private static final int BGWHITE = 0xFFFFF; / / QR code background color: White / / set QR QR code parameter information private static Map hints = new HashMap () {private static final long serialVersionUID = 1L; {put (EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); / / set the error correction level of QR QR code (H is the highest level) put (EncodeHintType.CHARACTER_SET, "utf-8") / / set the encoding method put (EncodeHintType.MARGIN, 0); / / White edge}} / * * generate QR code picture + background + text description * @ param codeFile generation map address * @ param bgImgFile background map address * @ param WIDTH QR code width * @ param HEIGHT QR code height * @ param qrUrl QR code recognition address * @ param note text description 1 * @ param tui text description 2 * @ param size text Word size * @ param imagesX QR code x axis direction * @ param imagesY QR code y axis direction * @ param text1X text description 1x axis direction * @ param text1Y text description 1y axis direction * @ param text2X text description 2x axis direction * @ param text2Y text description 2y axis direction * / public static void CreatQRCode (File codeFile File bgImgFile,Integer WIDTH,Integer HEIGHT,String qrUrl, String note,String tui,Integer size,Integer imagesX,Integer imagesY,Integer text1X,Integer text1Y, Integer text2X,Integer text2Y) {try {MultiFormatWriter multiFormatWriter = new MultiFormatWriter () / / the order of parameters is: encoding content, encoding type, generating picture width, generating picture height, setting parameters BitMatrix bm = multiFormatWriter.encode (qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints); BufferedImage image = new BufferedImage (WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB) / / start creating Bitmap images using QR code data, set to black (0xFFFFFFFF) and white (0xFF000000) bicolor for (int x = 0; x < WIDTH; x +) {for (int y = 0; y < HEIGHT; y +) {image.setRGB (x, y, bm.get (x, y)? QRCOLOR: BGWHITE);}} / * * add a background image * / BufferedImage backgroundImage = ImageIO.read (bgImgFile); int bgWidth=backgroundImage.getWidth (); int qrWidth=image.getWidth (); / / the distance from the x edge of the background image, centered with int disx= (bgWidth-qrWidth)-imagesX / / distance y distance * int disy=imagesY; Graphics2D rng=backgroundImage.createGraphics (); rng.setComposite (AlphaComposite.getInstance (AlphaComposite.SRC_ATOP)); rng.drawImage (image,disx,disy,WIDTH,HEIGHT,null); / * text description parameter setting * / Color textColor=Color.white Rng.setColor (textColor); rng.drawImage (backgroundImage,0,0,null); / / set font type and size (BOLD bold / PLAIN normal) rng.setFont (new font Arial, Font.BOLD,size); / / set font color rng.setColor (Color.black); int strWidth=rng.getFontMetrics (). StringWidth (note) / / text 1 display position int disx1= (bgWidth-strWidth)-text1X;// left or right rng.drawString (note,disx1,text1Y); / / text 2 display position int disx2= (bgWidth-strWidth)-text2X;// left or right rng.drawString (tui,disx2,text2Y); / / up and down rng.dispose (); image=backgroundImage Image.flush (); ImageIO.write (image, "png", codeFile);} catch (Exception e) {e.printStackTrace ();}} / * Test * @ param args * / public static void main (String [] args) {File bgImgFile=new File ("D://tu/bg.png") / / background image File QrCodeFile = new File ("D://tu/myqrcode.png"); / / generate image location String url = "https://blog.csdn.net/weixin_38407595";// QR code link String note ="; / / text description String tui = "" / / text description / / publicity QR code generation / / generate map address, background address, QR code width, QR code height, QR code recognition address, text description 1, text description 2, text size, picture x-axis direction, picture y-axis direction, text 1 | 2xyaxis direction CreatQRCode (QrCodeFile,bgImgFile, 148,148, url, note,tui, 38,408,123,0,0,410,210) }} above is all the content of the article "how to generate QR Code and add background Map in JAVA". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!