java生成二维码

1、我用的是maven,导入依赖包


 
 
  1. <!-- 二维码 -->
  2. <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
  3. <dependency>
  4. <groupId>com.google.zxing </groupId>
  5. <artifactId>core </artifactId>
  6. <version>2.1 </version>
  7. </dependency>
  8. <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
  9. <dependency>
  10. <groupId>com.google.zxing </groupId>
  11. <artifactId>javase </artifactId>
  12. <version>2.1 </version>
  13. </dependency>

2、如果你像我一样是菜鸟,记得右键项目》maven》update project

3、贴代码


 
 
  1. package com.jusfoun.jxsd.util;
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.util.Hashtable;
  12. import javax.imageio.ImageIO;
  13. import com.google.zxing.BarcodeFormat;
  14. import com.google.zxing.BinaryBitmap;
  15. import com.google.zxing.EncodeHintType;
  16. import com.google.zxing.LuminanceSource;
  17. import com.google.zxing.ReaderException;
  18. import com.google.zxing.Result;
  19. import com.google.zxing.WriterException;
  20. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
  21. import com.google.zxing.common.BitMatrix;
  22. import com.google.zxing.common.HybridBinarizer;
  23. import com.google.zxing.qrcode.QRCodeReader;
  24. import com.google.zxing.qrcode.QRCodeWriter;
  25. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  26. /**
  27. * 二维码生成和读的工具类
  28. *
  29. */
  30. public class QrCodeUtil {
  31. /**
  32. * 生成包含字符串信息的二维码图片
  33. * @param outputStream 文件输出流路径
  34. * @param content 二维码携带信息
  35. * @param qrCodeSize 二维码图片大小
  36. * @param imageFormat 二维码的格式
  37. * @throws WriterException
  38. * @throws IOException
  39. */
  40. public static boolean createQrCode(String filePath, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{
  41. //设置二维码纠错级别MAP
  42. Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
  43. hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别
  44. QRCodeWriter qrCodeWriter = new QRCodeWriter();
  45. //创建比特矩阵(位矩阵)的QR码编码的字符串
  46. BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
  47. // 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
  48. int matrixWidth = byteMatrix.getWidth();
  49. BufferedImage image = new BufferedImage(matrixWidth- 200, matrixWidth- 200, BufferedImage.TYPE_INT_RGB);
  50. image.createGraphics();
  51. Graphics2D graphics = (Graphics2D) image.getGraphics();
  52. graphics.setColor(Color.WHITE);
  53. graphics.fillRect( 0, 0, matrixWidth, matrixWidth);
  54. // 使用比特矩阵画并保存图像
  55. graphics.setColor(Color.BLACK);
  56. for ( int i = 0; i < matrixWidth; i++){
  57. for ( int j = 0; j < matrixWidth; j++){
  58. if (byteMatrix.get(i, j)){
  59. graphics.fillRect(i- 100, j- 100, 1, 1);
  60. }
  61. }
  62. }
  63. OutputStream outputStream= new FileOutputStream( new File(filePath));
  64. return ImageIO.write(image, imageFormat, outputStream);
  65. }
  66. /**
  67. * 读二维码并输出携带的信息
  68. */
  69. public static void readQrCode(InputStream inputStream) throws IOException{
  70. //从输入流中获取字符串信息
  71. BufferedImage image = ImageIO.read(inputStream);
  72. //将图像转换为二进制位图源
  73. LuminanceSource source = new BufferedImageLuminanceSource(image);
  74. BinaryBitmap bitmap = new BinaryBitmap( new HybridBinarizer(source));
  75. QRCodeReader reader = new QRCodeReader();
  76. Result result = null ;
  77. try {
  78. result = reader.decode(bitmap);
  79. } catch (ReaderException e) {
  80. e.printStackTrace();
  81. }
  82. System.out.println(result.getText());
  83. }
  84. /**
  85. * 生成讲课二维码
  86. * @param content
  87. * @return
  88. * @throws WriterException
  89. * @throws IOException
  90. */
  91. public static String createTeachQRCode(String id) throws WriterException, IOException {
  92. String qrCodeUrl= "/qrcode/"+id+ ".jpg";
  93. createQrCode( "src\\main\\webapp\\qrcode\\"+id+ ".jpg",id, 900, "JPEG");
  94. return qrCodeUrl;
  95. }
  96. /**
  97. * 测试代码
  98. * @throws WriterException
  99. */
  100. public static void main(String[] args) throws IOException, WriterException {
  101. //createQrCode(new FileOutputStream(new File("d:\\qrcode.jpg")),"china is good",900,"JPEG");
  102. createQrCode( "src\\main\\webapp\\qrcode\\qrcode.jpg", "china is good", 900, "JPEG");
  103. readQrCode( new FileInputStream( new File( "d:\\qrcode.jpg")));
  104. }
  105. }



        </div>
            </div>
        </article>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值