Java生成二维码

1,下载jar包(QRCode.jar)

maven依赖

  1. <dependency>

  2. <groupId>QRCode</groupId>

  3. <artifactId>QRCode</artifactId>

  4. <version>3.0</version>

  5. </dependency>

2,编写实体类实现二维码的生成

二维码工具类

  1. public class CreateQRCode {

  2. /**

  3. * 创建二维码

  4. * @param qrData 生成二维码中要存储的信息

  5. * @param path 二维码图片存储路径 eg:"D:/qrcode.png"

  6. * @throws Exception

  7. */

  8. public static boolean creatQrcode(String qrData, String path) {

  9. try {

  10. Qrcode qrcode = new Qrcode();

  11. qrcode.setQrcodeErrorCorrect('M');//纠错等级(分为L、M、H三个等级)

  12. qrcode.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其它字符

  13. qrcode.setQrcodeVersion(7);//版本

  14. //设置一下二维码的像素

  15. int width = 67 + 12 * (7 - 1);

  16. int height = 67 + 12 * (7 - 1);

  17. BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

  18. //绘图

  19. Graphics2D gs = bufferedImage.createGraphics();

  20. gs.setBackground(Color.WHITE);

  21. gs.setColor(Color.BLACK);

  22. gs.clearRect(0, 0, width, height);//清除下画板内容

  23. //设置下偏移量,如果不加偏移量,有时会导致出错。

  24. int pixoff = 2;

  25. byte[] d = qrData.getBytes("utf-8");

  26. if (d.length > 0 && d.length < 120) {

  27. boolean[][] s = qrcode.calQrcode(d);

  28. for (int i = 0; i < s.length; i++) {

  29. for (int j = 0; j < s.length; j++) {

  30. if (s[j][i]) {

  31. gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);

  32. }

  33. }

  34. }

  35. }

  36. gs.dispose();

  37. bufferedImage.flush();

  38. ImageIO.write(bufferedImage, "png", new File(path));

  39. return true;

  40. } catch (IOException e) {

  41. e.printStackTrace();

  42. return false;

  43. }

  44. }

  45. /**

  46. * 解析二维码(QRCode)

  47. *

  48. * @param imgPath 图片路径

  49. * @return

  50. */

  51. public static String decoderQRCode(String imgPath) {

  52. //QRCode 二维码图片的文件

  53. File imageFile = new File(imgPath);

  54. BufferedImage bufImg = null;

  55. String content = null;

  56. try {

  57. bufImg = ImageIO.read(imageFile);

  58. QRCodeDecoder decoder = new QRCodeDecoder();

  59. content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8");

  60. } catch (IOException e) {

  61. System.out.println("Error: " + e.getMessage());

  62. e.printStackTrace();

  63. } catch (DecodingFailedException dfe) {

  64. System.out.println("Error: " + dfe.getMessage());

  65. dfe.printStackTrace();

  66. }

  67. return content;

  68. }

  69. }

二维码基础类

  1. class TwoDimensionCodeImage implements QRCodeImage {

  2. //BufferedImage作用将一幅图片加载到内存中

  3. BufferedImage bufImg;

  4. public TwoDimensionCodeImage(BufferedImage bufImg) {

  5. this.bufImg = bufImg;

  6. }

  7. @Override

  8. public int getWidth() {

  9. return bufImg.getWidth();//返回像素宽度

  10. }

  11. @Override

  12. public int getHeight() {

  13. return bufImg.getHeight();//返回像素高度

  14. }

  15. @Override

  16. public int getPixel(int i, int i1) {

  17. return bufImg.getRGB(i, i1);//得到长宽值,即像素值,i,i1代表像素值

  18. }

  19. }

3.controller调用

  1. package com.st.project.controller;

  2. import com.st.project.common.AjaxResult;

  3. import org.springframework.beans.factory.annotation.Value;

  4. import org.springframework.stereotype.Controller;

  5. import org.springframework.web.bind.annotation.PostMapping;

  6. import org.springframework.web.bind.annotation.RequestMapping;

  7. import org.springframework.web.bind.annotation.ResponseBody;

  8. import javax.servlet.http.HttpServletRequest;

  9. import static com.st.project.common.CreateQRCode.creatQrcode;

  10. import static com.st.project.common.CreateQRCode.decoderQRCode;

  11. /**

  12. * 创建二维码

  13. */

  14. @Controller

  15. @RequestMapping("/qrcode")

  16. public class QrcodeController {

  17. @Value("${portals.upload.image.path}")

  18. private String qrcodePath; //二维码存储路径

  19. /**

  20. * 创建二维码

  21. * @return

  22. */

  23. @ResponseBody

  24. @PostMapping("/add.dd")

  25. public AjaxResult addQrcode(HttpServletRequest request){

  26. AjaxResult ajaxResult = new AjaxResult();

  27. ajaxResult.setState(false);

  28. String qrData=request.getParameter("qrData");

  29. String qrSuffix=request.getParameter("qrSuffix");

  30. String qrcode=System.currentTimeMillis()+"."+qrSuffix;

  31. String path=qrcodePath+qrcode;

  32. boolean getQrcode=creatQrcode(qrData,path);

  33. if(getQrcode==true){

  34. ajaxResult.setState(true);

  35. ajaxResult.setData(qrcode);

  36. }

  37. return ajaxResult;

  38. }

  39. /**

  40. * 解析二维码

  41. * @return

  42. */

  43. @ResponseBody

  44. @PostMapping("/decoder.dd")

  45. public AjaxResult decoderQrcode(HttpServletRequest request){

  46. AjaxResult ajaxResult = new AjaxResult();

  47. ajaxResult.setState(false);

  48. String qrcode=request.getParameter("qrcode");

  49. String qrData=decoderQRCode(qrcodePath+qrcode);

  50. if(qrData!=null && !"".equals(qrData)){

  51. ajaxResult.setState(true);

  52. ajaxResult.setData(qrData);

  53. }

  54. return ajaxResult;

  55. }

  56. }

此时已生成一张名为qrcode.png的二维码图片:

原文地址:https://www.cnblogs.com/chinaifae/p/10474982.html