Recently, the company asked to scan the QR code and generate two-dimensional code functions. And some netizens in the group also mentioned. I wrote a demo here to share with you. The code is very brief, I hope you can enjoy it.
Netizens said in the online search a lot, found not the code is not complete, that is, jar mismatch.
I have shared a zxing version of the 2.2 example, also provided. The implementation code is as follows:
Package Com.herman.test;import Java.io.file;import Java.io.fileinputstream;import java.io.FileNotFoundException; Import Java.io.ioexception;import java.util.hashmap;import Java.util.map;import Javax.imageio.imageio;import Com.google.zxing.barcodeformat;import Com.google.zxing.binarybitmap;import Com.google.zxing.DecodeHintType; Import Com.google.zxing.encodehinttype;import Com.google.zxing.multiformatreader;import Com.google.zxing.multiformatwriter;import Com.google.zxing.notfoundexception;import Com.google.zxing.writerexception;import Com.google.zxing.client.j2se.bufferedimageluminancesource;import Com.google.zxing.client.j2se.matrixtoimagewriter;import Com.google.zxing.common.hybridbinarizer;import com.google.zxing.qrcode.decoder.errorcorrectionlevel;/** * QR Code tool class * @author Herman.xiong * @date 2015-7-10 10:52:15 * @ Version V3.0 * @since JDK 1.6,tomcat 6.0 */public class Zxingutil {private static final Map<encodehinttype, Errorcorrec tionlevel> Encodemap = new Hashmap<encodehiNttype, errorcorrectionlevel> ();p rivate static final Map<decodehinttype, errorcorrectionlevel> decodeMap= New Hashmap<decodehinttype, errorcorrectionlevel> ();p rivate static final String charset= "UTF-8", format= "PNG"; private static final int size=150;private Zxingutil () {}/** * Generate two-dimensional code picture * @author Herman.xiong * @date 2015-7-10 10:26:33 * @param file * @throws writerexception * @throws ioexception */public static void Createqrcode (String data, file file) thr oWS writerexception, IOException {matrixtoimagewriter.writetofile (New Multiformatwriter (). Encode (New String ( Data.getbytes (CharSet), CharSet), Barcodeformat.qr_code, size, size, encodemap), format, file);} /** * Read QR code information * @author Herman.xiong * @date 2015-7-10 Morning 10:25:58 * @param filePath QR code picture path * @return * @throws Filenotfou Ndexception * @throws IOException * @throws notfoundexception */public static string Readqrcode (String filePath) throws Fil Enotfoundexception, IOException, notfoundexception {return new Multiformatreader(). Decode (new Binarybitmap (New Hybridbinarizer) (New Bufferedimageluminancesource (New Imageio.read ( (FilePath)))), Decodemap). GetText ();} public static void Main (String [] arg) {Try{createqrcode ("Http://blog.csdn.net/xmtblog", New File ("D:\\111.png")); System.out.println (Readqrcode ("d:\\111.png"));//Source: http://download.csdn.net/detail/xmt1139057136/8886473// Welcome everyone to follow my blog! If in doubt, please add QQ Group: 454796847 Study together! }catch (Exception e) {e.printstacktrace ();}}}
The results are as follows:
Welcome everyone to follow my blog! If in doubt, please add QQ Group: 454796847 Study together!
Source: http://download.csdn.net/detail/xmt1139057136/8886473
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A few lines of code to solve the Java generation parsing two-dimensional code function