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. * 创建二维码

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

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

  7. * @throws Exception

  8. */

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

  10. try {

  11. Qrcode qrcode = new Qrcode();

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

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

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

  15.  

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

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

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

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

  20. //绘图

  21. Graphics2D gs = bufferedImage.createGraphics();

  22. gs.setBackground(Color.WHITE);

  23. gs.setColor(Color.BLACK);

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

  25.  

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

  27. int pixoff = 2;

  28.  

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

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

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

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

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

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

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

  36. }

  37. }

  38. }

  39. }

  40. gs.dispose();

  41. bufferedImage.flush();

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

  43. return true;

  44. } catch (IOException e) {

  45. e.printStackTrace();

  46. return false;

  47. }

  48. }

  49.  

  50. /**

  51. * 解析二维码(QRCode)

  52. *

  53. * @param imgPath 图片路径

  54. * @return

  55. */

  56. public static String decoderQRCode(String imgPath) {

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

  58. File imageFile = new File(imgPath);

  59. BufferedImage bufImg = null;

  60. String content = null;

  61. try {

  62. bufImg = ImageIO.read(imageFile);

  63. QRCodeDecoder decoder = new QRCodeDecoder();

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

  65. } catch (IOException e) {

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

  67. e.printStackTrace();

  68. } catch (DecodingFailedException dfe) {

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

  70. dfe.printStackTrace();

  71. }

  72. return content;

  73. }

  74.  

  75. }

 

二维码基础类

  1. class TwoDimensionCodeImage implements QRCodeImage {

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

  3. BufferedImage bufImg;

  4. public TwoDimensionCodeImage(BufferedImage bufImg) {

  5. this.bufImg = bufImg;

  6. }

  7.  

  8. @Override

  9. public int getWidth() {

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

  11. }

  12.  

  13. @Override

  14. public int getHeight() {

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

  16. }

  17.  

  18. @Override

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

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

  21. }

  22. }

3.controller调用

  1. package com.st.project.controller;

  2.  

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

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

  5. import org.springframework.stereotype.Controller;

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

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

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

  9.  

  10. import javax.servlet.http.HttpServletRequest;

  11.  

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

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

  14.  

  15. /**

  16. * 创建二维码

  17. */

  18. @Controller

  19. @RequestMapping("/qrcode")

  20. public class QrcodeController {

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

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

  23.  

  24. /**

  25. * 创建二维码

  26. * @return

  27. */

  28. @ResponseBody

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

  30. public AjaxResult addQrcode(HttpServletRequest request){

  31. AjaxResult ajaxResult = new AjaxResult();

  32. ajaxResult.setState(false);

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

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

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

  36. String path=qrcodePath+qrcode;

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

  38. if(getQrcode==true){

  39. ajaxResult.setState(true);

  40. ajaxResult.setData(qrcode);

  41. }

  42. return ajaxResult;

  43. }

  44.  

  45. /**

  46. * 解析二维码

  47. * @return

  48. */

  49. @ResponseBody

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

  51. public AjaxResult decoderQrcode(HttpServletRequest request){

  52. AjaxResult ajaxResult = new AjaxResult();

  53. ajaxResult.setState(false);

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

  55.  

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

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

  58. ajaxResult.setState(true);

  59. ajaxResult.setData(qrData);

  60. }

  61. return ajaxResult;

  62. }

  63.  

  64. }

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

 

posted on 2019-03-05 10:07  我是司  阅读(263)  评论(0编辑  收藏  举报

导航