java利用zxing编码解码一维码与二维码

  最近琢磨了一下二维码、一维码的编码、解码方法,感觉google的zxing用起来还是比较方便。
  本人原创,欢迎转载,转载请标注原文地址:http://wallimn.iteye.com/blog/2071020

[b]一、工具类[/b]

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;
}
}


[b]二、使用示例[/b]
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);
}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ZXing一维/二维码 使用文档 样例: System.IO.Stream stmYiWei = new System.IO.MemoryStream(); BitMatrix byteMatrix = new MultiFormatWriter().encode(sCode, BarcodeFormat.CODE_39, 230, 40); toBitmap(byteMatrix).Save(stmYiWei, ImageFormat.Bmp); Byte[] byteYiWei = new byte[stmYiWei.Length]; stmYiWei.Position = 0; stmYiWei.Read(byteYiWei, 0, (int)stmYiWei.Length); //将图片文件流保存为二进制文件以便保存到数据库中 System.IO.Stream stmErWei = new System.IO.MemoryStream(); IDictionary hints = new Dictionary(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); byteMatrix = new MultiFormatWriter().encode(sTmp, BarcodeFormat.QR_CODE, 200, 200, hints); toBitmap(byteMatrix).Save(stmErWei, ImageFormat.Bmp); Byte[] byteErWei = new byte[stmErWei.Length]; stmErWei.Position = 0; stmErWei.Read(byteErWei, 0, (int)stmErWei.Length); //将图片文件流保存为二进制文件以便保存到数据库中 strSQL = "insert into gdzc_biaoqian( bq_gd_no,bq_yiweima,bq_erweima,bq_us_no) values("; strSQL = strSQL + " @bq_gd_no,@bq_yiweima,@bq_erweima,@bq_us_no)"; SqlCommand commandImage = new SqlCommand(strSQL, connectionImage); commandImage.Parameters.Clear(); commandImage.Parameters.Add("@bq_gd_no", SqlDbType.Int).Value = Convert.ToInt32(sGdzcNo); commandImage.Parameters.Add("@bq_yiweima", SqlDbType.Image).Value = byteYiWei; commandImage.Parameters.Add("@bq_erweima", SqlDbType.Image).Value = byteErWei; commandImage.Parameters.Add("@bq_us_no", SqlDbType.Int).Value = Convert.ToInt32(Session["LoginUserID"]); commandImage.ExecuteNonQuery(); commandImage.Dispose();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值