日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二维码服务类

發布時間:2024/3/12 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二维码服务类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔

文章目錄

  • 前言
  • 一、使用步驟
    • 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();}}}

總結

以上是生活随笔為你收集整理的二维码服务类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。