- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- JAVA如何實(shí)現二維碼生成加背景圖
小編給大家分享一下JAVA如何實(shí)現二維碼生成加背景圖,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
pom.xml依賴(lài)
<!-- 二維碼生成 --> <!-- https://mvnrepository.com/artifact/com.google.zxing/core --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.0.1</version> </dependency> /** * 類(lèi)名稱(chēng):QRCodeMax * 類(lèi)描述:生成二維碼圖片+背景+文字描述工具類(lèi) * 創(chuàng )建人:一個(gè)除了帥氣,一無(wú)是處的男人 * 創(chuàng )建時(shí)間:2018年12月x日x點(diǎn)x分x秒 * 修改時(shí)間:2019年2月x日x點(diǎn)x分x秒 * 修改備注:更新有參數構造 * @version: 2.0 * */public class QRCodeMax { //文字顯示 private static final int QRCOLOR = 0x201f1f; // 二維碼顏色:黑色 private static final int BGWHITE = 0xFFFFFF; //二維碼背景顏色:白色 // 設置QR二維碼參數信息 private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() { private static final long serialVersionUID = 1L; { put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 設置QR二維碼的糾錯級別(H為最高級別) put(EncodeHintType.CHARACTER_SET, "utf-8");// 設置編碼方式 put(EncodeHintType.MARGIN, 0);// 白邊 } }; /** * 生成二維碼圖片+背景+文字描述 * @param codeFile 生成圖地址 * @param bgImgFile 背景圖地址 * @param WIDTH 二維碼寬度 * @param HEIGHT 二維碼高度 * @param qrUrl 二維碼識別地址 * @param note 文字描述1 * @param tui 文字描述2 * @param size 文字大小 * @param imagesX 二維碼x軸方向 * @param imagesY 二維碼y軸方向 * @param text1X 文字描述1x軸方向 * @param text1Y 文字描述1y軸方向 * @param text2X 文字描述2x軸方向 * @param text2Y 文字描述2y軸方向 */ public static void CreatQRCode( File codeFile, File bgImgFile,Integer WIDTH,Integer HEIGHT,String qrUrl, String note,String tui,Integer size,Integer imagesX,Integer imagesY,Integer text1X,Integer text1Y ,Integer text2X,Integer text2Y) { try { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); // 參數順序分別為: 編碼內容,編碼類(lèi)型,生成圖片寬度,生成圖片高度,設置參數 BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints); BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 開(kāi)始利用二維碼數據創(chuàng )建Bitmap圖片,分別設為黑(0xFFFFFFFF) 白(0xFF000000)兩色 for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE); } } /* * 添加背景圖片 */ BufferedImage backgroundImage = ImageIO.read(bgImgFile); int bgWidth=backgroundImage.getWidth(); int qrWidth=image.getWidth(); //距離背景圖片x邊的距離,居中顯示 int disx=(bgWidth-qrWidth)-imagesX; //距離y邊距離 * * * * int disy=imagesY; Graphics2D rng=backgroundImage.createGraphics(); rng.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP)); rng.drawImage(image,disx,disy,WIDTH,HEIGHT,null); /* * 文字描述參數設置 */ Color textColor=Color.white; rng.setColor(textColor); rng.drawImage(backgroundImage,0,0,null); //設置字體類(lèi)型和大小(BOLD加粗/ PLAIN平常) rng.setFont(new Font("微軟雅黑,Arial",Font.BOLD,size)); //設置字體顏色 rng.setColor(Color.black); int strWidth=rng.getFontMetrics().stringWidth(note); //文字1顯示位置 int disx1=(bgWidth-strWidth)-text1X;//左右 rng.drawString(note,disx1,text1Y);//上下 //文字2顯示位置 int disx2=(bgWidth-strWidth)-text2X;//左右 rng.drawString(tui,disx2,text2Y);//上下 rng.dispose(); image=backgroundImage; image.flush(); ImageIO.write(image, "png", codeFile); } catch (Exception e) { e.printStackTrace(); } } /** * 測試 * @param args */ public static void main(String[] args) { File bgImgFile=new File("D://tu/bg.png");//背景圖片 File QrCodeFile = new File("D://tu/myqrcode.png");//生成圖片位置 String url = "https://blog.csdn.net/weixin_38407595";//二維碼鏈接 String note = "" ;//文字描述 String tui = "" ;//文字描述 //宣傳二維碼生成 //生成圖地址,背景圖地址,二維碼寬度,二維碼高度,二維碼識別地址,文字描述1,文字描述2,文字大小,圖片x軸方向,圖片y軸方向,文字1||2xy軸方向 CreatQRCode(QrCodeFile,bgImgFile, 148, 148, url, note,tui, 38, 408, 123, 0, 0, 410, 210); }}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站