Using QRCode and zxing to generate two-dimensional code

Source: Internet
Author: User

First add the required jar to the project

Qrcode.jar, Zxingcore-2.2.jar,Javase-2.2.jar (also can not add, add a Matrixtoimagewriter class may also)

Package com.demo;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;/** * *  @ClassName:  matrixtoimagewriter * @ description:  If you do not want to add Javase-2.2.jar, add this class can *  @date  2015 April 30   morning 10:05:51*  @since     AR1.0 */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 {    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 {     bufferedimage image = tobufferedimage (Matrix);     if  (! Imageio.write (Image, format, stream))  {      throw new  IOException ("could not write an image of format "  + format);     }  }}

test class

package com.demo;import java.awt.color;import java.awt.graphics2d;import  java.awt.image.bufferedimage;import java.io.file;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.common.bitmatrix;import com.swetake.util.qrcode;public class qrcodedemo {  public static void main (String[] args)  {  qrcodedemo.encoderqrcode (" This is the QR code generated with QRCode ", " C:/users/liyy/desktop/testqrcode.png ");   qrcodedemo.zxingtest (" This is the QR code generated using zxing ", " C:/users/liyy/desktop/testzxing.png ");  } /**  *  * @ title: encoderqrcode  *  @Description:  using QRCode to generate QR codes   *  @param  @ param content  *  @param   @param  imgpath  *&nBSP; @return  void  return type   *  @throws   */ public static void  encoderqrcode (String content, string imgpath)  {  try {    qrcode qrcodehandler = new qrcode ();    Qrcodehandler.setqrcodeerrorcorrect (' M ');//  error correction    qrcodehandler.setqrcodeencodemode (' B ');    qrcodehandler.setqrcodeversion (7);   byte[] contentbytes =  Content.getbytes ("gb2312");    bufferedimage bufimg = new bufferedimage (140, &NBSP;140,&NBSP;BUFFEREDIMAGE.TYPE_INT_RGB);   graphics2d gs =  Bufimg.creategraphics ();    gs.setbackground (Color.White);    gs.clearrect (0,  0, 140, 140);   //  Set the image Color > black   gs.setcolor ( Color.Black);   //  set the offset &NBSP;&NBSP;&NBSP;INT&NBSP;PIXOFF&Nbsp;= 2;   //  output content >  QR code    if  (contentbytes.length  > 0)  &&  (contentbytes.length < 120)  {     Boolean[][] codeout = qrcodehandler.calqrcode (contentbytes);    for  ( int i = 0; i < codeout.length; i++)  {      for  (int j = 0; j < codeout.length; j++)  {       if  (Codeout[j][i])  {       gs.fillrect ((j  * 3)  + pixoff,  (i * 3)  + pixoff, 3, 3);       }     }    }   } else  {    system.err.println ("qrcode content bytes length = "  +  contentbytes.length +  " not in [ 0,120 ]. ");   }    Gs.dispose ();    bufimg.flush ();    file imgfile = new file ( Imgpath);   //  generate QR code qrcode picture    imageio.write (bufimg,  "PNG",  Imgfile);    system.out.println ("QRCode has output two-dimensional code! ");  } catch  (exception e)  {   e.printstacktrace ();   }  } /**  *  *  @Title: zxingtest  *  @Description:  Use Google's zxing to generate QR codes   *  @param   *  @return  void  return types   * @ throws  */  @SuppressWarnings ({  "rawtypes",  "Unchecked" &NBSP;})  public  Static void zxingtest (String content, string imgpath)  {  try {    multiformatwriter multiformatwriter = new mulTiformatwriter ();    map hints = new hashmap ();    hints.put ( encodehinttype.character_set,  "UTF-8");   bitmatrix bitmatrix =  Multiformatwriter.encode (content, barcodeformat.qr_code, 400, 400, hints);    file file1 = new file (Imgpath);    matrixtoimagewriter.writetofile ( bitmatrix,  "JPG",  file1);    system.out.println ("zxing has output two-dimensional code!");   } catch  (exception e)  {   e.printstacktrace ();   }  }}

Using QRCode and zxing to generate two-dimensional code

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.