ZXing 二维码 Demo

ZXing 是Google上的一个开源项目,功能强大, 可以支持各种条形码,二维码的编码和解码。
点这里可以了解原始项目情况。
[url]http://code.google.com/p/zxing/[/url]
[url]https://github.com/zxing/zxing[/url]
网上有很多关于ZXing 二维码使用的Demo.

不才仔细查看了Zxing的源代码,感觉有些类的封装和部分接口的设计不是特别适合我个人的口味。
1. 二维码的属性参数用Map的形式传入,初学者要仔细阅读文档后才能知道应该在Map里面放些什么东西,不那么直观。
2. MultiFormatWriter的encode 方法输入参数里面包含了输出图像的高和宽,本人私下认为没有这个必要。原因,一是二维码的行和列编码后已经明确了,例如一个50乘以50的二维码,如果用户输入一个49像素,或者75像素意义不大,49像素太小,75像素不是50的整数倍,这导致每个细胞的像素是1.5,毕竟没有半个像素这个说法,所以这样设计接口增加了内部逻辑的复杂度。
3.考虑到实际应用,输入就是一个字符串,输出一般就是个二维码,或者图片。所以接口最好也设计成这个样子,比较贴近生活。
4.本人注意到QRCode类的内部表示是一个byte型的二维数组,byte的取值范围大0~255,本人愚钝,本来还以为这里面存了什么复杂的数据, 但是实际上里面存储的只有0和1, 既然如此,不如改成 qrcode.jar 那个项目的风格,用boolean二维数组更加直接。

以下是本人重新封装的代码, 班门弄斧,让大家见笑。
[b]Encoder.java[/b]

package home.ibm.tony.zxing.qrcode;

import java.awt.image.BufferedImage;

import com.google.zxing.WriterException;
import com.google.zxing.qrcode.encoder.QRCode;

public interface Encoder {

public BufferedImage encode2Image(String content)
throws WriterException;


public BufferedImage encode2Image(String content, QRCodeProperties properties)
throws WriterException;


public QRCode encode(String content)
throws WriterException;


public QRCode encode(String content, QRCodeProperties properties)
throws WriterException;
}

[b]QRCodeEncoder.java[/b]
package home.ibm.tony.zxing.qrcode;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import com.google.zxing.WriterException;
import com.google.zxing.qrcode.encoder.QRCode;

public class QRCodeEncoder implements Encoder {

QRCodeProperties defaultProperties = new QRCodeProperties();

@Override
public BufferedImage encode2Image(String content) throws WriterException {

return convert(encode(content), defaultProperties);
}

@Override
public BufferedImage encode2Image(String content,
QRCodeProperties properties) throws WriterException {

return convert(encode(content), properties);
}

@Override
public QRCode encode(String content) throws WriterException {

return encode(content, defaultProperties);
}

@Override
public QRCode encode(String content, QRCodeProperties properties)
throws WriterException {

return com.google.zxing.qrcode.encoder.Encoder.encode(content,
properties.getErrorCorrection());

}


public static BufferedImage convert(QRCode qrcode, QRCodeProperties properties ){

int pixelSize = properties.getCellSize();
int margin = properties.getMargin();

boolean[][] result = toArray(qrcode);


int imageSize = result.length * pixelSize + margin * 2;

BufferedImage image = new BufferedImage(imageSize, imageSize,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setBackground(properties.getBgColor());
g.clearRect(0, 0, imageSize, imageSize);
g.setColor(properties.getFgColor());

for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result.length; j++) {
if (result[j][i]) {
g.fillRect(j * pixelSize + margin, i * pixelSize + margin,
pixelSize, pixelSize);
}
}
}

g.dispose();
image.flush();

return image;
}

public static boolean[][] toArray(QRCode qrcode) {
byte[][] array = qrcode.getMatrix().getArray();
boolean[][] result = new boolean[array.length][array[0].length];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
result[i][j]= array[j][i] > 0 ? true : false;
}
System.out.println();
}

return result;
}
}


[b]QRCodeProperties.java[/b]
package home.ibm.tony.zxing.qrcode;

import java.awt.Color;

import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCodeProperties {

private Color bgColor = Color.WHITE;

private int cellSize = 3;

private ErrorCorrectionLevel errorCorrection = ErrorCorrectionLevel.M;

private Color fgColor = Color.BLACK;

private int margin = 2;

private int version = 6;

public Color getBgColor() {
return bgColor;
}

public int getCellSize() {
return cellSize;
}

public ErrorCorrectionLevel getErrorCorrection() {
return errorCorrection;
}

public Color getFgColor() {
return fgColor;
}

public int getMargin() {
return margin;
}

public int getVersion() {
return version;
}

public void setBgColor(Color bgColor) {
this.bgColor = bgColor;
}

public void setCellSize(int cellSize) {
this.cellSize = cellSize;
}

public void setErrorCorrection(ErrorCorrectionLevel errorCorrection) {
this.errorCorrection = errorCorrection;
}

public void setFgColor(Color fgColor) {
this.fgColor = fgColor;
}

public void setMargin(int margin) {
this.margin = margin;
}

public void setVersion(int version) {
this.version = version;
}



}


[b]QRCodeDemo.java[/b]
package home.ibm.tony.zxing.qrcode;


import home.ibm.tony.qrcode.EnhancedQRCodeDecoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.google.zxing.WriterException;

public class QRCodeDemo {

public static void main(String[] args) throws IOException, WriterException {
String imgPath = "c:/z.png";
String encoderContent = "http://ieqq.iteye.com/blog/2114077";

QRCodeEncoder encoder = new QRCodeEncoder();

ImageIO.write(encoder.encode2Image (encoderContent), "png", new File(imgPath));

System.out.println("========encoder success");

}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值