前言
二维码生产方案
Java后端生产
引入zxing包
1 2 3 4 5 6 7 8 9 10 11 12
| <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.4.0</version> </dependency>
|
生成二维码并用base64编码返回
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
@Slf4j public class QrcodeUtil {
public static String getBase64QRCode(String content, int width, int height) { MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.MARGIN, 3); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = null; try { bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e) { log.error(e.getMessage()); }
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix); byte[] bytes = imageToBytes(image, "jpg"); return Base64.getEncoder().encodeToString(bytes); }
private static byte[] imageToBytes(BufferedImage image, String type) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ImageIO.write(image, type, out); } catch (IOException e) { log.error(e.getLocalizedMessage()); } return out.toByteArray(); } }
|
web接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package cn.idea360.demo.modules.login;
import cn.idea360.demo.common.utils.QrcodeUtil; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.OutputStream; import java.util.Base64;
@RestController @RequestMapping("/qr") public class QrController {
@RequestMapping(value = "/base64qr") public String getQrByBase64() throws Exception { String base64QRCode = QrcodeUtil.getBase64QRCode("C:\\Users\\Administrator\\Desktop\\1.jpg", 400, 400); return "data:image/jpg;base64," + base64QRCode; } }
|
Vue前端生产
笔者采用的是npm包管理
1 2
| "@xkeshi/vue-qrcode": "^1.0.0", "qrcode": "^1.3.2",
|
组件引入
1 2 3 4 5 6 7
| import VueQrcode from '@xkeshi/vue-qrcode';
export default { components: { VueQrcode }, }
|
使用
1 2 3 4 5 6 7 8 9
| <el-table-column prop="deptQrcode" header-align="center" align="center" label="二维码"> <template slot-scope="scope"> <vue-qrcode :value="scope.row.deptQrcode" :options="{ size: 100 }"></vue-qrcode> </template> </el-table-column>
|
最后
本文到此结束,感谢阅读。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力。