Java生成二維碼

1,下載jar包(QRCode.jar)

maven依賴

  1. <dependency>java

  2. <groupId>QRCode</groupId>web

  3. <artifactId>QRCode</artifactId>ajax

  4. <version>3.0</version>spring

  5. </dependency>app

2,編寫實體類實現二維碼的生成

二維碼工具類

  1. public class CreateQRCode {maven

  2.  

  3. /**ide

  4. * 建立二維碼工具

  5. * @param qrData 生成二維碼中要存儲的信息this

  6. * @param path 二維碼圖片存儲路徑 eg:"D:/qrcode.png"spa

  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的二維碼圖片:

相關文章
相關標籤/搜索