Java code generates QR code images and java code

Source: Internet
Author: User

Java code generates QR code images and java code

Jar package to be used: QRCode. jar

Click here to download QRCode. jar.

Because the code is relatively simple. I don't have to worry about it. I simply post the code and copy it to use it directly.

TwoDimensionCode.java:
1 import java. awt. color; 2 import java. awt. graphics2D; 3 import java. awt. image. bufferedImage; 4 import java. io. file; 5 import java. io. IOException; 6 import java. io. inputStream; 7 import java. io. outputStream; 8 9 import javax. imageio. imageIO; 10 11 import jp. sourceforge. qrcode. QRCodeDecoder; 12 import jp. sourceforge. qrcode. exception. decodingFailedException; 13 14 import com. swetake. util. qrcode; 15 1 6 17 public class TwoDimensionCode {18 19/*** 20 * generate a QR code (QRCode) image 21 * @ param content storage content 22 * @ param imgPath image path 23 */24 public void encoderQRCode (String content, String imgPath) {25 this. encoderQRCode (content, imgPath, "png", 7); 26} 27 28/** 29 * generate a QR code (QRCode) image 30 * @ param content storage content 31 * @ param output stream 32 */33 public void encoderQRCode (String content, OutputStream output) {34 t His. encoderQRCode (content, output, "png", 7); 35} 36 37/** 38 * generate a QR code (QRCode) image 39 * @ param content storage content 40 * @ param imgPath image path 41 * @ param imgType image type 42 */43 public void encoderQRCode (String content, String imgPath, String imgType) {44 this. encoderQRCode (content, imgPath, imgType, 7); 45} 46 47/** 48 * generate a QR code (QRCode) figure 49 * @ param content storage content 50 * @ param output stream 51 * @ param imgType figure Slice type 52 */53 public void encoderQRCode (String content, OutputStream output, String imgType) {54 this. encoderQRCode (content, output, imgType, 7); 55} 56 57/** 58 * generate a QR code (QRCode) image 59 * @ param content storage content 60 * @ param imgPath image path 61 * @ param imgType Image Type 62 * @ param size QR code size 63 */64 public void encoderQRCode (String content, string imgPath, String imgType, int size) {65 try {66 BufferedImage BufImg = this. qRCodeCommon (content, imgType, size); 67 68 File imgFile = new File (imgPath); 69 // generate a QR code QRCode image 70 ImageIO. write (bufImg, imgType, imgFile); 71} catch (Exception e) {72 e. printStackTrace (); 73} 74} 75 76/** 77 * generate a QR code (QRCode) image 78 * @ param content 79 * @ param output stream 80 * @ param imgType image type 81 * @ param size QR code size 82 */83 public void encoderQRCode (String content, out PutStream output, String imgType, int size) {84 try {85 BufferedImage bufImg = this. qRCodeCommon (content, imgType, size); 86 // generate a QR code QRCode image 87 ImageIO. write (bufImg, imgType, output); 88} catch (Exception e) {89 e. printStackTrace (); 90} 91} 92 93/*** 94 * generate a QR code (QRCode) public image method 95 * @ param content 96 * @ param imgType Image Type 97 * @ param size QR code size 98 * @ return 99 */100 private BufferedImage QRCodeCommon (String content, String imgType, int size) {101 BufferedImage bufImg = null; 102 try {103 Qrcode qrcodeHandler = new Qrcode (); 104 // set the error rate of the QR code, optional L (7%), M (15%), Q (25%), H (30%), the higher the error rate, the less information that can be stored, however, the smaller the requirement for two-dimensional code definition is, the smaller the 105 qrcodeHandler. setQrcodeErrorCorrect ('M'); 106 qrcodeHandler. setQrcodeEncodeMode ('B'); 107 // set the two-dimensional code size. The value range is 1-40. The larger the value is, the larger the size is. The greater the information that can be stored, the greater the 108 qrcodeHandler. setQrcodeVersion (size); 109 // byte array of the obtained content, Set the encoding format to 110 byte [] contentBytes = content. getBytes ("UTF-8"); 111 // image size 112 int imgSize = 67 + 12 * (size-1); 113 bufImg = new BufferedImage (imgSize, imgSize, BufferedImage. TYPE_INT_RGB); 114 Graphics2D gs = bufImg. createGraphics (); 115 // set the background color to 116 gs. setBackground (Color. WHITE); 117 gs. clearRect (0, 0, imgSize, imgSize); 118 119 // sets the image color> BLACK120 gs. setColor (Color. BLACK); 121 // set the offset. If this parameter is not set, the parsing may fail. 12 2 int pixoff = 2; 123 // output content> QR code 124 if (contentBytes. length> 0 & contentBytes. length <800) {125 boolean [] [] codeOut = qrcodeHandler. calQrcode (contentBytes); 126 for (int I = 0; I <codeOut. length; I ++) {127 for (int j = 0; j <codeOut. length; j ++) {128 if (codeOut [j] [I]) {129 gs. fillRect (j * 3 + pixoff, I * 3 + pixoff, 3, 3); 130} 131} 132} else {133 throw new Exception ("QRCode conte Nt bytes length = "+ contentBytes. length + "not in [0,800]. "); 135} 136 gs. dispose (); 137 bufImg. flush (); 138} catch (Exception e) {139 e. printStackTrace (); 140} 141 return bufImg; 142} 143 144/** 145 * parse QR code (QRCode) 146 * @ param imgPath image path 147 * @ return148 */149 public String decoderQRCode (String imgPath) {150 // QRCode QR code image File 151 File imageFile = new File (imgPath ); 152 BufferedImage bufImg = nul L; 153 String content = null; 154 try {155 bufImg = ImageIO. read (imageFile); 156 QRCodeDecoder decoder = new QRCodeDecoder (); 157 content = new String (decoder. decode (newTwoDimensionCodeImage (bufImg), "UTF-8"); 158} catch (IOException e) {159 System. out. println ("Error:" + e. getMessage (); 160 e. printStackTrace (); 161} catch (DecodingFailedException dfe) {162 System. out. println ("Error:" + dfe. g EtMessage (); 163 dfe. printStackTrace (); 164} 165 return content; 166} 167 168/** 169 * parse QR code (QRCode) 170 * @ param input stream 171 * @ return172 */173 public String decoderQRCode (InputStream input) {174 BufferedImage bufImg = null; 175 String content = null; 176 try {177 bufImg = ImageIO. read (output); 178 QRCodeDecoder decoder = new QRCodeDecoder (); 179 content = new String (decoder. decode (newTwoDimensionCode Image (bufImg), "UTF-8"); 180} catch (IOException e) {181 System. out. println ("Error:" + e. getMessage (); 182 e. printStackTrace (); 183} catch (DecodingFailedException dfe) {184 System. out. println ("Error:" + dfe. getMessage (); 185 dfe. printStackTrace (); 186} 187 return content; 188} 189 190 public static void main (String [] args) {191 String imgPath = "G:/ZTFCard.png "; 192 String encoderContent =" My business card "+" \ n my weibo: [http://t.qq.com/fengqingyan] "+" \ n Email: [zhitianfeng@hotmail.com] "+" \ n mobile phone: [15601973133] "; 193 TwoDimensionCode handler = new TwoDimensionCode (); 194 handler. encoderQRCode (encoderContent, imgPath, "png"); 195 // try {196 // OutputStream output = new FileOutputStream (imgPath); 197 // handler. encoderQRCode (content, output); 198 //} catch (Exception e) {199 // e. printStackTrace (); 200 // {} 201 System. out. println ("======== encoder success"); 202 203 204 String decoderContent = handler. decoderQRCode (imgPath); 205 System. out. println ("Resolution Result:"); 206 System. out. println (decoderContent); 207 System. out. println ("========= decoder success !!! "); 208} 209}

The second java file: TwoDimensionCodeImage. java

 1 import java.awt.image.BufferedImage; 2  3 import jp.sourceforge.qrcode.data.QRCodeImage; 4  5 public class TwoDimensionCodeImage implements QRCodeImage { 6  7     BufferedImage bufImg; 8      9     public TwoDimensionCodeImage(BufferedImage bufImg) {10         this.bufImg = bufImg;11     }12     13     public int getHeight() {14         return bufImg.getHeight();15     }16 17     public int getPixel(int x, int y) {18         return bufImg.getRGB(x, y);19     }20 21     public int getWidth() {22         return bufImg.getWidth();23     }24 25 }

 

 

The following is a test class: QRtest. java

public class QRtest {    public static void main(String[] args){        TwoDimensionCode td = new TwoDimensionCode();        td.encoderQRCode("this is a test","d://123.jpg","jpg",12);    }}

Additional code: Click to download the code

END!

The following sample image is provided:

 

I 'd like to say that.

I suddenly thought that I could use this method to confess something and generate an I love you! Scan the QR code for the girl you like. Haha


Who has the source code (java version) generated by the QR code?

Import java. io .*;
Import java. util. Date;

Import java. awt .*;
Import java. awt. image .*;
Import javax. imageio .*;

Public class QRCodeEncoderTest
{

/** Creates a new instance of QRCodeEncoderTest */
Public QRCodeEncoderTest ()
{
}

Public static void create_image (String sms_info) throws Exception {
Try {
Qrcode testQrcode = new qrcode ();
TestQrcode. setQrcodeErrorCorrect ('M ');
TestQrcode. setQrcodeEncodeMode ('B ');
TestQrcode. setQrcodeVersion (7 );
String testString = sms_info;
Byte [] d = testString. getBytes ("gbk ");
System. out. println (d. length );
// BufferedImage bi = new BufferedImage (98, 98, BufferedImage. TYPE_INT_RGB );
BufferedImage bi = new BufferedImage (98, 98, BufferedImage. TYPE_BYTE_BINARY );
Graphics2D g = bi. createGraphics ();
G. setBackground (Color. WHITE );
G. clearRect (0, 0, 98, 98 );
G. setColor (Color. BLACK );

// Limit the maximum number of bytes to 120
If (d. length> 0 & d. length <120 ){
Boolean [] [] s = testQrcode. calQrcode (d );
For (int I = 0; I <s. length; I ++ ){
For (int j = 0; j <s. length; j ++ ){
If (s [j] [I]) {
G. fillRect (j * 2 + 3, I * 2 + 3, 2 );
}
}
}
}
G. dispose ();
Bi. flush ();
... The remaining full text>

The urgency QR code generator java finally saves the image to a directory.

I know that the software is sold by calling the camera of the Local Machine, taking a photo, saving it as a QR code, and then you can modify some code and save it as an image in the specified directory,

Related Article

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.