java后台生成二维码
需求:生成的二维码可以在微信、支付宝、等手机扫码设备可识别内容!
1.导入maven
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency>
2.1:先举例生成二维码保存本地:
/**
* 存放本地固定路径
*/
private static final String QR_CODE_IMAGE_PATH = "F:\\kedaSoft\\appinterface\\WebRoot\\ycs.png";
private static void generateQRCodeImage(String text, Integer width, Integer height, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); Path path = FileSystems.getDefault().getPath(filePath); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); } public static void main(String[] args) { try { generateQRCodeImage("测试,保存在本地的二维码", 350, 350, QR_CODE_IMAGE_PATH); } catch (WriterException e) { System.out.println("写入异常 :: " + e.getMessage()); } catch (IOException e) { System.out.println("io异常 IOException :: " + e.getMessage()); } }
如图所示:找到保存的路径查看:
2.2:举例生成二维码不保存本地使用web浏览器访问:
/** * * @param text 扫码显示内容 * @param width 二维码长度 * @param height 二维码高度 * @return 返回byte数组 * @throws WriterException 写入异常 * @throws IOException io异常 */ public static byte[] getQRCodeImage(String text, Integer width, Integer height) throws WriterException, IOException{ QRCodeWriter qrCodeWriter = new QRCodeWriter(); // 解决中文乱码 HashMap<EncodeHintType, Object> hints = new HashMap<>(16); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix encode = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(encode,"PNG",byteArrayOutputStream); return byteArrayOutputStream.toByteArray(); }
// 这里我是用的jfinal框架,如你用的是spring:加上RequestMaping(“/qrCode”)即可。
public void qrCode(){ HttpServletResponse response = this.getResponse(); //二维码内的信息 String info = "二维码内的信息"; try { byte[] qrCode = QrCodeGenerator.getQRCodeImage(info, 360, 360); OutputStream outputStream = response.getOutputStream(); outputStream.write(qrCode); response.setCharacterEncoding("utf-8"); response.setContentType("image/png"); outputStream.flush(); } catch (WriterException e) { System.out.println("写入异常 :: " + e.getMessage()); } catch (IOException e) { System.out.println("io异常 IOException :: " + e.getMessage()); } }
3.:全部放到一个工具类:
package com.wom.utils; /** * @author ycs * @date 2022/2/14 */ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.HashMap; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QrCodeGenerator { /** * 存放本地固定路径 */ private static final String QR_CODE_IMAGE_PATH = "F:\\kedaSoft\\appinterface\\WebRoot\\ycs.png"; private static void generateQRCodeImage(String text, Integer width, Integer height, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); Path path = FileSystems.getDefault().getPath(filePath); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); } /** * * @param text 扫码显示内容 * @param width 二维码长度 * @param height 二维码高度 * @return 返回byte数组 * @throws WriterException 写入异常 * @throws IOException io异常 */ public static byte[] getQRCodeImage(String text, Integer width, Integer height) throws WriterException, IOException{ QRCodeWriter qrCodeWriter = new QRCodeWriter(); // 解决中文乱码 HashMap<EncodeHintType, Object> hints = new HashMap<>(16); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix encode = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(encode,"PNG",byteArrayOutputStream); return byteArrayOutputStream.toByteArray(); } public static void main(String[] args) { try { generateQRCodeImage("测试,保存在本地的二维码", 350, 350, QR_CODE_IMAGE_PATH); } catch (WriterException e) { System.out.println("写入异常 :: " + e.getMessage()); } catch (IOException e) { System.out.println("io异常 IOException :: " + e.getMessage()); } } }