二维码服务类
提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- 前言
- 一、使用步驟
- 1.引入依賴
- 1.引入庫
- 二.工具類
前言
本文通過引入zxing的依賴包實現二維碼的下載
提示:以下是本篇文章正文內容,下面案例可供參考
一、使用步驟
1.引入依賴
代碼如下:
<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.3</version></dependency>1.引入庫
import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.CharacterSetECI; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.Hashtable;二.工具類
代碼如下:
/*** 二維碼服務類* Created by louie on 2017-11-9.*/ public class QRCodeService {/*** 生成二維碼* @param content 二維碼內容* @param width 寬* @param height 高* @param imgPath 二維碼圖片存放路徑*/public void createQRCode(String content,int width,int height,String imgPath) throws WriterException, IOException {BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height);BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);for (int i = 0; i < width; i++) {for (int j = 0; j < height; j++) {bufferedImage.setRGB(i,j,bitMatrix.get(i,j)? Color.BLACK.getRGB():Color.WHITE.getRGB());}}File imageFile = new File(imgPath+"myQRCode.png");if (!imageFile.getParentFile().exists()){imageFile.getParentFile().mkdirs();}ImageIO.write(bufferedImage,"png",imageFile);}/*** 生成二維碼* @param content 二維碼內容* @param width 寬* @param height 高* @param margin 二維碼外邊距,0到4* @param imgPath 二維碼圖片存放路徑*/public void createQRCode(String content,int width,int height,int margin,String imgPath) throws WriterException, IOException {Hashtable<EncodeHintType,Object> hintTypes = new Hashtable<EncodeHintType, Object>();hintTypes.put(EncodeHintType.CHARACTER_SET,CharacterSetECI.UTF8);hintTypes.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hintTypes.put(EncodeHintType.MARGIN,margin);BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hintTypes);BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);for (int i = 0; i < width; i++) {for (int j = 0; j < height; j++) {bufferedImage.setRGB(i,j,bitMatrix.get(i,j)? Color.BLACK.getRGB():Color.WHITE.getRGB());}}File imageFile = new File(imgPath+"myQRCode.png");if (!imageFile.getParentFile().exists()){imageFile.getParentFile().mkdirs();}ImageIO.write(bufferedImage,"png",imageFile);}/*** 獲取二維碼圖片字節數組* @param content 二維碼內容* @param width 寬* @param height 高* @param margin 外邊距* @return 二維碼對應的字節數組* @throws Exception*/public byte[] getQRCodeBytes(String content,int width,int height,int margin) throws Exception{Hashtable<EncodeHintType,Object> hintTypes = new Hashtable<EncodeHintType, Object>();ByteArrayOutputStream outputStream = null;hintTypes.put(EncodeHintType.CHARACTER_SET,CharacterSetECI.UTF8);//設置編碼hintTypes.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//設置容錯級別hintTypes.put(EncodeHintType.MARGIN,margin);//設置外邊距try {BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hintTypes);//獲取二維碼//將二維碼寫入圖片BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);for (int i = 0; i < width; i++) {for (int j = 0; j < height; j++) {bufferedImage.setRGB(i,j,bitMatrix.get(i,j)? Color.black.getRGB():Color.WHITE.getRGB());}}//將圖片轉為字節數組outputStream = new ByteArrayOutputStream();ImageIO.write(bufferedImage,"png",outputStream);return outputStream.toByteArray();} catch (Exception e) {e.printStackTrace();throw e;} finally {if (outputStream != null){outputStream.close();}}}總結
- 上一篇: 视频压缩技术及安卓中用法
- 下一篇: Lucene 搜索词分词