The following Java generated two-dimensional code tool class, you can choose to generate files, or directly in the page generation
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Java.nio.file.FileSystems;
Import Java.util.HashMap;
Import Java.util.Map;
Import Javax.imageio.ImageIO;
Import Javax.servlet.http.HttpServletResponse;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Com.alibaba.fastjson.JSONObject;
Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.Binarizer;
Import Com.google.zxing.BinaryBitmap;
Import Com.google.zxing.DecodeHintType;
Import Com.google.zxing.EncodeHintType;
Import Com.google.zxing.LuminanceSource;
Import Com.google.zxing.MultiFormatReader;
Import Com.google.zxing.MultiFormatWriter;
Import com.google.zxing.NotFoundException;
Import Com.google.zxing.Result;
Import com.google.zxing.WriterException;
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; /**
*
* Two-dimensional code Tool class * @see [Related class/method] * @since [Product/Module Version]/public class Qrcodeutil {private static final Logger log =
Loggerfactory.getlogger (Qrcodeutil.class); /** * Generate two-dimensional code file test * @param filePath file path * @param filename * @param number * @param Phone number * @see [Class, Class # method, Class # member]/public static void Generatencodetest (String filePath, String fileName, Str ing number, string phone) {int width = 200;//image width int height = 200;//Image Height String
format = "PNG";//Image type Jsonobject JSON = new Jsonobject ();
Json.put ("number", number);
Json.put ("Phone", phone); String content = json.tojsonstring ()//content map<encodehinttype, object> hints = new Hashmap<enco
Dehinttype, object> ();
Hints.put (Encodehinttype.character_set, "UTF-8"); try {Bitmatrix Bitmatrix = new Multiformatwriter ().Encode (content, barcodeformat.qr_code, width, height, hints)//generate matrix String Path = Filesystems.getdefault (). GE
Tpath (FilePath, FileName). toString ();
File File = new file (path);
Matrixtoimagewriter.writetofile (Bitmatrix, format, file);//Output Image System.out.println ("Two-dimensional Code output success");
System.out.println ("Picture address:" + FilePath + fileName);
System.out.println ("---------------------------");
catch (Writerexception e) {e.printstacktrace ();
System.out.println ("Two-dimensional code output anomaly");
catch (IOException e) {e.printstacktrace ();
System.out.println ("Two-dimensional code output anomaly"); }/** * * Parsing two-dimensional code content test * @param filePath Two-dimensional code absolute path * @see [Class, Class # method, Class # member]/public
static void Parsedecodetest (String filePath) {bufferedimage image;
try {image = Imageio.read (new File (FilePath));Luminancesource Source = new Bufferedimageluminancesource (image);
Binarizer Binarizer = new Hybridbinarizer (source);
Binarybitmap Binarybitmap = new Binarybitmap (Binarizer);
Map<decodehinttype, object> hints = new Hashmap<decodehinttype, object> ();
Hints.put (Decodehinttype.character_set, "UTF-8"); result = new Multiformatreader (). Decode (Binarybitmap, hints);//decode the image jsonobject content = Jsonobject
. Parseobject (Result.gettext ());
StringBuffer sb = new StringBuffer (); Sb.append ("No.:"). Append (Content.getstring ("number"). Append ("\ r \ n"). Append ("Hand
Machine number: "). Append (content.getstring (" Phone "));
String Returntext = sb.tostring ();
System.out.println (Returntext);
catch (IOException e) {e.printstacktrace (); } CATCH (notfoundexception e) {e.printstacktrace (); /** * * Generates a two-dimensional code output stream * used when displaying directly in a JSP page * No need to save that is to show * @param r Esponse * @param number * @param phone number * @see [Class, Class # method, Class # member]/public static void Generaten
Code (httpservletresponse response, string number, String phone) {Jsonobject json = new Jsonobject ();
Json.put ("number", number);
Json.put ("Phone", phone); String content = json.tojsonstring ();/content int width = 200; Image width int height = 200; Image Height String format = "png";//Image Type Map<encodehinttype, object> hints = new hashmap<en
Codehinttype, object> ();
Hints.put (Encodehinttype.character_set, "UTF-8"); try {Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (content, Barcodeformat.qr_code , width, height, hints);/ Generate Matrix Matrixtoimagewriter.writetostream (Bitmatrix, Format, Response.getoutputstream ());//Output image log
. info ("Two dimensional code output successful");
catch (Writerexception e) {e.printstacktrace ();
Log.error ("Two-dimensional code output anomaly");
catch (IOException e) {e.printstacktrace ();
Log.error ("Two-dimensional code output anomaly"); } public static void Main (string[] args) {generatencodetest ("d:\\", "Zxing.png", "001", "13019931
996 ");
Parsedecodetest ("D:\\zxing.png");
}
}