資源簡介
java生成二維碼,且中間帶圖片(logo)的方法,且logo的具體位置可以自己設置,下載下來就能運行的

代碼片段和文件信息
package?code;
import?java.awt.Color;
import?java.awt.Graphics2D;
import?java.awt.Image;
import?java.awt.image.BufferedImage;
import?java.io.File;
import?javax.imageio.ImageIO;
import?com.swetake.util.Qrcode;
public?class?Logo_Two_Code?{
/**?
?????*?生成二維碼(QRCode)圖片?
?????*?@param?content?二維碼圖片的內容
?????*?@param?imgPath?生成二維碼圖片完整的路徑
?????*?@param?ccbpath??二維碼圖片中間的logo路徑
?????*/??
????public?static?int?createQRCode(String?content?String?imgPathString?ccbPath)?{??
????????try?{??
????????????Qrcode?qrcodeHandler?=?new?Qrcode();??
????????????//設置二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小??
????????????qrcodeHandler.setQrcodeErrorCorrect(‘M‘);??
????????????//N代表數字A代表字符a-ZB代表其他字符
????????????qrcodeHandler.setQrcodeEncodeMode(‘B‘);?
????????????//?設置設置二維碼版本,取值范圍1-40,值越大尺寸越大,可存儲的信息越大??
????????????qrcodeHandler.setQrcodeVersion(8);??
??
????????????byte[]?contentBytes?=?content.getBytes(“gb2312“);??
????????????BufferedImage?bufImg?=?new?BufferedImage(140?140?BufferedImage.TYPE_INT_RGB);??
????????????Graphics2D?gs?=?bufImg.createGraphics();??
??
????????????gs.setBackground(Color.WHITE);??
????????????gs.clearRect(0?0?140?140);??
??
????????????//?設定圖像顏色?>?BLACK??
????????????gs.setColor(Color.BLACK);??
??
????????????//?設置偏移量?不設置可能導致解析出錯??
????????????int?pixoff?=?2;??
????????????//?輸出內容?>?二維碼??
????????????if?(contentBytes.length?>?0?&&?contentBytes.length?<150)?{??
????????????????boolean[][]?codeOut?=?qrcodeHandler.calQrcode(contentBytes);??
????????????????for?(int?i?=?0;?i?????????????????????for?(int?j?=?0;?j?????????????????????????if?(codeOut[j][i])?{??
????????????????????????????gs.fillRect(j?*?3?+?pixoff?i?*?3?+?pixoff?3?3);??
????????????????????????}??
????????????????????}??
????????????????}??
????????????}?else?{??
????????????????System.err.println(“QRCode?content?bytes?length?=?“??
????????????????????????+?contentBytes.length?+?“?not?in?[?0125].?“);??
????????????????return?-1;
????????????}??
????????????Image?img?=?ImageIO.read(new?File(ccbPath));//實例化一個Image對象。
????????????gs.drawImage(img?50?50?null);
????????????gs.dispose();??
????????????bufImg.flush();??
??
????????????//?生成二維碼QRCode圖片??
????????????File?imgFile?=?new?File(imgPath);??
????????????ImageIO.write(bufImg?“png“?imgFile);??
??
????????}?catch?(Exception?e)?
????????{??
????????????e.printStackTrace();??
????????????return?-100;
????????}??
????????
????????return?0;
????}??
????public?static?void?main(String[]?args)?{
???? ?String?imgPath?=?“D:/tmpd/logo_QRCode.png“;?
???? ?String?imgPath1?=?“D:/tmpd/logo_QRCode.jpeg“;?
???? ?String?encoderContent?=?“菩提?千年(手機號:15876148239):你若不離不棄,我必生死相依。鞭策:1我的理想是什么?2我下一個目標在哪里?3我有能力做什么?4我的錢途呢幾何?“;
???? ?Logo_Two_Code?logo_Two_Code?=?new?Logo_Two_Code();
???? ?logo_Two_Code.createQRCode(encoderContent?imgPath?imgPath1);
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-03-07?17:17??java生成二維碼且中間帶圖片的方法\
?????文件????????3202??2013-03-07?17:10??java生成二維碼且中間帶圖片的方法\Logo_Two_Code.java
?????文件?????1099539??2013-01-25?09:57??java生成二維碼且中間帶圖片的方法\QRCode.jar
評論
共有 條評論