Explain the method of Zxing-core generating two-dimensional code and analyze _java

Source: Internet
Author: User
Tags string format

Two-dimensional code everywhere, sweep a sweep there are gifts Oh, now two-dimensional code is so popular, presumably we are not very clear how the two-dimensional code is generated, now small series by sharing this article to help you learn two-dimensional code generation method.

In fact, this feature is mainly used by the jar published by Goggle.

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 found out that there was a matrixtoimagewriter error on the copy of the code, we needed to find it, but I posted the code here and I could use it 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.GETW 
Idth (); 
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 {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 WriteToStream (Bitmatrix matrix, String format, OutputStream stream) throws IOException {Buffer 
Edimage 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

Like the build, we need a helper class (Bufferedimageluminancesource), the same kind of Google also provides, here I will also post 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 (width, Heig 
HT); 
int sourcewidth = Image.getwidth (); 
int sourceheight = Image.getheight ();  if (left + width > Sourcewidth | | top + height > sourceheight) {throw new 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 (y) &am P 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 illegalargument 
Exception ("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 Bufferedimageluminanceso Urce (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 ()); 
TCH (Exception e) { 

Now you can see the contents of the two-dimensional code in the console.

The above is a small set to introduce the Zxing-core to generate two-dimensional code and analysis of the relevant knowledge, hope for everyone to help, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.