标签:输出 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 content 13 * @param imgPath 14 */ 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 exception 62 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:******4@qq.com" + "\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 }
标签:输出 int span awt stack sys not bytes tac
原文地址:http://www.cnblogs.com/caoleiCoding/p/6372031.html