Zxing-core generation of two-dimensional code and parsing

Source: Internet
Author: User
Tags string format

Now when the two-dimensional code is so popular, you must also know how the two-dimensional code is generated. Now let's take a look at how it was built.

In fact, the main use of the goggle released by the jar: This article to turn to open the link 1, two-dimensional code generation

Add the Zxing-core.jar package to the classpath.
The generation of two-dimensional code needs the help of Matrixtoimagewriter class, this class is provided by Google, can copy the class to the source code, here I will the source code of this class, can use directly.

Code that can generate two-dimensional code directly

public void Test1 () throws exception{
		String content = "www.baidu.com";
	     String Path = "d://";
	     
	     Multiformatwriter multiformatwriter = new Multiformatwriter ()//zxing is provided by Google about barcode
	     
	     Map hints = new HashMap ();
	     Hints.put (Encodehinttype.character_set, "UTF-8");
	     Bitmatrix Bitmatrix = multiformatwriter.encode (content, Barcodeformat.qr_code, 400,hints);//This is the size of the photo
	     File File1 = new File (path, "my.jpg");
	     Matrixtoimagewriter.writetofile (Bitmatrix, "JPG", file1);
	     System.out.println ("execution completed");
	}
When we can find out that this code was copied on a MatrixtoimagewriterError, so we need to find, but here I posted code, can be used directly.

Import Com.google.zxing.common.BitMatrix;
 Import Javax.imageio.ImageIO;
 Import Java.io.File;
 Import Java.io.OutputStream;
 Import java.io.IOException;
 
 Import Java.awt.image.BufferedImage;
   Public final class Matrixtoimagewriter {private static final int black = 0xff000000;
 
   private static final int white = 0xFFFFFFFF;  Private Matrixtoimagewriter () {} public static bufferedimage Tobufferedimage (Bitmatrix matrix) {int width =
     Matrix.getwidth ();
     int height = matrix.getheight ();
     BufferedImage image = new BufferedImage (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);
   } return image; public static void WriteToFile (Bitmatrix matrix, String format, file file) throws IOException {Buf
     Feredimage image = Tobufferedimage (matrix); if (! Imageio.write (image, formaT, file) {throw new IOException ("Could not write ' an image of the format" + format + "to" + file); } public static void WriteToStream (Bitmatrix matrix, String format, OutputStream stream) throws Ioexc
     eption {bufferedimage image = Tobufferedimage (matrix); if (!
     Imageio.write (image, format, stream)) {throw new IOException ("Could not write an image of format" + format); }
   }
 
 }

Now you can download the root directory of D disk to see the generated two-dimensional code.


two-dimensional code parsing and generation, we need a helper class (Bufferedimageluminancesource), the same type of Google also provides, here I also posted the source code, you can directly copy the use of, save the trouble of finding

Bufferedimageluminancesource import Com.google.zxing.LuminanceSource;
 Import Java.awt.Graphics2D;
 Import Java.awt.geom.AffineTransform;
 
 Import Java.awt.image.BufferedImage;
   Public final class Bufferedimageluminancesource extends Luminancesource {private final bufferedimage image;
   private final int left;
 
   private final int top;
   Public Bufferedimageluminancesource (bufferedimage image) {This (image, 0, 0, image.getwidth (), Image.getheight ()); Public Bufferedimageluminancesource (bufferedimage image, int left, int top, int width, int height) {Super (W
 
     Idth, height);
     int sourcewidth = Image.getwidth ();
     int sourceheight = Image.getheight (); if (left + width > Sourcewidth | | top + height > sourceheight) {throw new IllegalArgumentException ("Crop rec
 
     Tangle does not fit within image data. "); for (int y = top, y < top + height; y++) {for (int x = left; x < left + width; + +) {if (imagE.getrgb (x, y) & 0xff000000) = = 0) {Image.setrgb (x, y, 0xFFFFFFFF);//= white}}}
     This.image = new BufferedImage (Sourcewidth, Sourceheight, Bufferedimage.type_byte_gray);
     This.image.getGraphics (). DrawImage (image, 0, 0, NULL);
     This.left = left;
   This.top = top;  @Override public byte[] GetRow (int y, byte[] row) {if (Y < 0 | | y >= getheight ()) {throw new
     IllegalArgumentException ("Requested row is outside the image:" + y);
     int width = getwidth ();
     if (row = = NULL | | Row.length < width) {row = new Byte[width];
     } image.getraster (). getDataElements (left, top + y, width, 1, row);
   return row;
     @Override public byte[] Getmatrix () {int width = getwidth ();
     int height = getheight ();
     int area = width * height;
     byte[] Matrix = new Byte[area];
     Image.getraster (). getDataElements (left, top, width, height, matrix); return matrix;
   @Override public boolean iscropsupported () {true; @Override Public Luminancesource crop (int. left, int top, int width, int height) {return new Bufferedimagel
   Uminancesource (image, This.left + left, This.top + top, width, height);
   @Override public boolean isrotatesupported () {true;
     @Override public Luminancesource 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 to New Bufferedimageluminancesource (Rotatedimage, Top, Sourcewidth-(left + width), getheight (), width); }
 
 }

Code to parse two-dimensional code

 Multiformatreader Formatreader = new Multiformatreader ();
             String FilePath = "path to Picture";
             File File = new file (FilePath);
             BufferedImage image = Imageio.read (file);;
             Luminancesource Source = new Bufferedimageluminancesource (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 the contents of the two-dimensional code in the console.

It's a mere note.



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.