生活随笔
收集整理的這篇文章主要介紹了
使用zxing生成二维码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用zxing批量在做好的立牌背景圖的指定位置上,把指定的文本內(nèi)容(鏈接地址、文本等)生成二維碼并放在該位置,
最后加上立牌編號。
步驟:
1).做好背景圖,如下圖:
掃一掃添加關(guān)注
2).生成二維碼BufferedImage對象。代碼如下:
[java] view plaincopy
?????????????????????public?static?BufferedImage?toBufferedImage(String?text,?int?width,??????????????int?height)?throws?Exception?{??????????int?BLACK?=?0xFF000000;??????????int?WHITE?=?0xFFFFFFFF;??????????Hashtable<EncodeHintType,?Object>?hints?=?new?Hashtable<EncodeHintType,?Object>();??????????hints.put(EncodeHintType.CHARACTER_SET,?"utf-8");???????????hints.put(EncodeHintType.MARGIN,?1);??????????BitMatrix?matrix?=?new?MultiFormatWriter().encode(text,??????????????????BarcodeFormat.QR_CODE,?width,?height,?hints);??????????BufferedImage?image?=?new?BufferedImage(width,?height,??????????????????BufferedImage.TYPE_INT_RGB);??????????for?(int?x?=?0;?x?<?width;?x++)?{??????????????for?(int?y?=?0;?y?<?height;?y++)?{??????????????????image.setRGB(x,?y,?matrix.get(x,?y)???BLACK?:?WHITE);??????????????}??????????}??????????return?image;??????}??
3).在立牌背景圖的指定位置上生成二維碼,代碼如下:
[java] view plaincopy
??????????????????????public?static?void?markImageByCode(Image?img,?String?srcImgPath,??????????????String?targerPath,?int?positionWidth,?int?positionHeight)?{??????????OutputStream?os?=?null;??????????try?{????????????????Image?srcImg?=?ImageIO.read(new?File(srcImgPath));????????????????BufferedImage?buffImg?=?new?BufferedImage(srcImg.getWidth(null),??????????????????????srcImg.getHeight(null),?BufferedImage.TYPE_INT_RGB);??????????????????????????????Graphics2D?g?=?buffImg.createGraphics();??????????????????????????????g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,??????????????????????RenderingHints.VALUE_INTERPOLATION_BILINEAR);????????????????g.drawImage(??????????????????????srcImg.getScaledInstance(srcImg.getWidth(null),??????????????????????????????srcImg.getHeight(null),?Image.SCALE_SMOOTH),?0,?0,??????????????????????null);????????????????g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,??????????????????????alpha));??????????????????????????????g.drawImage(img,?positionWidth,?positionHeight,?null);??????????????g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));????????????????????????????g.dispose();??????????????????????????????os?=?new?FileOutputStream(targerPath);??????????????ImageIO.write(buffImg,?"PNG",?os);????????????????System.out.println("二維碼圖片生成成功");????????????}?catch?(Exception?e)?{??????????????e.printStackTrace();??????????}?finally?{??????????????try?{??????????????????if?(null?!=?os)??????????????????????os.close();??????????????}?catch?(Exception?e)?{??????????????????e.printStackTrace();??????????????}??????????}??????}??
4).在立牌上加上立牌編號
[java] view plaincopy
????????????????????????public?final?static?void?pressText(String?pressText,?String?srcImageFile,??????????????String?destImageFile,?int?x,?int?y,?float?alpha)?{??????????try?{??????????????File?img?=?new?File(srcImageFile);??????????????Image?src?=?ImageIO.read(img);??????????????int?width?=?src.getWidth(null);??????????????int?height?=?src.getHeight(null);??????????????BufferedImage?image?=?new?BufferedImage(width,?height,??????????????????????BufferedImage.TYPE_INT_RGB);??????????????Graphics2D?g?=?image.createGraphics();????????????????????????????g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,??????????????????????RenderingHints.VALUE_TEXT_ANTIALIAS_ON);??????????????g.drawImage(src,?0,?0,?width,?height,?null);????????????????????????????g.setColor(new?Color(89,?87,?87));????????????????????????????g.setFont(new?Font("方正蘭亭中黑_GBK",?Font.BOLD,?14));??????????????g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,??????????????????????alpha));????????????????????????????g.drawString(pressText,?x,?y);??????????????g.dispose();??????????????ImageIO.write((BufferedImage)?image,?"PNG",?new?File(destImageFile));??????????}?catch?(Exception?e)?{??????????????e.printStackTrace();??????????}??????}?? 示例: 代碼:
測試代碼
[java] view plaincopy
public?class?codeTest?{??????public?static?void?main(String[]?args)?throws?Exception?{??????????String?text?=?"http://www.xxx.com/";?//?二維碼內(nèi)容????????????????????????????????String?targetPath?=?"f:/qrcode/targetimg/"?+?Utils.toStr();????????????????????Utils.makeDirs(targetPath);????????????????????int?begin?=?100;??????????int?end?=?101;??????????for?(int?i?=?begin;?i?<=?end;?i++)?{????????????????????????????String?code?=?Utils.toStr()?+?Utils.formateNumber(i);????????????????????????????BufferedImage?image?=?Utils.toBufferedImage(text??????????????????????+?"?payCode="?+?code,240,240);????????????????????????????Utils.markImageByCode(image,?"f:/qrcode/srcimg/src.png",??????????????????????targetPath?+?"/"?+?code?+?".png",?340,?160);????????????????????????????Utils.pressText(code,?targetPath?+?"/"?+?code?+?".png",?targetPath??????????????????????+?"/"?+?code?+?".png",?390,?417,?0.5f);??????????}????????????????}??}?? 效果:
批量生成的圖片效果圖如下
批量圖:
utils代碼:
[java] view plaincopy
package?cn.utils.code;????import?java.awt.AlphaComposite;??import?java.awt.Color;??import?java.awt.Font;??import?java.awt.Graphics2D;??import?java.awt.Image;??import?java.awt.RenderingHints;??import?java.awt.image.BufferedImage;??import?java.io.File;??import?java.io.FileOutputStream;??import?java.io.OutputStream;??import?java.text.DecimalFormat;??import?java.text.SimpleDateFormat;??import?java.util.Date;??import?java.util.Hashtable;????import?javax.imageio.ImageIO;????import?com.google.zxing.BarcodeFormat;??import?com.google.zxing.EncodeHintType;??import?com.google.zxing.MultiFormatWriter;??import?com.google.zxing.common.BitMatrix;??????public?abstract?class?Utils?{??????????????public?static?String?DF_DATETIME?=?"yyyyMMdd";??????private?static?float?alpha?=?1f;?????????????????????????????public?static?BufferedImage?toBufferedImage(String?text,?int?width,??????????????int?height)?throws?Exception?{??????????int?BLACK?=?0xFF000000;??????????int?WHITE?=?0xFFFFFFFF;??????????Hashtable<EncodeHintType,?Object>?hints?=?new?Hashtable<EncodeHintType,?Object>();??????????hints.put(EncodeHintType.CHARACTER_SET,?"utf-8");???????????hints.put(EncodeHintType.MARGIN,?1);??????????BitMatrix?matrix?=?new?MultiFormatWriter().encode(text,??????????????????BarcodeFormat.QR_CODE,?width,?height,?hints);??????????BufferedImage?image?=?new?BufferedImage(width,?height,??????????????????BufferedImage.TYPE_INT_RGB);??????????for?(int?x?=?0;?x?<?width;?x++)?{??????????????for?(int?y?=?0;?y?<?height;?y++)?{??????????????????image.setRGB(x,?y,?matrix.get(x,?y)???BLACK?:?WHITE);??????????????}??????????}??????????return?image;??????}??????????????????????????????public?static?void?markImageByCode(Image?img,?String?srcImgPath,??????????????String?targerPath,?int?positionWidth,?int?positionHeight)?{??????????OutputStream?os?=?null;??????????try?{????????????????Image?srcImg?=?ImageIO.read(new?File(srcImgPath));????????????????BufferedImage?buffImg?=?new?BufferedImage(srcImg.getWidth(null),??????????????????????srcImg.getHeight(null),?BufferedImage.TYPE_INT_RGB);??????????????????????????????Graphics2D?g?=?buffImg.createGraphics();??????????????????????????????g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,??????????????????????RenderingHints.VALUE_INTERPOLATION_BILINEAR);????????????????g.drawImage(??????????????????????srcImg.getScaledInstance(srcImg.getWidth(null),??????????????????????????????srcImg.getHeight(null),?Image.SCALE_SMOOTH),?0,?0,??????????????????????null);????????????????g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,??????????????????????alpha));??????????????????????????????g.drawImage(img,?positionWidth,?positionHeight,?null);??????????????g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));????????????????????????????g.dispose();??????????????????????????????os?=?new?FileOutputStream(targerPath);??????????????ImageIO.write(buffImg,?"PNG",?os);????????????????System.out.println("二維碼圖片生成成功");????????????}?catch?(Exception?e)?{??????????????e.printStackTrace();??????????}?finally?{??????????????try?{??????????????????if?(null?!=?os)??????????????????????os.close();??????????????}?catch?(Exception?e)?{??????????????????e.printStackTrace();??????????????}??????????}??????}????????????????????????????????public?final?static?void?pressText(String?pressText,?String?srcImageFile,??????????????String?destImageFile,?int?x,?int?y,?float?alpha)?{??????????try?{??????????????File?img?=?new?File(srcImageFile);??????????????Image?src?=?ImageIO.read(img);??????????????int?width?=?src.getWidth(null);??????????????int?height?=?src.getHeight(null);??????????????BufferedImage?image?=?new?BufferedImage(width,?height,??????????????????????BufferedImage.TYPE_INT_RGB);??????????????Graphics2D?g?=?image.createGraphics();????????????????????????????g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,??????????????????????RenderingHints.VALUE_TEXT_ANTIALIAS_ON);??????????????g.drawImage(src,?0,?0,?width,?height,?null);????????????????????????????g.setColor(new?Color(89,?87,?87));????????????????????????????g.setFont(new?Font("方正蘭亭中黑_GBK",?Font.BOLD,?14));??????????????g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,??????????????????????alpha));????????????????????????????g.drawString(pressText,?x,?y);??????????????g.dispose();??????????????ImageIO.write((BufferedImage)?image,?"PNG",?new?File(destImageFile));??????????}?catch?(Exception?e)?{??????????????e.printStackTrace();??????????}??????}??????????????????????public?static?String?toStr()?{??????????return?toStr(DF_DATETIME);??????}??????????????public?static?String?toStr(String?format)?{??????????return?toStr(format,?new?Date());??????}??????????????public?static?String?toStr(Date?date)?{??????????return?toStr(DF_DATETIME,?date);??????}??????????????public?static?String?toStr(String?format,?Date?date)?{??????????return?new?SimpleDateFormat(format).format(date);??????}????????public?static?String?formateNumber(int?num)?{??????????DecimalFormat?df?=?new?DecimalFormat("000000");??????????String?str2?=?df.format(num);??????????return?str2;??????}????????public?static?boolean?makeDirs(String?filePath)?{????????????File?folder?=?new?File(filePath);??????????return?(folder.exists()?&&?folder.isDirectory())???true?:?folder??????????????????.mkdirs();??????}????}?? 使用的技術(shù): 1.使用的zxing生成二維碼工具。
1)下載地址:
http://repo1.maven.org/maven2/com/google/zxing/javase/3.1.0/
2).maven配置
[java] view plaincopy
<dependency>??????????????<groupId>com.google.zxing</groupId>??????????????<artifactId>core</artifactId>??????????????<version>2.2</version>??????????</dependency>?
總結(jié)
以上是生活随笔為你收集整理的使用zxing生成二维码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。