Two-dimensional code is able to encapsulate pictures, files and so on, mainly to the image, files and other URLs encoded into two-dimensional code, users sweep the phone automatically access.
The last time in "JQuery" using the Jquery-qrcode plugin to convert the URL into a QR code, mobile phone scanning can be accessed (click to open the link) using the Jquery-qrcode plug-in with jQuery can encode the URL into a string, on the URL to display the encoded QR code, Mobile devices can sweep directly.
This time in Java to implement the QR code encoding and decoding output to disk, do not need to do in the Web page, but also can decode, but the steps are more complex.
Java itself certainly does not have the QR code encoding and decoding function, need to download two plug-ins, that is, the additional jar package, one for encoding, the string, URL, URL conversion into a QR code com.swetake.util.Qrcode, can be downloaded from its official website (click to open the link), I also uploaded a copy for everyone (click the Open link), another to decode, the QR code into the original string, URL, URL, such as Jp.sourceforge.qrcode.data.QRCodeImage, this can also be downloaded from its official website (click to open the link), I also uploaded a copy for everyone (click to open the link)
The encoded Com.swetake.util.Qrcode is downloaded after a qrcode_ Java0.50beta10.tar.gz, the latest version of the WinRAR can be decompressed, unzip the inside Lib directory Qrcode.jar out, change a name called Qrcodeencoding.jar, decoding Jp.sourceforge.qrcode.data.QR Codeimage after the download is a redir.zip, unzip the same inside the Lib directory qrcode.jar out, change a name called Qrcodedecoding.jar. Originally these third party jar packages, no modifications can be thrown into the Java project to use. However, these two Japanese companies, it is too predestined, the development of the two-dimensional code bag hit shirt. Therefore, to change the name in order to put together. Change to another name is OK, anyway can not be called QRCode at the same time, My code is called Qrcode.jar, decoding called Qrcodedecoding.jar, the main is the first do not know the name of the two jar is the same, and introduced the code of the Com.swetake.util.Qrcode, did not change the name of the code package.
Create a new Java project in Eclipse, call it random, my name is QRCode, then finally manually open the project directory of Eclipse, find this folder, create a new lib in it, Put Qrcodeencoding.jar and Qrcodedecoding.jar in, then refresh the project in Eclipse, right-click the bottom of the property, select Add Jars in the Java Build path libraries. If it is not copied to the project directory, select Add External Jars. Add these two packages:
After that, a new Qrcode.java is created under the package under SRC, which is a class. Thus, the entire project directory structure is as follows, there should be no test.png,test.png is later generated by the QR code file:
When the preparation is ready, you can write the code formally.
Directly affixed to the full code, then a line of people to analyze, how to do.
Package Qrcode;import Java.awt.*;import Java.awt.image.*;import java.io.*;import javax.imageio.*;import Jp.sourceforge.qrcode.qrcodedecoder;import Jp.sourceforge.qrcode.data.qrcodeimage;import Com.swetake.util.Qrcode ;p Ublic class QRCode {public static void Makeqrcode (string content, String imgpath) {QRCode QRCode = new QRCode (); qrcode.s Etqrcodeerrorcorrect (' M '); Qrcode.setqrcodeencodemode (' B '); qrcode.setqrcodeversion (7); BufferedImage bufimg = new BufferedImage (325, 325,BUFFEREDIMAGE.TYPE_INT_RGB); graphics2d GS = Bufimg.creategraphics (); Gs.setbackground (Color.White); gs.clearrect (0, 0, 325, 325); Gs.setColor ( Color.Black); try {byte[] contentbytes = content.getbytes ("Utf-8"); if (0 < Contentbytes.length && Contentbytes.length <) {boolean[][] codeout = Qrcode.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 * 7 + 5, I * 7 + 5, 7, 7);}}} Gs.dispose (); Bufimg.flush (); File Imgfile = New File (Imgpath);//Born into Pngimageio.write (bufimg, "PNG", imgfile); System.out.println ("Build succeeded! ");} catch (Exception e) {System.out.println ("Error! ");}} public static string Decodeqrcode (String imgpath) throws IOException {file ImageFile = new File (Imgpath); BufferedImage bufimg = null; String Decodeddata = null;bufimg = Imageio.read (ImageFile); Qrcodedecoder decoder = new Qrcodedecoder ();d ecodeddata = new String (Decoder.decode (New J2seimage (bufimg))); System.out.println ("Parse success! "); return decodeddata;} public static void Main (string args[]) throws IOException {string content = "http://www.a.com"; File directory = new file (""); String Imgpath = Directory.getcanonicalpath () + "\\test.png"; makeqrcode (content, Imgpath); System.out.println (Decodeqrcode (Imgpath));}} Class J2seimage implements Qrcodeimage {bufferedimage bufimg;public j2seimage (bufferedimage bufimg) {this.bufimg = bufimg;} public int getwidth () {return bufimg.getwidth ();} public int getheight () {return bufimg.getheight ();} public int GetPixel (int x, int y) {return Bufimg.getrgb (x, y);}}
1, first introduced in the head:
Import Java.awt.*;import java.awt.image.*;import java.io.*;import com.swetake.util.Qrcode;
2, then write Makeqrcode this class, the code is as follows:
Since there is also a main function in this class, add the static property/* * @content the content to encode * @imgPath the path generated by the QR code *///it is said that the above description of the parameter annotation method, in line with the name of the Enterprise standard, ╮ (╯▽╰) ╭, you can take a damn look at it. public static void Makeqrcode (string content, String Imgpath) {///The following are a number of specified actions, not why, this class will write this,//can change the part I will comment qrcode QRCode = new QRCode ();//Here is the fault tolerance, there are l,m,q three kinds, L is the smallest, generally if you want to add a picture on the QR code, use the q//note here must use single quotation marks, because this is a char or int type, Instead of string type Qrcode.setqrcodeerrorcorrect (' M '); Qrcode.setqrcodeencodemode (' B '); qrcode.setqrcodeversion (7);// Create a new 325x325px image, this size is not changed to change, there are fastidious, and the following gs.fillrect statement associated bufferedimage bufimg = new BufferedImage (325, 325, BUFFEREDIMAGE.TYPE_INT_RGB); graphics2d GS = Bufimg.creategraphics ();//The background color of the image is white gs.setbackground (color.white);//The 325x325 here must be bufferedimage with the above bufimg = new BufferedImage (325, 325, BUFFEREDIMAGE.TYPE_INT_RGB); corresponding//above is 325x325 you must be here how much gs.clearrect (0, 0, 325, 325);// The color of the QR code is black Gs.setcolor (color.black);//It does not throw, do not let compile through ╮(╯▽╰)╭ try {//encoding for UTF-8, now also write gb2312 and GBK that is to die, what age, encoding is also globalized! byte[] Contentbytes = content.getbytes ("Utf-8");//can encode the length of the string 0-120 bytes, the fixed length of the two-dimensional code to come, do not change if (0 < Contentbytes.lengTh && Contentbytes.length < +) {boolean[][] codeout = Qrcode.calqrcode (contentbytes); for (int i = 0; I < C Odeout.length; i++) {for (int j = 0; J < Codeout.length; J + +) {if (Codeout[j][i]) {//Here is each point for 7x7//this 7 unification change, if you want every point to be 6x6, then this sentence must be written Gs.fillr ECT (J * 6 + 5, I * 6 + 5, 6, 6);//That 5 is the edge of the white, that is, the two-dimensional code outside there are some white border, if this number is 0, close to the edge of the picture is said to be wrong//set to 0 I did not try, just to look good to stay 5px Left white, the picture size is 7*45+5*2=325//if you each point is 6*6, stay white 3px, then the image size is 6*45+3*2=276. That is, the above bufferedimage bufimg = new BufferedImage (325, 325, BUFFEREDIMAGE.TYPE_INT_RGB); with Gs.clearrect (0, 0, 325, 325); Self-improvement//The formula of 45 and 2 is fixed, two-dimensional code is this, not for what. Gs.fillrect (J * 7 + 5, I * 7 + 5, 7, 7);}}} Gs.dispose (); Bufimg.flush (); File Imgfile = new file (imgpath);//Born into Pngimageio.write (bufimg, "PNG", imgfile); System.out.println ("Build succeeded! ");} catch (Exception e) {System.out.println ("Error! ");}}
3, then the following main function write to the string to be encoded, such as http://www.a.com this URL, and the saved picture directory, such as the current project directory, you can complete the code of the two-dimensional code work:
public static void Main (string args[]) throws IOException {//String to be encoded string content = "http://www.a.com";//Remove current project directory, and indicate that the save file is named Test.pngfile directory = new File (""); String Imgpath = Directory.getcanonicalpath () + "\\test.png";//Makeqrcode encoding method Makeqrcode (content, Imgpath);}
The main function needs to throw an exception because it uses the Fetch engineering directory function in file, and Java is afraid to fail to get the project directory.
4, the following two-dimensional code decoding work, first introduced in the file header
Import Javax.imageio.*;import Jp.sourceforge.qrcode.qrcodedecoder;import jp.sourceforge.qrcode.data.QRCodeImage;
5, first at the bottom of the file to complete the qrcodeimage of this interface package, White is to convert the picture into qrcodeimage, it this jar package can be successfully decoded, so to encapsulate this interface, cannot be forced type conversion.
Class J2seimage implements Qrcodeimage {bufferedimage bufimg;public j2seimage (bufferedimage bufimg) {this.bufimg = bufimg;} public int getwidth () {return bufimg.getwidth ();} public int getheight () {return bufimg.getheight ();} public int GetPixel (int x, int y) {return Bufimg.getrgb (x, y);}}
This interface is easy to package! is to take the picture, the length of the width, the color of each pixel.
6, the Qrcodeimage this interface package into J2seimage, write a Decodeqrcode This class, the code is as follows:
<span style= "White-space:pre" ></span>public static string Decodeqrcode (String imgpath) throws IOException {/* * @imageFile decoded file * @BufferedImage This is the so-called picture buffer stream * @decodedData store the decoded content */file ImageFile = new file (Imgpath); BufferedImage bufimg = null; String Decodeddata = null;bufimg = Imageio.read (ImageFile); Qrcodedecoder decoder = new Qrcodedecoder ();//The decoding of the QR code can be done directly with the Decode method after encapsulation Decodeddata = new String (Decoder.decode (new J2seimage (bufimg)); System.out.println ("Parse success! ");//finally return the decoded content to Okreturn decodeddata;}
By adding a System.out.println (Decodeqrcode (Imgpath)) to the main function, you can output the decoded content in the console, run it, and generate an http://In the project directory. Www.a.com URL is encoded into the QR code image test.png, this test.png will not be transmitted up, now the person is a TMD sword, nothing to take cell phone on the QR code, curiosity killed the cat, I put a deduction fee URL you will hehe, while the console output:
"Java" uses Java to encode and decode URLs in two-dimensional code