Javaweb Two-dimensional code

Source: Internet
Author: User
Tags string format

Description Related Packages
zxing-core3.3zxing-javase3.3junit4.12
Pom file
<dependency>    <groupId>Com.google.zxing</groupId>    <artifactId>Core</artifactId>    <version>3.3.0</version></dependency><dependency>    <groupId>Com.google.zxing</groupId>    <artifactId>Javase</artifactId>    <version>3.3.0</version></dependency><dependency>    <groupId>Junit</groupId>    <artifactId>Junit</artifactId>    <version>4.12</version></dependency>
Related code
Package com;import java.awt.image.BufferedImage;import Java.io.File;import java.io.IOException;import Java.nio.file.FileSystems;import Java.nio.file.Path;import Java.util.HashMap;import Java.util.Map;import Javax.imageio.ImageIO;import org.junit.Test;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.MatrixToImageConfig;import Com.google.zxing.client.j2se.MatrixToImageWriter;import Com.google.zxing.common.BitMatrix;import Com.google.zxing.common.HybridBinarizer;import Com.google.zxing.oned.Code128Writer; Public classDemo {Static classimageformat {Static FinalString PNG ="PNG";Static FinalString GIF ="GIF";Static FinalString BMP ="BMP";Static FinalString JPEG ="JPEG"; }@Test     Public void qrcodebuildtest() {//Output directoryString Outpath ="D:\\Demo\\";//Output file nameString outname ="Zxingdemo.png";//Generate contentString contents ="QRCode Demo"; Demo.Qrcodebuild(contents, Outpath, Outname, ImageFormat.PNG, $, $); }@Test     Public void barcodebuildtest() {//Output directoryString Outpath ="D:\\Demo\\";//Output file nameString outname ="Zxingdemo.png";//Generate contentString contents ="BarCode Demo"; Demo.Barcodebuild(contents, Outpath, Outname, ImageFormat.PNG, the, -); }@Test     Public void qrcodeparsetest() {//String content = Demo.qrcodeparse ("D:\\demo\\zxingdemo.png");String content = Demo.Qrcodeparse("Http://dl2.iteye.com/upload/attachment/0103/0259/f1c4cf6c-c825-3504-8c9e-97019e3796d1.png"); System. out.println(NULL= = content?"Parse failed": content); }/*** Two-dimensional code generation     *      * @param contents* Text content     * @param outpath* Output Directory     * @param outname* Output File     * @param format* Output File type (Png,jpeg ...)     * @param width* Create image width     * @param height* Create picture height     */     Public Static void Qrcodebuild(String contents, String Outpath, String outname, string format,intWidthintHeight) {Try{//Output Directory detection            if(!NewFile (Outpath).exists()) {NewFile (Outpath).mkdirs(); } Path Path = filesystems.Getdefault().GetPath(Outpath, outname);//related configuration (coded, white edge)Map<encodehinttype, object> hints =NewHashmap<encodehinttype, object> (); Hints.put(Encodehinttype.Character_set,"UTF-8"); Hints.put(Encodehinttype.MARGIN,1);//GenerateBitmatrix matrix =New Multiformatwriter().encode(contents, Barcodeformat.Qr_code, width, height, hints);//Output two-dimensional codeMatrixtoimageconfig config =New Matrixtoimageconfig(0xff000000,0xFFFFFFFF); Matrixtoimagewriter.Writetopath(Matrix, format, path, config);//output as file Stream (Web project)            //OutputStream stream = Response.getoutputstream ();            //Matrixtoimagewriter.writetostream (Matrix, Format, stream,config);System. out.println("Debug output: OK"); }Catch(Writerexception | IOException e) {e.Printstacktrace(); }    }/*** Two-dimensional code analysis     *      * @param inpath* QR code image address     * @returnTwo-dimensional code text content     */     Public StaticStringQrcodeparse(String Inpath) {//File detection        if(!NewFile (Inpath).exists()) {System. out.println("file does not exist:"+ Inpath);return NULL; The String content ="";Try{//Read picturesBufferedImage image = ImageIO.Read(NewFile (Inpath));//Extract picture pixelsLuminancesource Source =New Bufferedimageluminancesource(image);//Two value (set grayscale to 0 or 255)Binarizer Binarizer =New Hybridbinarizer(source);//Convert to binary pictureBinarybitmap Binarybitmap =New Binarybitmap(Binarizer);//Related configurationMap<decodehinttype, object> hints =NewHashmap<decodehinttype, object> (); Hints.put(Decodehinttype.Character_set,"UTF-8");//parsingResult result =New Multiformatreader().Decode(Binarybitmap, hints);//decode the image            //Get textContent = result.GetText();//Output barcode type (I use the QR code, so will output Qr_code)System. out.println(result.)Getbarcodeformat()); }Catch(IOException | Notfoundexception e) {e.Printstacktrace(); }returnContent }/**** One-dimensional code generation     *      * @param content* Text     * @param outpath* Output Directory     * @param outname* Output File name     * @param format* file Format     * @param width* Create image width     * @param height* Create picture height     */     Public Static void Barcodebuild(string content, String Outpath, String outname, string format,intWidthintHeight) {//File detection        if(!NewFile (Outpath).exists()) {NewFile (Outpath).mkdirs(); } Path Path = filesystems.Getdefault().GetPath(Outpath, outname);Try{Code128writer writer =New Code128writer(); Bitmatrix matrix = writer.encode(Content, Barcodeformat.code_128, width, height); Matrixtoimagewriter.Writetopath(Matrix, format, path); System. out.println("Debug output: OK"); }Catch(Writerexception | IOException e) {e.Printstacktrace(); }    }}

You can refer to here and here

Javaweb Two-dimensional code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.