国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java實現的生成二維碼和解析二維碼URL操作示例

2024-07-14 08:41:37
字體:
來源:轉載
供稿:網友

本文實例講述了Java實現的生成二維碼和解析二維碼URL操作。分享給大家供大家參考,具體如下:

二維碼依賴jar包,zxing

<!-- 二維碼依賴 start --><dependency>  <groupId>com.google.zxing</groupId>  <artifactId>javase</artifactId>  <version>3.0.0</version></dependency>

createQRCode生成二維碼,anlysisQRCode解析二維碼

package com.xgt.util;import com.google.zxing.*;import com.google.zxing.client.j2se.BufferedImageLuminanceSource;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.common.HybridBinarizer;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import org.apache.commons.io.IOUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import sun.misc.BASE64Encoder;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.*;import java.nio.file.Path;import java.util.Hashtable;public class QRCodeUtil {  private static final Logger logger = LoggerFactory.getLogger(QRCodeUtil.class);  /**   * 創建二維碼   * @param url   * @param fileName   * @return   * @throws IOException   */  public static String createQRCode(String url,String fileDirectory,String fileName) throws IOException {    int width = 500;    int height = 500;    String format = "png";    Hashtable hints = new Hashtable();    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);    hints.put(EncodeHintType.MARGIN, 2);    try {      BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);      File fileDir = new File(fileDirectory);      FileToolUtil.judeDirExists(fileDir);      Path file = new File(fileDirectory,fileName+".png").toPath();//在D盤生成二維碼圖片      MatrixToImageWriter.writeToPath(bitMatrix, format, file);      BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);      ByteArrayOutputStream os = new ByteArrayOutputStream();//新建流。      ImageIO.write(image, format, os);//利用ImageIO類提供的write方法,將bi以png圖片的數據模式寫入流。      byte b[] = os.toByteArray();//從流中獲取數據數組。      String str = new BASE64Encoder().encode(b);      IOUtils.closeQuietly(os);      return str;    } catch (Exception e) {      // TODO Auto-generated catch block      e.printStackTrace();    }finally {      //DeleteFileUtil.delete(fileDirectory);    }    return "NULL";  }  /**   * 解析出二維碼的url   * 無用   * @param file   * @param fileDirectory   * @throws NotFoundException   */  public static void anlaysisQRCode(File file,String fileDirectory) throws NotFoundException {    MultiFormatReader formatReader=new MultiFormatReader();    BufferedImage image=null;    try {      image = ImageIO.read(file);    BinaryBitmap binaryBitmap =new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));    Hashtable hints=new Hashtable();    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");    Result result=formatReader.decode(binaryBitmap,hints);    logger.info("解析結果:"+result.toString());    logger.info("解析格式:"+result.getBarcodeFormat());    } catch (IOException e) {      // TODO Auto-generated catch block      e.printStackTrace();    }finally {      DeleteFileUtil.delete(fileDirectory);    }  }}

希望本文所述對大家java程序設計有所幫助。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎赉特旗| 牙克石市| 五指山市| 丹凤县| 资中县| 陆河县| 微山县| 微博| 湘潭县| 连山| 漳州市| 淮安市| 伊宁市| 闽清县| 密山市| 盐池县| 沙湾县| 武宁县| 平罗县| 封丘县| 宁夏| 乌鲁木齐县| 鄱阳县| 运城市| 礼泉县| 边坝县| 沁水县| 清水河县| 岗巴县| 邵阳市| 明水县| 逊克县| 当涂县| 滕州市| 乌鲁木齐县| 绵竹市| 安龙县| 甘泉县| 长武县| 常德市| 成都市|