Using zxing to implement a two-dimensional code generator inline image _java

Source: Internet
Author: User
Tags generator

Using zxing to realize the embedded image of two-dimensional code generator has certain reference value, as follows:

The basic idea is to use the zxing generated two-dimensional code picture, and then read the picture, insert the icon in it, and then the entire output picture.

Recent projects need to generate two-dimensional code, looking for a few examples of synthesis, to make the final effect, two-dimensional code can generate picture format (JPG, etc.) or on the Web page display, this piece of article only for record, the similarities of a lot, forgive ....

Note: the need for zxing packaging tool class, the approximate process is to read the embedded picture, the content into two-dimensional code, the image embedded into two-dimensional code, out of the picture.

Here's the complete code:

Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.Shape;
Import Java.awt.geom.AffineTransform;
Import Java.awt.geom.RoundRectangle2D;
Import java.awt.image.AffineTransformOp;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Java.util.HashMap;


Import Java.util.Map;


Import Javax.imageio.ImageIO;
Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.EncodeHintType;
Import Com.google.zxing.MultiFormatWriter;
Import com.google.zxing.WriterException;
Import Com.google.zxing.common.BitMatrix;


Import Com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class Zxing {private static final int black = 0xff000000, private static final int white = 0xFFFFFFFF;//Picture width of one 
 Like private static final int image_width = 80; 
 private static final int image_height = 80; 
 private static final int image_half_width = IMAGE_WIDTH/2; private static final int frame_width =2; 
Two-dimensional code writer private static Multiformatwriter mutiwriter = new Multiformatwriter (); public static void Main (string[] args) {try {//bitmatrix Bitmatrix = multiformatwriter.encode (content, Barcodeformat.qr_
CODE, 400,hints);
  String content= "13400000000";//two-dimensional code content BufferedImage image = Genbarcode (content,,, "f:\\amazed.png"); if (!
  Imageio.write (image, "JPG", New File ("F:\\2122.jpg")) {throw new IOException ("Could not write an image of format");

 /**//Change the above code here, use the stream to read into the page can outputstream OS = Response.getoutputstream (); if (!

      Imageio.write (image, "JPG", OS)) {throw new IOException ("Could not write an image of format");  **/} catch (Writerexception e) {e.printstacktrace ();} catch (IOException e) {/TODO auto-generated catch
Block E.printstacktrace (); } private BufferedImage Tobufferedimage (Bitmatrix matrix) {int width = matrix.getwidth (); int height = Matrix.getheigh
T (); 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; private static BufferedImage Genbarcode (string content, int width, int height, String srcimagepath) throws Writere Xception, IOException {//Read source image BufferedImage Scaleimage = scale (Srcimagepath, Image_width, Image_h 
   eight, true); 
   int[][] Srcpixels = new Int[image_width][image_height]; for (int i = 0; i < scaleimage.getwidth (); i++) {for (int j = 0; J < Scaleimage.getheight (); j + +) {s 
     RCPIXELS[I][J] = Scaleimage.getrgb (i, j); 
   } map<encodehinttype, object> hint = new Hashmap<encodehinttype, object> (); Hint.put (Encodehinttype.character_set, "utf-8"); Content encoding Hint.put (encodehinttype.error_correction, ErrorCorrectionLevel.H);//Error level Hint.put (Encodehinttype.margin, 1) ; Sets the width of the blank area of the outer border of the two-dimensional code//generate two-dimensional code
   Bitmatrix matrix = mutiwriter.encode (content, Barcodeformat.qr_code, width, height, hint); 
   The two-dimensional matrix is converted into one dimensional pixel Group int halfw = Matrix.getwidth ()/2; 
   int halfh = Matrix.getheight ()/2; 
 
   int[] pixels = new int[width * height]; 
       for (int y = 0; y < matrix.getheight (); y++) {A for (int x = 0; x < matrix.getwidth () + x + +) {//Read picture  if (x > Halfw-image_half_width && x < HALFW + image_half_width && y  > Halfh-image_half_width && y < HALFH + image_half_width) {Pixels[y * WIDTH + x] = 
       SRCPIXELS[X-HALFW + IMAGE_HALF_WIDTH][Y-HALFH + image_half_width]; ///In picture four weeks to form a border else if (x > Halfw-image_half_width-frame_width && x < HALF W-image_half_width + frame_width && y > Halfh-image_half_width-frame_width && y < HALFH + image_half_width + FRAMe_width) | | 
               (x > HALFW + image_half_width-frame_width && x < HALFW + Image_half_width + frame_width && y > Halfh-image_half_width-frame_width && y < HALFH + Image_hal F_width + frame_width) | | 
               (x > Halfw-image_half_width-frame_width && x < HALFW + Image_half_width + frame_width && y > Halfh-image_half_width-frame_width && y < Halfh-image_hal F_width + frame_width) | | 
               (x > Halfw-image_half_width-frame_width && x < HALFW + Image_half_width + frame_width && y > halfh + image_half_width-frame_width && y < HALFH + Image_hal 
       F_width + frame_width)) {Pixels[y * WIDTH + x] = 0XFFFFFFF; else {//Here you can modify the color of the two-dimensional code, you can set the color of two-dimensional code and background respectively; Pixels[y * width + x] = maTrix.get (x, y)? 
       0XFF000000:0XFFFFFFF; 
   }} bufferedimage image = New BufferedImage (width, height, bufferedimage.type_int_rgb); 
 
   Image.getraster (). setDataElements (0, 0, width, height, pixels); 
 return image; /** * The incoming original image is scaled by height and width to produce the icon that meets the requirement * * @param srcimagefile * source file Address * @param height * High target * @param width * @param hasfiller * If the ratio is incorrect, whether filler is required: true for filler; 
  False is not filler;  * @throws IOException */private static BufferedImage scale (String srcimagefile, int height, int width, Boolean 
   Hasfiller) throws IOException {double ratio = 0.0;//Scaling File = new file (srcimagefile); 
   BufferedImage srcimage = imageio.read (file); 
   Image destimage = srcimage.getscaledinstance (width, height, bufferedimage.scale_smooth); Calculate scale if (srcimage.getheight () > height) | | (Srcimage.getwidth () > width)) {if (Srcimage.getheight () > SRCimage.getwidth ()) {ratio = (new Integer (height)). Doublevalue ()/srcimage.getheight (); 
     else {ratio = (new Integer (width)). Doublevalue ()/srcimage.getwidth (); 
     } affinetransformop op = new Affinetransformop (affinetransform.getscaleinstance (ratio, ratio), null); 
   Destimage = Op.filter (srcimage, NULL); } if (Hasfiller) {//filler bufferedimage image = new BufferedImage (width, height, bufferedimage.type_int_ 
     RGB); 
     Graphics2D graphic = Image.creategraphics (); 
     Graphic.setcolor (Color.pink); 
     Graphic.fillrect (Ten, width, height);
     Graphic.drawrect (360, width, height); if (width = = destimage.getwidth (null)) {graphic.drawimage (destimage, 0, Height-destimage.getheight (null)) 
       /2, Destimage.getwidth (null), destimage.getheight (null), color.white, NULL); Shape shape = new Roundrectangle2d.float (0, Height-destimAge.getheight (NULL))/2, Width, width, 20, 20);
       Graphic.setstroke (New Basicstroke (5f));
     Graphic.draw (Shape); else {graphic.drawimage (Destimage, (Width-destimage.getwidth (NULL))/2, 0, Desti
       Mage.getwidth (NULL), destimage.getheight (null), color.white, NULL);
       Shape shape = new Roundrectangle2d.float ((Width-destimage.getwidth (NULL))/2, 0, Width, width, 20, 20);
       Graphic.setstroke (New Basicstroke (5f));
     Graphic.draw (Shape); 
     } graphic.dispose (); 
     
   Destimage = image; 
 Return (bufferedimage) destimage;

 } 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat 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.