Java中基于maven实现zxing二维码功能
程序员文章站
2024-03-08 11:47:28
maven所需jar
com.google.zxing ...
maven所需jar
<dependency> <groupid>com.google.zxing</groupid> <artifactid>core</artifactid> <version>3.0.0</version> </dependency> <dependency> <groupid>com.google.zxing</groupid> <artifactid>javase</artifactid> <version>3.0.0</version> </dependency>
生成二维码:
public static void main(string[] args) throws ioexception { string text="www.baidu.com"; int width=100; int height=100; 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(text, barcodeformat.qr_code, width, height,hints); path file=new java.io.file("d:/new.png").topath(); matrixtoimagewriter.writetopath(bitmatrix, format, file); } catch (writerexception e) { // todo auto-generated catch block e.printstacktrace(); } 解析二维码: public static void main(string[] args) throws notfoundexception { multiformatreader formatreader=new multiformatreader(); file file =new file("d:/new.png"); bufferedimage image=null; try { image = imageio.read(file); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } 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); system.err.println("解析结果:"+result.tostring()); system.out.println(result.getbarcodeformat()); system.out.println(result.gettext()); }
以上所述是小编给大家介绍的java中基于maven实现zxing二维码功能,希望对大家有所帮助