Java 生成二维码

来源:互联网 发布:985 211 区别知乎 编辑:程序博客网 时间:2024/06/05 00:07
  1. package Test;  
  2.    
  3. importjava.awt.Color;  
  4. importjava.awt.Graphics2D;  
  5. importjava.awt.image.BufferedImage;  
  6. importjava.io.File;  
  7.    
  8. importjavax.imageio.ImageIO;  
  9.    
  10. importcom.swetake.util.Qrcode;  
  11.    
  12. public class ErWeiMa{  
  13.    /** 
  14.      * 生成二维码(QRCode)图片 
  15.      * @paramcontent 
  16.      * @paramimgPath 
  17.      */   
  18.     public voidencoderQRCode(String content, String imgPath) {   
  19.         try {   
  20.    
  21.             Qrcode qrcodeHandler =newQrcode();   
  22.            qrcodeHandler.setQrcodeErrorCorrect('M');   
  23.             qrcodeHandler.setQrcodeEncodeMode('B');   
  24.            qrcodeHandler.setQrcodeVersion(7);   
  25.    
  26.             System.out.println(content);   
  27.             byte[]contentBytes = content.getBytes("UTF-8");   
  28.    
  29.             BufferedImage bufImg =newBufferedImage(140140,   
  30.                     BufferedImage.TYPE_INT_RGB);   
  31.    
  32.             Graphics2D gs =bufImg.createGraphics();   
  33.    
  34.             gs.setBackground(Color.WHITE);   
  35.             gs.clearRect(00140140);   
  36.    
  37.             // 设定图像颜色 > BLACK   
  38.             gs.setColor(Color.BLACK);   
  39.    
  40.             // 设置偏移量 不设置可能导致解析出错   
  41.             int pixoff =2;   
  42.             // 输出内容 > 二维码   
  43.             if(contentBytes.length > 0 && contentBytes.length <120) {   
  44.                 boolean[][]codeOut = qrcodeHandler.calQrcode(contentBytes);   
  45.                 for (int i = 0; i< codeOut.length; i++) {   
  46.                     for (int j = 0; j< codeOut.length; j++) {   
  47.                         if(codeOut[j][i]) {   
  48.                             gs.fillRect(j * 3 +pixoff, i * 3 + pixoff, 33);   
  49.                         }   
  50.                     }   
  51.                 }   
  52.             } else {   
  53.                 System.err.println("QRCodecontent bytes length = "   
  54.                         + contentBytes.length + "not in [ 0,120 ]. ");   
  55.             }   
  56.    
  57.             gs.dispose();   
  58.             bufImg.flush();   
  59.    
  60.             File imgFile = newFile(imgPath);   
  61.    
  62.             // 生成二维码QRCode图片   
  63.             ImageIO.write(bufImg, "png",imgFile);   
  64.    
  65.         } catch(Exception e) {   
  66.             e.printStackTrace();   
  67.         }   
  68.    
  69.     }   
  70.    
  71.     /** 
  72.      * @param argsthe command line arguments 
  73.      */   
  74.     public static voidmain(String[] args) {   
  75.         String imgPath = "D:/EWM/ewm.png";   
  76.    
  77.         String content = "";   
  78.    
  79.         ErWeiMa handler = newErWeiMa();   
  80.         handler.encoderQRCode(content,imgPath);   
  81.    
  82.         System.out.println("encoderQRcode success");   
  83.     }   
  84. }  
原创粉丝点击