- 生成二维码图片
- 网页生成二维码
生成二维码图片(中文乱码已处理)
package com.example.QRCode;
import com.alibaba.fastjson.JSONObject;
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;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
public class QRCode01 {
public static void main(String[] args) {
try {
//二维码包含信息内容
//存入一个对象
Map map=new HashMap();
map.put("id","1001");
map.put("name","花花");
//将json对象转化为json字符串
String text = JSONObject.toJSONString(map);
generateQRCodeImage(text, 350, 350, QR_CODE_IMAGE_PATH);
System.out.println("二维码已生成");
} catch (WriterException e) {
System.out.println( e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
//二维码图片生成路径
private static final String QR_CODE_IMAGE_PATH = "C:/Users/86153/Desktop/11.PNG";
//二维码生成方法
private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//中文扫码会乱码
HashMap map = new HashMap<>();
map.put(EncodeHintType.CHARACTER_SET, "utf-8");
//保存为二维码图片
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,map);
Path path = FileSystems.getDefault().getPath(filePath);
//生成图片存放路径
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
}
网页生成二维码(方法一样)
package com.example.QRCode;
import com.alibaba.fastjson.JSONObject;
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;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Controller
public class QRCode02 {
@GetMapping("/qrcode")
public String qrcode() {
return "qrcode";
}
@GetMapping(value="/qrimage")
public ResponseEntity getQRImage() {
//二维码包含信息内容
//存入一个对象
Map map=new HashMap();
map.put("id","1001");
map.put("name","花花");
//将json对象转化为json字符串
String info = JSONObject.toJSONString(map);
byte[] qrcode = null;
try {
qrcode = getQRCodeImage(info, 360, 360);
} catch (WriterException e) {
System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
}
// Set headers
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity (qrcode, headers, HttpStatus.CREATED);
}
public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//中文扫码会乱码
HashMap map = new HashMap<>();
map.put(EncodeHintType.CHARACTER_SET, "utf-8");
保存为二维码图片
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,map);
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
byte[] pngData = pngOutputStream.toByteArray();
return pngData;
}
}
qrcode.html
QrCodeImage



