java產生二維碼

來源:互聯網
上載者:User

標籤:輸出   int   span   awt   stack   sys   not   bytes   tac   

具體代碼如下,作為一個新手,期待與你一起交流:

 1 import java.awt.Color;   2 import java.awt.Graphics2D;   3 import java.awt.image.BufferedImage;   4 import java.io.File;   5    6 import javax.imageio.ImageIO;   7    8 import com.swetake.util.Qrcode; 9 public class QRCodeEncoderHandler {10    /**11     * 產生二維碼(QRCode)圖片12     * @param content13     * @param imgPath14     */15     public void encoderQRCode(String content,String imgPath){16         try{17             Qrcode qrcodeHandler = new Qrcode();  18             qrcodeHandler.setQrcodeErrorCorrect(‘M‘);  19             qrcodeHandler.setQrcodeEncodeMode(‘B‘);  20             qrcodeHandler.setQrcodeVersion(7);  21   22             System.out.println(content);  23             byte[] contentBytes = content.getBytes("utf-8");  24   25             BufferedImage bufImg = new BufferedImage(140, 140,  26                     BufferedImage.TYPE_INT_RGB);  27   28             Graphics2D gs = bufImg.createGraphics();  29   30             gs.setBackground(Color.WHITE);  31             gs.clearRect(0, 0,30000,30000);  32   33             // 設定映像顏色 > BLACK  34             gs.setColor(new Color(0,0,0,255));  35   36             // 設定位移量 不設定可能導致解析出錯  37             int pixoff = 2;  38             // 輸出內容 > 二維碼  39             if (contentBytes.length > 0 && contentBytes.length < 120) {  40                 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);  41                 for (int i = 0; i < codeOut.length; i++) {  42                     for (int j = 0; j < codeOut.length; j++) {  43                         if (codeOut[j][i]) {  44                             gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);  45                         }  46                     }  47                 }  48             } else {  49                 System.err.println("QRCode content bytes length = "  50                         + contentBytes.length + " not in [ 0,120 ]. ");  51             }  52   53             gs.dispose();  54             bufImg.flush();  55   56             File imgFile = new File(imgPath);  57   58             // 產生二維碼QRCode圖片  59             ImageIO.write(bufImg, "png", imgFile);   60         }catch (Exception e) {61             // TODO: handle exception62             e.printStackTrace(); 63         }64     }65     public static void main(String[] args) {66         String imgPath = "D://Michael.jpg";  67           68         String content = "姓名:**"  69                 + "\n\r電話:135********"  70                 + "\n\rEmail:******[email protected]" + "\n\rEmail2:******@163.com" +"\n\rQQ :******";  71   72         QRCodeEncoderHandler handler = new QRCodeEncoderHandler();  73         handler.encoderQRCode(content, imgPath);  74   75         System.out.println("encoder QRcode success");  76     }77 }

 

java產生二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.