Java code parsing two-dimensional code general steps
This paper uses Google's zxing technology to parse the two-dimensional code technology, the general steps to parse the QR code are as follows:
First, download the Zxing-core jar package:
Second, the creation of a Bufferedimageluminancesource class inheritance Luminancesource, this class in Google's source code, but for the convenience of use, the following have this type of source code, you can directly copy the use:
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); NT Sourcewidth = Image.getwidth (); int sourceheight = Image.getheight (); if (left + width > Sourcewidth | | top + height & Gt 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; + +) {if (Image.getrgb (x, y) & 0 xFF000000) = = 0) {Image.setrgb (x, y, 0xFFFFFFFF);//= White}}}this.image = new BufferedImage (Sourcewidth, Sourceheight, B Ufferedimage.type_byte_gray); This.image.getGraphics (). DrawImage (image, 0, 0, null); this.left = Left;this.top = top;} @Overridepublic byte[] GetRow (int y, byte[] row) {if (Y < 0 | | y >= getheight ()) {throw newIllegalArgumentException ("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; @Overridepublic byte[] Getmatrix () {int width = getwidth (); int height = getheight (); int area = width * height;byte[] Matri x = new Byte[area];image.getraster (). getDataElements (left, top, width, height, matrix); return matrix;} @Overridepublic Boolean iscropsupported () {return true;} @Overridepublic luminancesource crop (int left, int top, int width, int height) {return new Bufferedimageluminancesource (IM Age, This.left + left, This.top + top, width, height);} @Overridepublic Boolean isrotatesupported () {return true;} @Overridepublic 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 = newBufferedImage (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);}
Three, create a startup class to parse the QR code:
public static void Main (string[] args) {try {multiformatreader formatreader = new Multiformatreader (); String filePath = "d:/test/blog. 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 (); Decoding is set to UTF-8 hints.put (Encodehinttype.character_set, "UTF-8"); Optimization accuracy hints.put (Decodehinttype.try_harder, boolean.true); Complex mode, open Pure_barcode mode Hints.put (Decodehinttype.pure_barcode, boolean.true); 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 ();}}
Four, after the personal testing can realize the two-dimensional code analysis, can obtain the QR code URL and other information;
Java code parsing two-dimensional code