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

十年的平和網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整平和建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“平和網(wǎng)站設(shè)計”,“平和網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
二維碼依賴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é)運算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
名稱欄目:Java實現(xiàn)的生成二維碼和解析二維碼URL操作示例
本文來源:http://www.chinadenli.net/article22/piiejc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、搜索引擎優(yōu)化、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)