您的位置:首页 > 其它

采用zxing生成二维码

2014-04-09 10:11 232 查看
package bctts.qrcode.zxing;

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 final class MatrixToImageWriter {

private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;

private MatrixToImageWriter() {}

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; x++) {
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 = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an 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 bctts.qrcode.zxing;

import java.awt.image.BufferedImage;
import java.util.Hashtable;
import java.util.ResourceBundle;

import net.sf.json.JSONArray;

import bctts.qrcode.Encrypt;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.qrcode.EncodeHintType;
import com.itextpdf.text.pdf.qrcode.ErrorCorrectionLevel;

/**
* 生成二维码
*
* @author a
*
*/
public class QRCodeGeneration {

/**
* 生成二维码图片
*
* @param toEncryptStr
*            要加密的信息
* @return Image
* @throws Exception
*/
public static Image generateQR(JSONArray toEncryptStr) throws Exception {
// 设置QR二维码参数
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 矫错级别
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
/////////////////////////////从配置文件读取信息
String ReferencesEncryptionClass =  "bctts.qrcode.Encrypt" ;//默认加密类
ResourceBundle rb=ResourceBundle.getBundle("bctts.qrcode.configQrInfo");//注意路径是以.表示的
if(rb != null ){
ReferencesEncryptionClass = rb.getString("ReferencesEncryptionClass");
}
////////////////////////////////////////////////////
/*Encrypt encoder = new Encrypt();
String mi = encoder.encrypt(toEncryptStr.toString());// 加密
*/
//利用反射机制执行加密方法
Class clazz= Class.forName(ReferencesEncryptionClass);
Object o = clazz.newInstance();
//获取密文信息
String mi   = (String)clazz.getDeclaredMethod("encrypt",String.class).invoke(o,new Object[]{toEncryptStr.toString()});
/////////////////////////////////////////////
int width = 250;
int height = 250;
BitMatrix bitMatrix = new MultiFormatWriter().encode(
new String(mi.getBytes("GBK"), "iso-8859-1"),
BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
Image img = Image.getInstance(image, null, false);
return img;

}

}


package bctts.qrcode.zxing;

import java.io.File;
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;

public class Test {

/**
* @param args
*/
public static void main(String[] args) throws Exception {

String text = "此处输入二维码显示的数据";

int width = 200;
int height = 200;
//二维码的图片格式
String format = "gif";
Hashtable hints = new Hashtable();
//内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, width, height, hints);
//生成二维码
File outputFile = new File("d:"+File.separator+"new200200.gif");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
System.out.println("success");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: