QR code--google QR code use (Java version)

Source: Internet
Author: User
Tags string format

Download the jar Package

http://mvnrepository.com/artifact/com.google.zxing/core/2.2


Code:

Package Com.haier.util.qr;import Java.awt.image.bufferedimage;import Java.io.file;import java.io.IOException; Import Java.io.outputstream;import Javax.imageio.imageio;import Com.google.zxing.common.bitmatrix;public class Matrixtoimagewriter {private static final int BLACK = 0xff000000;private static final int white = 0xffffffff;private Matri Xtoimagewriter () {}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; + +) {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 = Tobuff Eredimage (matrix); if (! Imageio.write (image, format, file)) {throw new IOException ("Could not write a 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);}}

Package Com.haier.util.qr;import Java.io.file;import Java.io.outputstream;import java.util.hashtable;import Com.google.zxing.barcodeformat;import Com.google.zxing.encodehinttype;import Com.google.zxing.MultiFormatWriter; Import Com.google.zxing.common.bitmatrix;import Com.test.matrixtoimagewriter;public class QRUtil {public static void Create (String text, OutputStream stream, Integer size) throws Exception {int width = + (ten * size); int height = 100 + ( size);//The picture format of the two-dimensional code string formatted = "JPG"; Hashtable hints = new Hashtable ();//The encoding used for the content hints.put (Encodehinttype.character_set, "utf-8"); Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (Text,barcodeformat.qr_code, width, height, hints);//1.1 Go to white edge int[] rec = Bitmatrix.getenclosingrectangle (); int reswidth = rec[2] + 1;int resheight = rec[3] + 1;  Bitmatrix Resmatrix = new Bitmatrix (reswidth, resheight); Resmatrix.clear (); for (int i = 0; i < reswidth; i++) {for (int j = 0; J < Resheight; J + +) {if (Bitmatrix.get (i + rec[0], j + rec[1]) {Resmatrix.set (i, j);}}} Matrixtoimagewriter.writetostream (Resmatrix, format, stream);} public static void Main (string[] args) throws Exception {String text = "http://www.baidu.com/action/page/formSub.html? mac=1111111111111111111111111111111111111111111111111111111 "; int width = 300;int height = 300;//Two-dimensional code picture format string = "JPG"; Hashtable hints = new Hashtable ();//The encoding used for the content hints.put (Encodehinttype.character_set, "utf-8"); Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (Text,barcodeformat.qr_code, width, height, hints);//Generate two-dimensional code file OutputFile = new File ("D:" + File.separator + "new.jpg"); Matrixtoimagewriter.writetofile (Bitmatrix, format, outputFile);}}


QR code--google QR code use (Java version)

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.