標籤:輸出 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產生二維碼