新聞中心
本文實(shí)例講述了Java實(shí)現(xiàn)的生成二維碼和解析二維碼URL操作。分享給大家供大家參考,具體如下:
十年的平和網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整平和建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“平和網(wǎng)站設(shè)計(jì)”,“平和網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
二維碼依賴jar包,zxing
com.google.zxing javase 3.0.0
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 * 無(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)容感興趣的讀者可查看本站專題:《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ì)有所幫助。
網(wǎng)頁(yè)標(biāo)題:Java實(shí)現(xiàn)的生成二維碼和解析二維碼URL操作示例
網(wǎng)站URL:http://www.ef60e0e.cn/article/piiejc.html