Using Qrcode.jar to generate and parse two-dimensional codes (non-zxing)

Source: Internet
Author: User
Tags getmessage int size set background


For a few days Android, feeling zxing very large, a lot of bags,

Personal recommendation: Java EE display two-dimensional code, or upload to two-dimensional code to parse or .... Android Build & scan & Parse two-dimensional code

Download Address

http://download.csdn.net/detail/hejias/5891099

-------------------------------------------------------------------Gorgeous split Line------------------------------------------------ ----------------------------------

1.

Package com.test;


Import Java.awt.image.BufferedImage;


Import Jp.sourceforge.qrcode.data.QRCodeImage;

public class Twodimensioncodeimage implements Qrcodeimage {

	bufferedimage bufimg;
	
	Public Twodimensioncodeimage (BufferedImage bufimg) {
		this.bufimg = bufimg;
	}
	
	@Override public
	int getheight () {return
		bufimg.getheight ();
	}

	@Override public
	int getpixel (int x, int y) {return
		Bufimg.getrgb (x, y);
	}

	@Override public
	int getwidth () {return
		bufimg.getwidth ();
	}

}

2

Package com.test;
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.InputStream;

Import Java.io.OutputStream;

Import Javax.imageio.ImageIO;
Import Jp.sourceforge.qrcode.QRCodeDecoder;

Import jp.sourceforge.qrcode.exception.DecodingFailedException;

Import Com.swetake.util.Qrcode;  /** * * * @author jiaxi * */public class Twodimensioncode {public static void main (string[] args) {String  
         Imgpath = "D:/qrcode.png";  
         String encodercontent = "Hello big, small, Welcome to qrcode!" + "+";  
         Twodimensioncode handler = new Twodimensioncode ();  
         Handler.encoderqrcode (Encodercontent, Imgpath, "PNG");  
           
           
         System.out.println ("========encoder success");  
         String decodercontent = Handler.decoderqrcode (Imgpath);  
         System.out.println ("The analytic result is as follows:");  
         System.out.println (decodercontent); System.oUt.println ("========decoder success!!!"); /** * Generate two-dimensional code (qrcode) picture * @param Content Store Contents * @param imgpath picture path/public void Encod  
    Erqrcode (string content, String Imgpath) {This.encoderqrcode (content, Imgpath, "PNG", 7); /** * Generate two-dimensional code (qrcode) picture * @param content Storage Contents * @param output stream/public voi  
    D Encoderqrcode (String content, OutputStream output) {this.encoderqrcode (content, Output, "PNG", 7); /** * Generate two-dimensional code (qrcode) picture * @param Content Store Contents * @param imgpath Picture path * @param imgtype Picture type */public void Encoderqrcode (string content, String Imgpath, String imgtype) {This.encoderqrco  
    De (content, Imgpath, Imgtype, 7); /** * Generate a two-dimensional code (qrcode) picture * @param content Storage Contents * @param output stream * @param imgtype picture Type */public void Encoderqrcode (String content, outputstream output, String imgtype) {this.encoderqrcode (content, output, imgtype, 7);  
     /** * Generate two-dimensional code (qrcode) picture * @param Content Store Contents * @param imgpath Picture path * @param imgtype picture type * @param size Two-dimensional code size */public void Encoderqrcode (string content, String Imgpath, string imgtype, int si  
              
            Ze) {try {bufferedimage bufimg = This.qrcodecommon (content, imgtype, size);  
            File Imgfile = new file (Imgpath);  
        Generate two-dimensional Code qrcode picture Imageio.write (bufimg, Imgtype, imgfile);  
        catch (Exception e) {e.printstacktrace (); /** * Generate two-dimensional code (qrcode) picture * @param content Storage Contents * @param output stream * @param Imgtyp E Picture Type * @param size two-dimensional code size */public void Encoderqrcode (string content, OutputStream output, String imgt ype, int size) {try {bufferedimage bufimg = This.qrcodecommon (content,Imgtype, size);  
        Generate two-dimensional Code qrcode picture Imageio.write (bufimg, imgtype, Output);  
        catch (Exception e) {e.printstacktrace (); /** * Generate a two-dimensional code (qrcode) Picture of the public method * @param content Storage Contents * @param imgtype Picture type * @p Aram Size Two-dimensional code size * @return/Private BufferedImage Qrcodecommon (string content, string imgtype, int size  
        ) {BufferedImage bufimg = null;  
            try {qrcode Qrcodehandler = new QRCode (); Set the two-dimensional code error rate, optional L (7%), M (15%), Q (25%), H (30%), the higher the rate of error can be stored less information, but the requirements of two-dimensional code clarity Qrcodehandler.setqrcodeerrorcorrect (' M  
            ');  
            Qrcodehandler.setqrcodeencodemode (' B ');  
            Set up the two-dimensional code size, the value range 1-40, the larger the value of the larger size, the more information can be stored qrcodehandler.setqrcodeversion (size);  
            Gets the byte array of the content, sets the encoding format byte[] contentbytes = content.getbytes ("Utf-8"); Picture size int imgsize = + (size-1);  
            bufimg = new BufferedImage (imgsize, imgsize, Bufferedimage.type_int_rgb);  
            graphics2d GS = Bufimg.creategraphics ();  
            Set Background color gs.setbackground (color.white);  
  
            Gs.clearrect (0, 0, imgsize, imgsize);  
            Set Image Color > Black gs.setcolor (Color.Black);  
            Set offset, no setting may cause parsing error int pixoff = 2;  
                Output > Two-dimensional code if (Contentbytes.length > 0 && contentbytes.length < 800) {  
                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 {throw new ExcePtion ("qrcode content bytes Length =" + Contentbytes.length + "not in [0, 800].");}  
            Gs.dispose ();  
        Bufimg.flush ();  
        catch (Exception e) {e.printstacktrace ();  
    return bufimg; /** * Parse two-dimensional code (QRCODE) * @param imgpath Picture Path * @return/public String DECODERQ  
        Rcode (String Imgpath) {//QRCode two-dimensional code picture files File ImageFile = new file (Imgpath);  
        BufferedImage bufimg = null;  
        String content = null;  
            try {bufimg = Imageio.read (ImageFile);  
            Qrcodedecoder decoder = new Qrcodedecoder ();   
        Content = new String (Decoder.decode (New Twodimensioncodeimage (bufimg)), "Utf-8");  
            catch (IOException e) {System.out.println ("Error:" + e.getmessage ());  
        E.printstacktrace (); catch (Decodingfailedexception DfE) {System.out.println("Error:" + dfe.getmessage ());  
        Dfe.printstacktrace ();  
    } return content;  
      
 }


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.