最近琢磨了一下二維碼、一維碼的編碼、解碼方法,感覺google的zxing用起來還是比較方便。
本人原創,歡迎轉載,轉載請標注原文地址:http://wallimn.iteye.com/blog/2071020
一、工具類
Java代碼 收藏代碼
package com.exam.services.qrcode;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.awt.image.BufferedImage;
/**
* 使用ZXing2.3,生成條碼的輔助類。可以編碼、解碼。編碼使用code包,解碼需要javase包。
*
* <br/>
* <br/>
* 作者:wallimn<br/>
* 聯系:54871876@qq.com,http://wallimn.iteye.com<br/>
* 時間:2014年5月25日 下午10:33:05<br/>
*/
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() {
}
/**
* 生成矩陣,是一個簡單的函數,參數固定,更多的是使用示范。
*
* <br/>
* <br/>
* 作者:wallimn<br/>
* 時間:2014年5月25日 下午10:41:12<br/>
* 聯系:54871876@qq.com<br/>
*
* @param text
* @return
*/
public static BitMatrix toQRCodeMatrix(String text, Integer width,
Integer height) {
if (width == null || width < 300) {
width = 300;
}
if (height == null || height < 300) {
height = 300;
}
// 二維碼的圖片格式
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
// 內容所使用編碼
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
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;
}
/**
* 將指定的字符串生成二維碼圖片。簡單的使用示例。
*
* <br/>
* <br/>
* 作者:wallimn<br/>
* 時間:2014年5月25日 下午10:44:52<br/>
* 聯系:54871876@qq.com<br/>
*
* @param text
* @param file
* @param format
* @return
*/
public boolean toQrcodeFile(String text, File file, String format) {
BitMatrix matrix = toQRCodeMatrix(text, null, null);
if (matrix != null) {
try {
writeToFile(matrix, format, file);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
/**
* 根據點矩陣生成黑白圖。 作者:wallimn<br/>
* 時間:2014年5月25日 下午10:26:22<br/>
* 聯系:54871876@qq.com<br/>
*/
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;
}
/**
* 將字符串編成一維條碼的矩陣
*
* <br/>
* <br/>
* 作者:wallimn<br/>
* 時間:2014年5月25日 下午10:56:34<br/>
* 聯系:54871876@qq.com<br/>
*
* @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;
}
/**
* 根據矩陣、圖片格式,生成文件。 作者:wallimn<br/>
* 時間:2014年5月25日 下午10:26:43<br/>
* 聯系:54871876@qq.com<br/>
*/
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);
}
}
/**
* 將矩陣寫入到輸出流中。 作者:wallimn<br/>
* 時間:2014年5月25日 下午10:27:58<br/>
* 聯系:54871876@qq.com<br/>
*/
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包。
*
* <br/>
* <br/>
* 作者:wallimn<br/>
* 時間:2014年5月25日 下午11:06:07<br/>
* 聯系:54871876@qq.com<br/>
*
* @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;
}
}
二、使用示例
Java代碼 收藏代碼
package com.exam.services.qrcode;
import java.io.File;
public class Test {
/**
* 測試函數。簡單地將指定的字符串生成二維碼圖片。
*
* <br/><br/>
* 作者:wallimn<br/>
* 時間:2014年5月25日 下午10:30:00<br/>
* 聯系:54871876@qq.com<br/>
*/
public static void main(String[] args) throws Exception {
String text = "http://wallimn.itey.com";
String result;
String format = "gif";
//生成二維碼
File outputFile = new File("d:"+File.separator+"rqcode.gif");
MatrixUtil.writeToFile(MatrixUtil.toQRCodeMatrix(text, null, null), format, outputFile);
result = MatrixUtil.decode(outputFile);
System.out.println(result);
outputFile = new File("d:"+File.separator+"barcode.gif");
MatrixUtil.writeToFile(MatrixUtil.toBarCodeMatrix(text, null, null), format, outputFile);
result = MatrixUtil.decode(outputFile);
System.out.println(result);
}
}
最近琢磨了一下二維碼、一維碼的編碼、解碼方法,感覺google的zxing用起來還是比較方便。
本人原創,歡迎轉載,轉載請標注原文地址:http://wallimn.iteye.com/blog/2071020
一、工具類
- package com.exam.services.qrcode;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.BinaryBitmap;
- import com.google.zxing.DecodeHintType;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.LuminanceSource;
- import com.google.zxing.MultiFormatReader;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.Result;
- import com.google.zxing.WriterException;
- import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.common.HybridBinarizer;
- import javax.imageio.ImageIO;
- import java.io.File;
- import java.io.OutputStream;
- import java.io.IOException;
- import java.util.Hashtable;
- import java.awt.image.BufferedImage;
- /**
- * 使用ZXing2.3,生成條碼的輔助類。可以編碼、解碼。編碼使用code包,解碼需要javase包。
- *
- * <br/>
- * <br/>
- * 作者:wallimn<br/>
- * 聯系:54871876@qq.com,http://wallimn.iteye.com<br/>
- * 時間:2014年5月25日 下午10:33:05<br/>
- */
- 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() {
- }
- /**
- * 生成矩陣,是一個簡單的函數,參數固定,更多的是使用示范。
- *
- * <br/>
- * <br/>
- * 作者:wallimn<br/>
- * 時間:2014年5月25日 下午10:41:12<br/>
- * 聯系:54871876@qq.com<br/>
- *
- * @param text
- * @return
- */
- public static BitMatrix toQRCodeMatrix(String text, Integer width,
- Integer height) {
- if (width == null || width < 300) {
- width = 300;
- }
- if (height == null || height < 300) {
- height = 300;
- }
- // 二維碼的圖片格式
- Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
- // 內容所使用編碼
- hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
- 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;
- }
- /**
- * 將指定的字符串生成二維碼圖片。簡單的使用示例。
- *
- * <br/>
- * <br/>
- * 作者:wallimn<br/>
- * 時間:2014年5月25日 下午10:44:52<br/>
- * 聯系:54871876@qq.com<br/>
- *
- * @param text
- * @param file
- * @param format
- * @return
- */
- public boolean toQrcodeFile(String text, File file, String format) {
- BitMatrix matrix = toQRCodeMatrix(text, null, null);
- if (matrix != null) {
- try {
- writeToFile(matrix, format, file);
- return true;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return false;
- }
- /**
- * 根據點矩陣生成黑白圖。 作者:wallimn<br/>
- * 時間:2014年5月25日 下午10:26:22<br/>
- * 聯系:54871876@qq.com<br/>
- */
- 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;
- }
- /**
- * 將字符串編成一維條碼的矩陣
- *
- * <br/>
- * <br/>
- * 作者:wallimn<br/>
- * 時間:2014年5月25日 下午10:56:34<br/>
- * 聯系:54871876@qq.com<br/>
- *
- * @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;
- }
- /**
- * 根據矩陣、圖片格式,生成文件。 作者:wallimn<br/>
- * 時間:2014年5月25日 下午10:26:43<br/>
- * 聯系:54871876@qq.com<br/>
- */
- 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);
- }
- }
- /**
- * 將矩陣寫入到輸出流中。 作者:wallimn<br/>
- * 時間:2014年5月25日 下午10:27:58<br/>
- * 聯系:54871876@qq.com<br/>
- */
- 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包。
- *
- * <br/>
- * <br/>
- * 作者:wallimn<br/>
- * 時間:2014年5月25日 下午11:06:07<br/>
- * 聯系:54871876@qq.com<br/>
- *
- * @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;
- }
- }
二、使用示例
- package com.exam.services.qrcode;
- import java.io.File;
- public class Test {
- /**
- * 測試函數。簡單地將指定的字符串生成二維碼圖片。
- *
- * <br/><br/>
- * 作者:wallimn<br/>
- * 時間:2014年5月25日 下午10:30:00<br/>
- * 聯系:54871876@qq.com<br/>
- */
- public static void main(String[] args) throws Exception {
- String text = "http://wallimn.itey.com";
- String result;
- String format = "gif";
- //生成二維碼
- File outputFile = new File("d:"+File.separator+"rqcode.gif");
- MatrixUtil.writeToFile(MatrixUtil.toQRCodeMatrix(text, null, null), format, outputFile);
- result = MatrixUtil.decode(outputFile);
- System.out.println(result);
- outputFile = new File("d:"+File.separator+"barcode.gif");
- MatrixUtil.writeToFile(MatrixUtil.toBarCodeMatrix(text, null, null), format, outputFile);
- result = MatrixUtil.decode(outputFile);
- System.out.println(result);
- }
- }

