Zxing is provided by Google on the bar code (one-dimensional code, two-dimensional code) parsing tool, provides the method of generation and analysis of two-dimensional code, and now I briefly introduce the use of Java using zxing generation and parsing of the two-dimensional code
1, two-dimensional code generation
1.1 Add the Zxing-core.jar package to the classpath.
1.2 The generation of two-dimensional code needs to use the Matrixtoimagewriter class, which is provided by Google, you can copy the class into the source code, here I paste the source of the class, you can directly use.
ImportCom.google.zxing.common.BitMatrix;ImportJavax.imageio.ImageIO;ImportJava.io.File;ImportJava.io.OutputStream;ImportJava.io.IOException;ImportJava.awt.image.BufferedImage;PublicFinalClassMatrixtoimagewriter {PrivateStaticFinalint BLACK = 0xff000000;PrivateStaticFinalint white = 0xFFFFFFFF;PrivateMatrixtoimagewriter () {}PublicStaticBufferedImage tobufferedimage (Bitmatrix matrix) {int width =Matrix.getwidth ();int height =Matrix.getheight (); BufferedImage image =NewBufferedImage (width, height, bufferedimage.type_int_rgb);for (int x = 0; x < width; X + +) {for (int y = 0; Y < height; y++) {Image.setrgb (x, Y, Matrix.get (x, y)?Black:white); } }ReturnImage }PublicStaticvoidWriteToFile (Bitmatrix Matrix, String format, file file)ThrowsIOException {BufferedImage image =Tobufferedimage (matrix);if (!imageio.write (image, format, file)) {throw new IOException ("Could not write an image of format "+ Format +" to "+ file);}} public static void< Span style= "color: #000000;" > WriteToStream (Bitmatrix matrix, String format, OutputStream stream) throws IOException {BufferedImage image = Tobufferedimage (matrix); if (! Imageio.write (image, format, stream)) {throw new IOException ("Could not write an image of format" +
1.3 Writing the implementation code to generate the QR code
Try{String content = "120605181003;HTTP://WWW.CNBLOGS.COM/JTMJX"; String Path = "C:/users/administrator/desktop/testimage"; Multiformatwriter multiformatwriter = new multiformatwriter (); Map hints = new HashMap (); Hints.put (Encodehinttype.character_set, "UTF-8"); Bitmatrix Bitmatrix = multiformatwriter.encode (content, Barcodeformat.qr_code, n, I,hints); File File1 = new File (path, "napkin. jpg"); Matrixtoimagewriter.writetofile (Bitmatrix, "JPG", file1);} Catch (Exception e) {e.printstacktrace ();}
Now you can generate a two-dimensional code image after running, is it very simple ah? Next we'll look at how to parse the QR code
2, two-dimensional code analysis
2.1 Add the Zxing-core.jar package to the classpath.
2.2 And the generation, we need an auxiliary class (Bufferedimageluminancesource), the same kind of Google also provides, here I also put the source of the class, I can directly copy the use of a, save the trouble of finding
BufferedimageluminancesourceImportCom.google.zxing.LuminanceSource;ImportJava.awt.Graphics2D;ImportJava.awt.geom.AffineTransform;ImportJava.awt.image.BufferedImage;PublicFinalClass BufferedimageluminancesourceExtendsLuminancesource {PrivateFinalBufferedImage image;PrivateFinalIntLeftPrivateFinalIntTopPublicBufferedimageluminancesource (bufferedimage image) {This (image, 0, 0, Image.getwidth (), Image.getheight ()); }Public Bufferedimageluminancesource (bufferedimage image,int left,int top,int width,IntHeight) {Super(width, height);int sourcewidth =Image.getwidth ();int sourceheight =Image.getheight ();if (left + width > Sourcewidth | | top + Height >Sourceheight) {ThrowNew IllegalArgumentException ("Crop rectangle does not fit within image data."); }for (int y = top; Y < top + height; y++) {for (int x = left; X < left + width; X + +) {if ((Image.getrgb (x, y) & 0xff000000) = = 0) {Image.setrgb (x, y, 0xFFFFFFFF);//= White} } }This.image =NewBufferedImage (Sourcewidth, Sourceheight, Bufferedimage.type_byte_gray);This.image.getGraphics (). DrawImage (image, 0, 0,Null);This.left =LeftThis.top =Top } @OverridePublicByte[] GetRow (int y,Byte[] row) {if (Y < 0 | | y >=GetHeight ()) {ThrowNew IllegalArgumentException ("Requested row is outside the image:" +y); }int width =GetWidth ();if (row = =null | | Row.length <width) {row =NewByte[Width]; } image.getraster (). getDataElements (left, top + y, width, 1, row);ReturnRow } @OverridePublicByte[] Getmatrix () {int width =GetWidth ();int height =GetHeight ();int area = width *Heightbyte[] Matrix =NewByte[Area]; Image.getraster (). getDataElements (left, top, width, height, matrix);ReturnMatrix } @OverridePublicBooleanIscropsupported () {ReturnTrue; } @OverridePublic Luminancesource Crop (int left,int top,int width,IntHeight) {ReturnNew Bufferedimageluminancesource (Image,This.left + Left,This.top +Top, width, height); } @OverridePublicBooleanIsrotatesupported () {ReturnTrue; } @OverridePublicLuminancesource Rotatecounterclockwise () {int sourcewidth =Image.getwidth ();int sourceheight = Image.getheight (); AffineTransform transform = new affinetransform (0.0, -1.0, 1.0, 0.0, 0.0, Sourcewidth); BufferedImage rotatedimage = new BufferedImage ( Sourceheight, Sourcewidth, Bufferedimage.type_byte_gray); graphics2d g = Rotatedimage.creategraphics (); G.drawimage (image, transform, null); G.dispose (); int width = getwidth (); return new Bufferedimageluminancesource (rotatedimage , Top, Sourcewidth-(left + width), getheight (), width); } }
2.3 Write the implementation code to parse the QR code
Try{Multiformatreader Formatreader =NewMultiformatreader (); String FilePath = "C:/users/administrator/desktop/testimage/test.jpg"; File File =NewFile (FilePath); BufferedImage image =Imageio.read (file);; Luminancesource Source =NewBufferedimageluminancesource (image); Binarizer Binarizer =New Hybridbinarizer (source); Binarybitmap Binarybitmap = new binarybitmap (Binarizer); Map hints = new HashMap (); Hints.put (Encodehinttype.character_set, "UTF-8"); Result result = Formatreader.decode (binarybitmap,hints); SYSTEM.OUT.PRINTLN ("result =" + result.tostring ()); System.out.println ("Resultformat =" + Result.getbarcodeformat ()); System.out.println ("Resulttext =" + Result.gettext ());} Catch (Exception e) {e.printstacktrace ();}
Now you can see that the console prints the contents of the QR code after running.
To this end, the use of zxing generation and resolution of the two-dimensional code to tell the completion of the presentation, mainly for their own memo, at the same time to facilitate the needs of people. Oh!
Java generates two-dimensional code using zxing