Java實(shí)現(xiàn)的生成二維碼和解析二維碼URL操作示例
本文實(shí)例講述了Java實(shí)現(xiàn)的生成二維碼和解析二維碼URL操作。分享給大家供大家參考,具體如下:
二維碼依賴(lài)jar包,zxing
<!-- 二維碼依賴(lài) 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);
/**
* 創(chuàng)建二維碼
* @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盤(pán)生成二維碼圖片
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
ByteArrayOutputStream os = new ByteArrayOutputStream();//新建流。
ImageIO.write(image, format, os);//利用ImageIO類(lèi)提供的write方法,將bi以png圖片的數(shù)據(jù)模式寫(xiě)入流。
byte b[] = os.toByteArray();//從流中獲取數(shù)據(jù)數(shù)組。
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
* 無(wú)用
* @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("解析結(jié)果:"+result.toString());
logger.info("解析格式:"+result.getBarcodeFormat());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DeleteFileUtil.delete(fileDirectory);
}
}
}
PS:這里再為大家推薦兩款二維碼相關(guān)在線工具供大家參考使用:
在線生成二維碼工具(加強(qiáng)版)
http://tools.jb51.net/transcoding/jb51qrcode
在線二維碼解碼識(shí)別工具
http://tools.jb51.net/transcoding/trans_qrcode
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java編碼操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
SpringBoot 策略模式實(shí)現(xiàn)切換上傳文件模式
策略模式是指有一定行動(dòng)內(nèi)容的相對(duì)穩(wěn)定的策略名稱(chēng),這篇文章主要介紹了SpringBoot 策略模式 切換上傳文件模式,需要的朋友可以參考下2023-11-11
解決@Transactional注解事務(wù)不回滾不起作用的問(wèn)題
這篇文章主要介紹了解決@Transactional注解事務(wù)不回滾不起作用的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
全面解析Java支持的數(shù)據(jù)類(lèi)型及Java的常量和變量類(lèi)型
這篇文章主要介紹了Java支持的數(shù)據(jù)類(lèi)型及Java的常量和變量類(lèi)型,是Java入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-02-02
詳解IDEA啟動(dòng)多個(gè)微服務(wù)的配置方法
這篇文章主要介紹了詳解IDEA啟動(dòng)多個(gè)微服務(wù)的配置方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

