java二維碼生成的方法
本文實(shí)例為大家分享了java二維碼的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
這次用到的jar包是zxing,沒(méi)有用到core的jar包
先導(dǎo)入zxing.jar包
生成二維碼
package cn.huse.erweima; import java.io.File; import java.util.HashMap; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * 生成二維碼 * */ public class CreateQRCode { public static void main(String[] args) { int width = 300; int height = 300; String format = "gif"; String content = "www.baidu.com"; //定義二維碼的參數(shù) HashMap hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); try { BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints); File file = new File("e:"+File.separator+"new.gif"); MatrixToImageWriter.writeToFile(matrix, format, file); } catch (Exception e) { e.printStackTrace(); } } }
解析二維碼
package cn.huse.erweima; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import javax.imageio.ImageIO; import com.google.zxing.BinaryBitmap; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatReader; import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.HybridBinarizer; //解析二維碼 public class ReadQRCode { public static void main(String[] args) { MultiFormatReader formatReader = new MultiFormatReader(); File file = new File("e:"+File.separator+"new.gif"); try { BufferedImage image = ImageIO.read(file); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); HashMap hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); Result result = formatReader.decode(binaryBitmap,hints); System.out.println(result.toString()); System.out.println(result.getBarcodeFormat()); System.out.println(result.getText()); } catch (NotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中的6種API請(qǐng)求參數(shù)讀取方式總結(jié)
使用Spring Boot開(kāi)發(fā)API的時(shí)候,讀取請(qǐng)求參數(shù)是服務(wù)端編碼中最基本的一項(xiàng)操作,Spring Boot中也提供了多種機(jī)制來(lái)滿足不同的API設(shè)計(jì)要求,通過(guò)本文,為大家總結(jié)6種常用的請(qǐng)求參數(shù)讀取方式,需要的朋友可以參考下2024-07-07解決springcloud啟動(dòng)時(shí)報(bào)錯(cuò)Connection refused:connect問(wèn)題
這篇文章主要介紹了解決springcloud啟動(dòng)時(shí)報(bào)錯(cuò)Connection refused:connect問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Java將Word文檔轉(zhuǎn)換為PDF文件的幾種常用方法總結(jié)
這篇文章主要介紹了Java將Word文檔轉(zhuǎn)換為PDF文件的四種常用方法,分別使用ApachePOI+iText、Aspose.Words?for?Java、Docx4j和JODConverter,這些庫(kù)各有優(yōu)點(diǎn),但在使用時(shí)需要注意庫(kù)與Java環(huán)境的兼容性、安裝所需依賴、轉(zhuǎn)換速度和資源消耗,需要的朋友可以參考下2024-10-10Springboot實(shí)現(xiàn)視頻上傳及壓縮功能
這篇文章主要介紹了Springboot實(shí)現(xiàn)視頻上傳及壓縮功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03java實(shí)現(xiàn)socket從服務(wù)器連續(xù)獲取消息的示例
這篇文章主要介紹了java實(shí)現(xiàn)socket從服務(wù)器連續(xù)獲取消息的示例,需要的朋友可以參考下2014-04-04