A simple example of Java using zxing to generate two-dimensional code _java

Source: Internet
Author: User
Tags string format

Zxing is a Google-provided on the barcode (one-dimensional code, two-dimensional code) analytical tools, provides a two-dimensional code generation and resolution method, now I briefly introduce the use of Java to generate and parse two-dimensional code zxing

1, two-dimensional code generation

1.1 Add the Zxing-core.jar package to the classpath.

1.2 Two-dimensional code generation needs to use the Matrixtoimagewriter class, this class is provided by Google, you can copy the class to the source code, here I will be the source code affixed to this category, you can use 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 = Mat
   Rix.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 {Bufferedim
   Age 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 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" + format); }
  }
 
 }

1.3 To write the implementation code to generate two-dimensional 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, 400,hints);
   File File1 = new file (path, "napkin. jpg");
   Matrixtoimagewriter.writetofile (Bitmatrix, "JPG", file1);
   
 } catch (Exception e) {
   e.printstacktrace ();
 }

Now run to generate a two-dimensional code picture, is not very simple ah? Next we'll see how to parse the two-dimensional code

2, the analysis of two-dimensional code

2.1 Add the Zxing-core.jar package to the classpath.

2.2 and build, we need a helper class (Bufferedimageluminancesource), the same type of Google also provides, here I also put the source code of the class, 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 (width,
 
   height);
   int sourcewidth = Image.getwidth ();
   int sourceheight = Image.getheight (); if (left + width > Sourcewidth | | top + height > sourceheight) {throw new IllegalArgumentException ("Crop Rectan
 
   GLE 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) & 0xff0000= = 0) {Image.setrgb (x, y, 0xFFFFFFFF);//= white}} this.image = new BufferedImage (sourcew
   Idth, 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 Illega
   Largumentexception ("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 Bufferedimageluminancesource (imag
  E, 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); }
 
 }

2.3 To write the implementation code to parse two-dimensional code

try {
             Multiformatreader formatreader = new Multiformatreader ();
       String FilePath = "c:/users/administrator/desktop/testimage/test.jpg";
       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 that the console prints out the contents of the two-dimensional code.

So far, the use of zxing to generate and parse two-dimensional code to tell the completion of the presentation, mainly for their own to make a memo, while facilitating the need for people. Oh!

The above Java use zxing to generate two-dimensional code is a simple example of the small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.