java-Two-dimensional code to write zxing

Source: Internet
Author: User
Tags string format

Zxing This is Google's

Http://code.google.com/p/zxing/downloads/list

Two-dimensional code source case

 Packagecom.utils;ImportJava.awt.image.BufferedImage;ImportJava.awt.image.DataBufferByte;ImportJava.io.File;Importjava.io.IOException;Importjava.util.Date;Importjava.util.Hashtable;ImportJavax.imageio.ImageIO;ImportCom.google.zxing.BarcodeFormat;ImportCom.google.zxing.EncodeHintType;ImportCom.google.zxing.MultiFormatWriter;ImportCom.google.zxing.common.BitMatrix; Public classUtilsqrcode {Private Static Final intBLACK = 0xff000000; Private Static Final intWhite = 0xFFFFFFFF; /**     * @paramargs*/     Public Static voidMain (string[] args) {File file=NewFile ("c:\\users\\huage\\desktop\\ picture \ \" +NewDate (). GetTime () + ". png"); Encode ("Http://www.baidu.com", File, Barcodeformat.qr_code, 200, 200); }        /**     * @paramContents *@paramfile *@paramformat *@paramWidth *@paramHeight*/     Public Static  voidEncode (String contents, file file, Barcodeformat format,intWidthintheight) {        Try{Contents=NewString (Contents.getbytes ("UTF-8"), "Iso-8859-1"); Hashtable<encodehinttype, object> hints =NewHashtable<encodehinttype, object>(); Hints.put (Encodehinttype.character_set,"Utf-8"); Hints.put (Encodehinttype.margin,1); Bitmatrix Bitmatrix=NewMultiformatwriter (). Encode (contents, format, width, height,hints); BufferedImage Image=tobufferedimage (Bitmatrix); WriteToFile (Image,"PNG", file); } Catch(Exception e) {e.printstacktrace (); }    }             Public Static byte[] imgtobytes (bufferedimage image) {if(Image = =NULL)return New byte[0]; byte[] data =((DataBufferByte) Image.getdata (). Getdatabuffer ()). GetData (); returndata; }         Public Static voidWriteToFile (bufferedimage image, String format, file file)throwsIOException {if(Image = =NULL|| File = =NULL)return;    Imageio.write (image, format, file); }         Public Staticbufferedimage tobufferedimage (Bitmatrix matrix) {intwidth =matrix.getwidth (); intHeight =matrix.getheight (); BufferedImage Image=Newbufferedimage (width, height, bufferedimage.type_int_argb);  for(intx = 0; x < width; X + +) {             for(inty = 0; Y < height; y++) {Image.setrgb (x, Y, Matrix.get (x, y)==true?black:white); }        }        returnimage; }}

java-Two-dimensional code to write zxing

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.