Java實(shí)現(xiàn)的生成二維碼和解析二維碼URL操作示例
本文實(shí)例講述了Java實(shí)現(xiàn)的生成二維碼和解析二維碼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);
/**
* 創(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盤生成二維碼圖片
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
ByteArrayOutputStream os = new ByteArrayOutputStream();//新建流。
ImageIO.write(image, format, os);//利用ImageIO類提供的write方法,將bi以png圖片的數(shù)據(jù)模式寫入流。
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
* 無用
* @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
在線二維碼解碼識別工具
http://tools.jb51.net/transcoding/trans_qrcode
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java編碼操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
springboot使用webservice發(fā)布和調(diào)用接口的實(shí)例詳解
本文介紹了如何在Springboot中使用webservice發(fā)布和調(diào)用接口,涵蓋了必要的依賴添加和代碼示例,文中提供了服務(wù)端和客戶端的實(shí)現(xiàn)方法,以及如何設(shè)置端口和服務(wù)地址,幫助讀者更好地理解和應(yīng)用Springboot結(jié)合webservice的技術(shù)2024-10-10
教你在?Java?中實(shí)現(xiàn)?Dijkstra?最短路算法的方法
這篇文章主要教你在?Java?中實(shí)現(xiàn)?Dijkstra?最短路算法的方法,在實(shí)現(xiàn)最短路算法之前需要先實(shí)現(xiàn)帶權(quán)有向圖,文章中給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起)
這篇文章主要介紹了Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
關(guān)于Spring @Bean 相同加載順序不同結(jié)果不同的問題記錄
本文主要探討了在Spring 5.1.3.RELEASE版本下,當(dāng)有兩個全注解類定義相同類型的Bean時,由于加載順序不同,最終生成的Bean實(shí)例也會不同,文章通過分析ConfigurationClassPostProcessor的執(zhí)行過程,解釋了BeanDefinition的加載和覆蓋機(jī)制,感興趣的朋友一起看看吧2025-02-02

