背景介紹:
最近我們這邊需要做一個(gè)自動(dòng)生成二維碼并且還要將二維碼嵌入現(xiàn)有的背景模板中,一直又沒(méi)寫過(guò)這些方面的東西,突然寫還真是有點(diǎn)迷糊,特別是對(duì)于java圖片API并不很熟悉,所以參考了很多網(wǎng)友的分享,很快做出來(lái)了,現(xiàn)在分享一下
注意:
關(guān)于二維碼的生成方式在文章java生成帶logo的多彩二維碼 已經(jīng)寫過(guò)了,大家可以合起來(lái)一起使用。
實(shí)例代碼:
1
2
3
4
5
6
7
8
9
10
|
BufferedImage image= bim; //二維碼 BufferedImage bg= ImageIO.read(路徑); //獲取北京圖片 Graphics2D g=bg.createGraphics(); int width=image.getWidth( null ) > bg.getWidth() * 5 / 10 ? (bg.getWidth() * 5 / 10 ) : image.getWidth( null ); int height=image.getHeight( null ) > bg.getHeight() * 5 / 10 ? (bg.getHeight() * 5 / 10 ) : image.getWidth( null ); g.drawImage(image,(bg.getWidth()- width)/ 2 ,(bg.getHeight()-height)/ 2 ,width,height, null ); g.dispose(); bg.flush(); image.flush(); ImageIO.write(bg, "png" , file); |
二維碼的效果圖:
總結(jié)
以上就是利用java實(shí)現(xiàn)自動(dòng)生成二維碼和現(xiàn)有的背景圖合并的內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)。