当前位置: 代码迷 >> 综合 >> 使用Java (Google zxing) 在后端生成二维码
  详细解决方案

使用Java (Google zxing) 在后端生成二维码

热度:48   发布时间:2023-11-22 21:05:59.0

首先在Pom中导入依赖

<!-- 后端生成二维码工具类--><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.4.0</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.0</version></dependency>

创建工具类 QRCodeGenUtil

package com.example.demo.util;import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import java.io.File;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.HashMap;public class QRCodeGenUtil {//生成市场二维码public void getMarketCode(String salmarketId,String marketName){int width=450;//图片宽int height=450;//图片高度String content;String format="png";//正式环境/*String url="*.*.*.*";*///开发环境String url="*.*.*.*";content="http://"+url+":8088/sxmarket/wechat/checkScan?salmarketId="+salmarketId;//String content="http://*.*.*.*:8088/sxmarket/wechat/checkDetails?salmarketId=43&operator=840";//String content="http://localhost:8088/sxmarket/wechat/checkDetails?salmarketId=43&operator=840&goodsinfoId=20&start=2018-12-11&end=2019-04-23";//定义二维码参数HashMap hints=new HashMap();hints.put(EncodeHintType.CHARACTER_SET,"utf-8");hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);hints.put(EncodeHintType.MARGIN,2);//生成二维码try {String pathname="D:/"+marketName+"二维码.png";BitMatrix bitMatrix= new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);Path file=new File(pathname).toPath();MatrixToImageWriter.writeToPath(bitMatrix,format,file);String osName = System.getProperty("os.name", "");// 获取操作系统的名字if (osName.startsWith("Windows")) {// windowsRuntime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pathname);} else if (osName.startsWith("Mac OS")) {// MacClass fileMgr = Class.forName("com.apple.eio.FileManager");Method openURL = fileMgr.getDeclaredMethod("openURL", String.class);openURL.invoke(null, url);} else {// Unix or LinuxString[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};String browser = null;for (int count = 0; count < browsers.length && browser == null; count++) { // 执行代码,在brower有值后跳出,// 这里是如果进程创建成功了,==0是表示正常结束。if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) {browser = browsers[count];}}if (browser == null) {throw new RuntimeException("未找到任何可用的浏览器");} else {// 这个值在上面已经成功的得到了一个进程。Runtime.getRuntime().exec(new String[]{browser, url});}}}catch(Exception e){e.printStackTrace();}}//生成经营户的二维码public void getOperatorCode(String salmarketId,String operator,String operatorName){int width=450;int height=450;String content;String format="png";//正式环境//String url="*.*.*.*";//开发环境String url="*.*.*.*";content="http://"+url+":8088/sxmarket/wechat/checkScan?salmarketId="+salmarketId+"&operator="+operator;//定义二维码参数HashMap hints=new HashMap();hints.put(EncodeHintType.CHARACTER_SET,"utf-8");hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);hints.put(EncodeHintType.MARGIN,2);//生成二维码try {String pathname="D:/"+operatorName+"二维码.png";BitMatrix bitMatrix= new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);Path file=new File(pathname).toPath();MatrixToImageWriter.writeToPath(bitMatrix,format,file);String osName = System.getProperty("os.name", "");// 获取操作系统的名字if (osName.startsWith("Windows")) {// windowsRuntime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pathname);} else if (osName.startsWith("Mac OS")) {// MacClass fileMgr = Class.forName("com.apple.eio.FileManager");Method openURL = fileMgr.getDeclaredMethod("openURL", String.class);openURL.invoke(null, url);} else {// Unix or LinuxString[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};String browser = null;for (int count = 0; count < browsers.length && browser == null; count++) { // 执行代码,在brower有值后跳出,// 这里是如果进程创建成功了,==0是表示正常结束。if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) {browser = browsers[count];}}if (browser == null) {throw new RuntimeException("未找到任何可用的浏览器");} else {// 这个值在上面已经成功的得到了一个进程。Runtime.getRuntime().exec(new String[]{browser, url});}}}catch(Exception e){e.printStackTrace();}}
}

 

  相关解决方案