/**
* 使用ZXing,生成条码的辅助类。可以编码、解码。编码使用code包,解码需要javase包。
*/
public final class MatrixUtil {
private static final String CHARSET = "utf-8";
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
/**
* 禁止生成实例,生成实例也没有意义。
*/
private MatrixUtil() {
}
/**
* 生成矩阵,是一个简单的函数,参数固定,更多的是使用示范
*
* @param text
* @return
*/
public static BitMatrix toQRCodeMatrix(String text, Integer width,
Integer height, String eclevel) {
if (width == null || width > 300) {
width = 300;
}
if (height == null || height > 300) {
height = 300;
}
// 二维码的图片格式
// Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, CHARSET); //内容所使用编码 使用小写的编码,大写会出现]Q2\000026开头内容
hints.put(EncodeHintType.MARGIN, 0); ////margin 边框设置
hints.put(EncodeHintType.MAX_SIZE, 773);
hints.put(EncodeHintType.MIN_SIZE, 310);
//ErrorCorrectionLevel.H 容错率:容错率越高,二维码的有效像素点就越多.
eclevel = eclevel == null ? "" : eclevel.toUpperCase();
if (eclevel.equals("M")) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
}else if (eclevel.equals("L")) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
}else if (eclevel.equals("Q")) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
}else if (eclevel.equals("H")) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
}else{
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
}
BitMatrix bitMatrix = null;
try {
bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 生成二维码
// File outputFile = new File("d:"+File.separator+"new.gif");
// MatrixUtil.writeToFile(bitMatrix, format, outputFile);
return bitMatrix;
}
/**
* 将指定的字符串生成二维码图片。简单的使用示例
*
* @param text
* @param file
* @param format
* @return
*/
public boolean toQrcodeFile(String text, File file, String format) {
BitMatrix matrix = toQRCodeMatrix(text, null, null, null);
if (matrix != null) {
try {
writeToFile(matrix, format, file);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
/**
* 根据点矩阵生成黑白图
*/
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;
}
/**
* 将字符串编成一维条码的矩阵
*
* @param str
* @param width
* @param height
* @return
*/
public static BitMatrix toBarCodeMatrix(String str, Integer width,
Integer height) {
if (width == null || width < 200) {
width = 200;
}
if (height == null || height < 50) {
height = 50;
}
try {
// 文字编码
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
BitMatrix bitMatrix = new MultiFormatWriter().encode(str,
BarcodeFormat.CODE_128, width, height, hints);
return bitMatrix;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 根据矩阵、图片格式,生成文件
*/
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);
}
}
/**
* 解码,需要javase包。
*
* @param file
* @return
*/
public static String decode(File file) {
BufferedImage image;
try {
if (file == null || file.exists() == false) {
throw new Exception(" File not found:" + file.getPath());
}
image = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
// 解码设置编码方式为:utf-8,
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 测试函数。简单地将指定的字符串生成二维码图片
*/
public static void main(String[] args) throws Exception {
String text = "http://g.click.taobao.com/q?pid=mm_14153868_4088432_13314443&rd=2&ct=url%3Dhttp%253A%252F%252Fwww.taobao.com&rf=http%3A%2F%2Fwww.showyounger.com%2Fwebsite&et=30886910&pgid=0792b8cdb6c3121791fe266d1e44fda9&v=1.1";
String result;
String format = "gif";
// 生成二维码
File outputFile = new File("d:/test/qrcode.gif");
MatrixUtil.writeToFile(MatrixUtil.toQRCodeMatrix(text, 256, 256, "L"),
format, outputFile);
result = MatrixUtil.decode(outputFile);
System.out.println(result);
// outputFile = new File("d:/test/barcode.gif");
// MatrixUtil.writeToFile(MatrixUtil.toBarCodeMatrix(text, null, null),
// format, outputFile);
//
// result = MatrixUtil.decode(outputFile);
// System.out.println(result);
}
}