QR code generation

Source: Internet
Author: User
Package liupeng; import Java. io. file; import Org. JSON. jsonexception; import Org. JSON. jsonobject; public class test {/*** Description: QR code generation * @ author liupeng * @ Param ARGs * @ throws jsonexception */public static void main (string [] ARGs) throws jsonexception {jsonobject OBJ = new jsonobject (); obj. put ("name", "liupeng"); obj. put ("Age", 22); obj. put ("school", "[Yancheng Normal University] Emy of Information Science and Technology-> Software Engineering"); // string tomcatroot = httpreq Uest. getsession (). getservletcontext (). getrealpath ("/sealandsignimages"); // string Path = qrcodeoperator. makeqrcode (tomcatroot, "Liu Peng's QR code", obj. tostring (); string Path = qrcodeoperator. makeqrcode ("F:" + file. separator, "Liu Peng's QR code", obj. tostring (); If (path! = "") {System. out. println ("the QR code content is:" + obj. tostring () ;}} package liupeng; import Java. AWT. image. bufferedimage; import Java. io. file; import Java. util. hashmap; import Java. util. map; import javax. imageIO. imageIO; import javax. servlet. HTTP. httpservletrequest; import COM. google. zxing. barcodeformat; import COM. google. zxing. binarizer; import COM. google. zxing. binarybitmap; import COM. google. zxing. encodehinttype; import Com. google. zxing. luminancesource; import COM. google. zxing. multiformatreader; import COM. google. zxing. multiformatwriter; import COM. google. zxing. result; import COM. google. zxing. common. bitmatrix; import COM. google. zxing. common. hybridbinarizer; public class qrcodeoperator {/*** generate a QR code * @ Param name: name of the QR code file (without a suffix) * @ Param content QR code content * @ return QR code image path */public static string makeqrcode (string filepath, stri Ng name, string content) {string retpath = ""; try {// string content = "120605181003; http://www.cnblogs.com/jtmjx"; // string Path = fileoperator. getrootpath (); string Path = filepath; // httprequest. getsession (). getservletcontext (). getrealpath ("/sealandsignimages"); multiformatwriter = new multiformatwriter (); map hints = new hashmap (); hints. put (encodehinttype. character_set, "UTF-8" ); Bitmatrix = multiformatwriter. encode (content, barcodeformat. qr_code, 200,200, hints); file file1 = new file (path, name + ". jpg "); matrixtoimagewriter. writetofile (bitmatrix, "jpg", file1); retpath = file1.getabsolutepath ();} catch (exception e) {e. printstacktrace ();} return retpath;} public static void analysisqrcode () {try {multiformatreader formatreader = new multiformatreader (); strin G filepath = "F:/users/Administrator/desktop/testimage/test.jpg"; file = new file (filepath); bufferedimage image = ImageIO. read (File); luminancesource source = new bufferedimageluminancesource (image); binarizer = new hybridbinarizer (source); binarybitmap = new binarybitmap (binarizer ); map hints = new hashmap (); hints. put (encodehinttype. character_set, "UTF-8"); Result = Formatreader. decode (binarybitmap, hints); system. out. println ("result =" + result. tostring (); system. out. println ("resultformat =" + result. getbarcodeformat (); system. out. println ("resulttext =" + result. gettext ();} catch (exception e) {e. printstacktrace () ;}} package liupeng; import COM. google. zxing. common. bitmatrix; import javax. imageIO. imageIO; import Java. io. file; import Java. io. outputstream; import ja Va. io. ioexception; import Java. AWT. image. bufferedimage; public final class matrixtoimagewriter {Private Static final int black = 0xff000000; Private Static final int white = 0 xffffffff; private matrixtoimagewriter () {} public static bufferedimage partition (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 


Note: You need to import org. JSON. jar and zxing-core-2.0.jar

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.