背景介紹:
最近我們這邊需要做一個自動生成二維碼并且還要將二維碼嵌入現有的背景模板中,一直又沒寫過這些方面的東西,突然寫還真是有點迷糊,特別是對于java圖片API并不很熟悉,所以參考了很多網友的分享,很快做出來了,現在分享一下
注意:
關于二維碼的生成方式在文章java生成帶logo的多彩二維碼 已經寫過了,大家可以合起來一起使用。
實例代碼:
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); |
二維碼的效果圖:
總結
以上就是利用java實現自動生成二維碼和現有的背景圖合并的內容,有需要的朋友們可以參考學習。