利用zxing制作彩色,高容错,支持中文等UTF编码的QR二维码图片

利用zxing制作彩色,高容错,支持中文等UTF编码的QR二维码图片。代码如下

import java.awt.Color;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;


public class test {

 public static void main(String[] args) {
  try{
           String contents = "http://www.jsst.cn";
           String path = "e:\\test.png";
           Map hints = new HashMap();
           hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
           hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
           MultiFormatWriter barcodeWriter = new MultiFormatWriter();
           BitMatrix matrix = barcodeWriter.encode(contents, BarcodeFormat.QR_CODE, 300, 300, hints);
           
           File file = new File(path);
           Color yellow =  new Color(255, 255, 0);
           Color white =  new Color(255, 255, 255);
           MatrixToImageWriter.writeToFile(matrix, "png", file, new MatrixToImageConfig(yellow.getRGB(), white.getRGB()));
       } catch (Exception e) {
           e.printStackTrace();
       }
 }
}

 

 

posted @ 2013-07-19 21:29  明将军  Views(920)  Comments(0Edit  收藏  举报
恨怨悲苦憎怒嗔、仁爱慈孝耻义廉。是故恨人所以得仁,无爱者必不怨,不慈者必无悲,孝而有苦,憎后耻来,义自怒生,廉人心嗔。夹天地七大苦,破人情七大碍,遂舍善恶之心,得称剑神。