In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to generate a QR code in Zxing and return it. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Introduction of Google Zxing jar package com.google.zxing core 3.4.0 com.google.zxing javase 3.4.0 tool class package com.sunlands.feo.school.candlestick.utils;import com.google.zxing.*;import com.google.zxing.client.j2se.BufferedImageLuminanceSource;import com.google.zxing.common.BitMatrix;import com.google.zxing.common.HybridBinarizer;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import org.springframework.stereotype.Component;import javax.imageio.ImageIO;import java.awt.* Import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.util.Hashtable;/** * @ author dongxie * / @ Componentpublic class QrCodeUtils {private static final String CHARSET = "UTF-8"; private static final String FORMAT_NAME = "JPG"; / * QR code size * / private static final int QRCODE_SIZE = 300 / * LOGO width * / private static final int WIDTH = 60; / * * LOGO height * / private static final int HEIGHT = 60 / * create param content image * * @ param logoPath logo * @ param isCompress whether to compress Logo * @ return return QR code image * @ throws WriterException e * @ throws IOException BufferedImage * / public static BufferedImage createImage (String content, String logoPath, boolean isCompress) throws WriterException, IOException {Hashtable hints = new Hashtable () Hints.put (EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put (EncodeHintType.CHARACTER_SET, CHARSET); hints.put (EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = new MultiFormatWriter (). Encode (content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); int width = bitMatrix.getWidth (); int height = bitMatrix.getHeight () BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x
< width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } if (logoPath == null || "".equals(logoPath)) { return image; } // 插入图片 QrCodeUtils.insertImage(image, logoPath, isCompress); return image; } /** * 添加Logo * * @param source 二维码图片 * @param logoPath Logo * @param isCompress 是否压缩Logo * @throws IOException void */ private static void insertImage(BufferedImage source, String logoPath, boolean isCompress) throws IOException { File file = new File(logoPath); if (!file.exists()) { return; } Image src = ImageIO.read(new File(logoPath)); int width = src.getWidth(null); int height = src.getHeight(null); // 压缩LOGO if (isCompress) { if (width >WIDTH) {width = WIDTH;} if (height > HEIGHT) {height = HEIGHT;} Image image = src.getScaledInstance (width, height, Image.SCALE_SMOOTH); BufferedImage tag = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics () / / draw g.drawImage (image, 0,0, null); g.dispose (); src = image;} / / insert LOGO Graphics2D graph = source.createGraphics (); int x = (QRCODE_SIZE-width) / 2; int y = (QRCODE_SIZE-height) / 2 Graph.drawImage (src, x, y, width, height, null); Shape shape = new RoundRectangle2D.Float (x, y, width, width, 6,6); graph.setStroke (new BasicStroke (3f)); graph.draw (shape); graph.dispose () } / * generate QR code with Logo * * @ param content QR code content * @ param logoPath Logo * @ param destPath QR code output path * @ param isCompress whether to compress Logo * @ throws Exception void * / public static void create (String content, String logoPath, String destPath, boolean isCompress) throws Exception {BufferedImage image = QrCodeUtils.createImage (content, logoPath, isCompress) Mkdirs (destPath); ImageIO.write (image, FORMAT_NAME, new File (destPath));} / * * generate QR code without Logo * * @ param content QR code content * @ param destPath QR code output path * / public static void create (String content, String destPath) throws Exception {QrCodeUtils.create (content, null, destPath, false) } / * generate a QR code with Logo and output it to the specified output stream * * @ param content QR code content * @ param logoPath Logo * @ param output output stream * @ param isCompress whether to compress Logo * / public static void create (String content, String logoPath, OutputStream output, boolean isCompress) throws Exception {BufferedImage image = QrCodeUtils.createImage (content, logoPath, isCompress) ImageIO.write (image, FORMAT_NAME, output);} / * generates a QR code without Logo and outputs it to the specified output stream * * @ param content QR code content * @ param output output stream * @ throws Exception void * / public static void create (String content, OutputStream output) throws Exception {QrCodeUtils.create (content, null, output, false) } / * QR code parsing * @ param file QR code * @ return returns the parsed QR code content * @ throws Exception String * / public static String parse (File file) throws Exception {BufferedImage image; image = ImageIO.read (file); if (image = = null) {return null } BufferedImageLuminanceSource source = new BufferedImageLuminanceSource (image); BinaryBitmap bitmap = new BinaryBitmap (new HybridBinarizer (source)); Result result; Hashtable hints = new Hashtable (); hints.put (DecodeHintType.CHARACTER_SET, CHARSET); result = new MultiFormatReader (). Decode (bitmap, hints); return result.getText () } / * QR code parsing * @ param path QR code storage location * @ return returns the parsed QR code content * @ throws Exception String * / public static String parse (String path) throws Exception {return QrCodeUtils.parse (new File (path)) } / * determine whether the path exists. If it does not exist, create a * * @ param dir directory * / public static void mkdirs (String dir) {if (dir! = null & &! ".equals (dir)) {File file = new File (dir); if (! file.isDirectory ()) {file.mkdirs () } use package com.jiafly.blueberry.controller;import com.jiafly.blueberry.common.utils.QrCodeUtils;import org.apache.tomcat.util.http.fileupload.IOUtils;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import javax.imageio.ImageIO;import javax.imageio.stream.ImageOutputStream;import javax.servlet.http.HttpServletResponse Import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.OutputStream;/** * author dongxie * / @ RestController@RequestMapping (path = "/ qrcode") public class QrCodeController {@ GetMapping (path = "/ create") public void image (HttpServletResponse response, @ RequestParam ("content") String content) {try {BufferedImage bufferedImage = QrCodeUtils.createImage (content, null, false) ResponseImage (response, bufferedImage);} catch (Exception e) {/ / exceptions are handled by themselves. Applications should not print stack logs directly, which is difficult to locate e.printStackTrace (). }} / * settings can be accessed directly through postman or browser * * @ param response response * @ param bufferedImage bufferedImage * @ throws Exception e * / public void responseImage (HttpServletResponse response, BufferedImage bufferedImage) throws Exception {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (); ImageOutputStream imageOutput = ImageIO.createImageOutputStream (byteArrayOutputStream); ImageIO.write (bufferedImage, "jpeg", imageOutput) InputStream inputStream = new ByteArrayInputStream (byteArrayOutputStream.toByteArray ()); OutputStream outputStream = response.getOutputStream (); response.setContentType ("image/jpeg"); response.setCharacterEncoding ("UTF-8"); IOUtils.copy (inputStream, outputStream); outputStream.flush () }} about how to generate a QR code in Zxing and return it, I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.