Springboot中需要生成二維碼
首先,在pom.xml中添加依賴的包:
<!--添加二維碼生成相關的jar包-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
直接上代碼:
package com.chess.utils;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class FilepathUtils {
/**
* 解析二維碼解析
*/
public static String analyzeEncode(String path) {
String content = null;
BufferedImage image;
try {
image = ImageIO.read(new File(path));
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 對圖像進行解碼
System.out.println("圖檔中内容: ");
System.out.println("author: " + result.getText());
content = result.getText();
} catch (IOException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
return content;
}
/**
* 生成二維碼
*
* @param content
* @throws Exception
*/
public static String getEncode(String content) throws Exception {
String mkdir = "D://qRCode/";
File f = new File(mkdir);
if (!f.exists())
f.mkdirs();
String string = UUIDUtils.getQRCodePath();
String path = mkdir + string + ".png";
int width = ; // 圖像寬度
int height = ; // 圖像高度
String format = "png";// 圖像類型
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, width, height, hints);// 生成矩陣
Path path1 = Paths.get(PATH + path);
MatrixToImageWriter.writeToPath(bitMatrix, format, path1);// 輸出圖像
return path;
}
public static void main(String[] args) throws Exception {
String path = getEncode("我是小球小球---");
System.out.println(path);
}
}
當然了,我們把jar包下載下傳下來直接在普通項目中使用也是可以的,隻需要把我們的jar包上傳到項目中的就行了!!!
其中的UUIDUtils代碼如下:
package com.chess.utils;
import java.util.Date;
import java.util.UUID;
/**
* 關于UUID的工具類
*
* @author 球球
*/
public class UUIDUtils {
/**
* 随機生成一個ID
* RandomUUID+時間戳
*
* @return
*/
public static String randomId() {
return UUID.randomUUID().toString().replaceAll("-", "") + "-" + new Date().getTime();
}
/**
* 生成二維碼所在路徑
* @return
*/
public static String getQRCodePath(){
return UUID.randomUUID().toString().replaceAll("-", "") + "-" + new Date().getTime();
}
}
為什麼要使用UUIDUtils:
在實際的項目開發過程中,我們可能會遇到要生成很多二維碼的情況,是以為了保證二維碼的唯一性,使用了UUID!!!