基于Java實(shí)現(xiàn)二維碼的生成和解析
最近因個(gè)人需求需要對(duì)根據(jù)內(nèi)容生成二維碼和進(jìn)行解析!記錄一下!二維碼其實(shí)就是一種編碼技術(shù),只是這種編碼技術(shù)是用在圖片上了,將給定的一些文字,數(shù)字轉(zhuǎn)換為一張經(jīng)過(guò)特定編碼的圖片。這里利用的是 google 公司的 zxing使用方便,可以操作條形碼或者二維碼等
導(dǎo)入相關(guān)jar包
<!-- 二維碼 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency>
二維碼工具類編
創(chuàng)建二維碼圖片
public static BufferedImage createImage(String charSet, String content, int qrWidth, int qrHeight) {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, charSet);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = null;
try {
bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, // 修改二維碼底部高度
hints);
} catch (WriterException e) {
e.printStackTrace();
}
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
return image;
}二維碼設(shè)置logo
public static void insertLogoImage(BufferedImage source, Image logo, int logoWidth, int logoHeight) {
Graphics2D graph = source.createGraphics();
int qrWidth = source.getWidth();
int qrHeight = source.getHeight();
int x = (qrWidth - logoWidth) / 2;
int y = (qrHeight - logoHeight) / 2;
graph.drawImage(logo, x, y, logoWidth, logoHeight, null);
Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}將文明說(shuō)明增加到二維碼上
public static BufferedImage textToImage(String str, int width, int height, int fontSize) {
BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) textImage.getGraphics();
//開(kāi)啟文字抗鋸齒
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.BLACK);
FontRenderContext context = g2.getFontRenderContext();
Font font = new Font("微軟雅黑", Font.PLAIN, fontSize);
g2.setFont(font);
LineMetrics lineMetrics = font.getLineMetrics(str, context);
FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font);
float offset = (width - fontMetrics.stringWidth(str)) / 2;
float y = (height + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2;
g2.drawString(str, (int) offset, (int) y);
return textImage;
}解析二維碼
/*
* 解析二維碼
*/
public static String decode(File file, DecodeHintType cherSet) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, cherSet);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}main方法測(cè)試類
public static void main(String[] args) {
String content="用戶攝影作品版權(quán)信息";
BufferedImage image = QRCodeUtil.createImage( "utf-8", content, 400, 400 );
QRCodeUtil.addUpFont( image,"用戶攝影作品版權(quán)信息" );
String formatName="png";
String imagePath="D:\temp\java二維碼.png";
File file=new File(imagePath);
try {
ImageIO.write(image, formatName, file);
} catch (IOException e) {
e.printStackTrace();
}
String decode = null;
try {
decode = QRCodeUtil.decode(file, DecodeHintType.CHARACTER_SET);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(decode);
System.out.println("執(zhí)行完成");
}這樣就實(shí)現(xiàn)了 !二維碼里面的內(nèi)容根據(jù)實(shí)際數(shù)據(jù)來(lái)就行!用手機(jī)掃描后查看。
到此這篇關(guān)于基于Java實(shí)現(xiàn)二維碼的生成和解析的文章就介紹到這了,更多相關(guān)Java二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MybatisPlus結(jié)合groupby實(shí)現(xiàn)分組和sum求和的步驟
這篇文章主要介紹了MybatisPlus結(jié)合groupby實(shí)現(xiàn)分組和sum求和的步驟,這次使用的是LambdaQueryWrapper,使用QueryWrapper相對(duì)來(lái)說(shuō)簡(jiǎn)單點(diǎn)就不寫(xiě)了,本文分步驟給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2023-12-12
javaweb Servlet開(kāi)發(fā)總結(jié)(一)
Servlet是sun公司提供的一門(mén)用于開(kāi)發(fā)動(dòng)態(tài)web資源的技術(shù)。這篇文章主要介紹了javaweb Servlet開(kāi)發(fā)的第一篇,感興趣的小伙伴們可以參考一下2016-05-05
SpringBoot中服務(wù)消費(fèi)的實(shí)現(xiàn)
本文主要介紹了SpringBoot中服務(wù)消費(fèi)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
SpringBoot中@Scheduled實(shí)現(xiàn)服務(wù)啟動(dòng)時(shí)執(zhí)行一次
本文主要介紹了SpringBoot中@Scheduled實(shí)現(xiàn)服務(wù)啟動(dòng)時(shí)執(zhí)行一次,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
詳解Java中使用泛型實(shí)現(xiàn)快速排序算法的方法
這篇文章主要介紹了Java中使用泛型實(shí)現(xiàn)快速排序算法的方法,快速排序的平均時(shí)間復(fù)雜度為(n\log n),文中的方法立足于基礎(chǔ)而并沒(méi)有考慮優(yōu)化處理,需要的朋友可以參考下2016-05-05
java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法
這篇文章主要介紹了java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法的相關(guān)資料,在開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)用到String 這個(gè)類進(jìn)行操作,需要的朋友可以參考下2017-08-08
SpringBoot3 Spring WebFlux簡(jiǎn)介(推薦)
SpringWebFlux是Spring Framework 5中引入的響應(yīng)式Web框架,用于支持非阻塞異步通信和響應(yīng)式流處理,與傳統(tǒng)的SpringMVC相比,WebFlux提供了完全異步非阻塞的編程模型,適用高并發(fā)、微服務(wù)架構(gòu)和實(shí)時(shí)數(shù)據(jù)流,本文介紹SpringBoot3 Spring WebFlux簡(jiǎn)介,感興趣的朋友一起看看吧2024-10-10

