Java實現(xiàn)的生成二維碼和解析二維碼URL操作示例
本文實例講述了Java實現(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)在線工具供大家參考使用:
在線生成二維碼工具(加強版)
http://tools.jb51.net/transcoding/jb51qrcode
在線二維碼解碼識別工具
http://tools.jb51.net/transcoding/trans_qrcode
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java編碼操作技巧總結(jié)》、《Java數(shù)學運算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
SpringBoot 策略模式實現(xiàn)切換上傳文件模式
策略模式是指有一定行動內(nèi)容的相對穩(wěn)定的策略名稱,這篇文章主要介紹了SpringBoot 策略模式 切換上傳文件模式,需要的朋友可以參考下2023-11-11解決@Transactional注解事務(wù)不回滾不起作用的問題
這篇文章主要介紹了解決@Transactional注解事務(wù)不回滾不起作用的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02全面解析Java支持的數(shù)據(jù)類型及Java的常量和變量類型
這篇文章主要介紹了Java支持的數(shù)據(jù)類型及Java的常量和變量類型,是Java入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2016-02-02