Generation of two-dimensional code

Source: Internet
Author: User

(a) Two-dimensional code generation step

1. Generate a two-dimensional code matrix

2. Coloring the two-dimensional code matrix

3. Generation of QR code files

(b) Code

1. Use Goole's Core.jar, Core-3.2.0.jar here, download link Http://pan.baidu.com/s/1kTu0EyF

Package Resume.util;import com.google.zxing.*; import Com.google.zxing.client.j2se.bufferedimageluminancesource;import Com.google.zxing.common.BitMatrix; Import Com.google.zxing.common.hybridbinarizer;import Javax.imageio.imageio;import java.awt.image.BufferedImage; Import Java.io.file;import java.io.ioexception;import java.util.Hashtable;/** * Created by vinphy on 2015/6/30.*/ Public classQrcodeutil {Private StaticFinal String CHARSET ="Utf-8"; Private StaticFinalintBLACK =0xff000000; Private StaticFinalintWhite =0xFFFFFFFF; /** 1. Generate a two-dimensional code matrix * @param text to generate a specified string of two-dimensional code; * Width width; height height*/         Public StaticBitmatrix Toqrcodematrix (String text,integer width,integer height) {//1) Determine the width and height of the matrix, 300*300            if(Width = =NULL|| Width < -) {width= -; }            if(Height = =NULL|| Height < -) {Height= -; }            //2) Set the QR code picture formathashtable<encodehinttype,string> hints =NewHashtable<encodehinttype,string>(); //3) Set the encoding used for the contentHints.put (Encodehinttype.character_set,charset); //4) Generate matrixBitmatrix matrix =NULL; Try{Matrix=NewMultiformatwriter (). Encode (text, barcodeformat.qr_code,width,height,hints); }Catch(writerexception e) {e.printstacktrace (); }            returnMatrix; }        /** 2. Generate black and white images based on dot matrix * @param matrix Two-dimensional code matrices*/         Public Staticbufferedimage tobufferedimage (Bitmatrix matrix) {//1. Get the matrix length and width            intwidth =matrix.getwidth (); intHeight =matrix.getheight (); //2. Get Bufferediamge instancesBufferedImage image =NewBufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB); //3. Coloring             for(intx =0; x <width; X + +){                 for(inty =0; Y < height; y++) {Image.setrgb (X,y,matrix.Get(x, y)?black:white); }            }            returnimage; }        /** 3. Generate files according to the matrix picture format * @param matrix of two-dimensional code; * Format file formats, such as GIF; * File Files*/         Public Static voidwritetofile (Bitmatrix matrix,string format,file File) {//1. Get black and white imagesBufferedImage image =tobufferedimage (matrix); //2. Generating Files            Try {                if(!Imageio.write (Image,format,file)) {                    Throw NewIOException (("Could not write an image of format") +format+" to"+file); }            }Catch(IOException e) {e.printstacktrace (); }        }
  }

2. Testing

 Public Static voidMain (string[] args) {//1. Text to convertString Text ="Ningwenhui"; //2. Generating FilesFile File =NewFile ("D:"+file.separator+"Vinphy.gif"); //3. Create a QR code in the fileWriteToFile (Toqrcodematrix (text,NULL,NULL),"gif", file); }

Google's Zxing-core.jar package encapsulates a way to generate black-and-white images and generate files, using the following:

/ ** * QR code generation * generates the corresponding QR code based on the six-bit random number generated by GetInfo () **/     Publicvoid Createcode () {String text=""+ This. GetInfo (); intwidth = the; intHeight = the; /** * Set the parameters of the QR code*/String name="Erweima"; String Path="d://create"; HashMap hints=NewHashMap (); //the encoding used for the contentHints.put (Encodehinttype.character_set,"Utf-8"); Try{Bitmatrix Bitmatrix=NewMultiformatwriter (). Encode (text,barcodeformat.qr_code,width,height,hints); //generate two-dimensional codeFile file1 =NewFile (path,name+". jpg"); Matrixtoimagewriter.writetofile (Bitmatrix,"jpg", file1); } Catch(Exception e) {e.printstacktrace (); }    }

Generation of two-dimensional code

Related Article

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.