1.利用swetakeQrcode生成并解析二维码
这个是日本人写的,生成的是我们常见的方形的二维码
首先导入相应的jar包,QRCode包,创建和读取的代码如下
public String SwetakeQRCode(String content,int size) {
// TODO Auto-generated method stub
//定义二维码图片名称
String qrName="QRcode_Local_"+System.currentTimeMillis()/1000+".jpg";
//Qrcode是swetake的封装对象
Qrcode testQrcode = new Qrcode();
// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
// 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
testQrcode.setQrcodeVersion(size);
// 图片尺寸
int imgSize =67+12*(size-1);
// 获得内容的字节数组,设置编码格式
try {
byte[] contentBytes = content.getBytes("utf-8");
BufferedImage qrImg= new BufferedImage(imgSize, imgSize,BufferedImage.TYPE_INT_RGB);
Graphics2D gs = qrImg.createGraphics();
// 设置背景颜色
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize,imgSize);
// 设定图像颜色> BLACK
gs.setColor(Color.BLACK);
// 设置偏移量,不设置可能导致解析出错
int pixoff =2;
// 二维码存储容量限制(0--800)
if (contentBytes.length > 0 && contentBytes.length < 800) {
boolean[][] codeOut =testQrcode.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j *3+ pixoff, i * 3+ pixoff, 3, 3);
}
}
}
}else {
throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");
}
gs.dispose();
qrImg.flush();
//存储图片
//String LocalUrl=ParamManagerImpl.get(SysParam.SYS_WEIXIN_QRCODE_PATH)+qrName;
String LocalUrl="C:/111/"+qrName;
File f = new File(LocalUrl);
if (!f.exists()){
f.createNewFile();
ImageIO.write(qrImg, "jpg", f);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ParamManagerImpl.get(SysParam.SYS_WEIXIN_QRCODE_BUCKET)+qrName;
}
@Override
public String DecoderLocalQRCode(String imgPath) {
// TODO Auto-generated method stub
// QRCode 二维码图片的文件
File imageFile = new File(imgPath);
BufferedImage bufImg = null;
String content = null;
try {
bufImg = ImageIO.read(imageFile);
QRCodeDecoder decoder = new QRCodeDecoder();
//读取本地二维码内容
content = new String(decoder.decode(new LocalQRCodeImage(bufImg)), "utf-8");
HttpPost post = new HttpPost(content);
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
dfe.printStackTrace();
}
return content;
}
package com.fz.apps.weixin.model;
import java.awt.image.BufferedImage;
import jp.sourceforge.qrcode.data.QRCodeImage;
public class LocalQRCodeImage implements QRCodeImage {
BufferedImage bufImg;
public LocalQRCodeImage(BufferedImage bufImg) {
this.bufImg = bufImg;
}
@Override
public int getHeight() {
return bufImg.getHeight();
}
@Override
public int getPixel(int x, int y) {
return bufImg.getRGB(x, y);
}
@Override
public int getWidth() {
return bufImg.getWidth();
}
}
Zxing是Google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用Java利用Zxing生成与解析二维码
导入Zxing包,创建和读取的代码如下:
public String SwetakeQRCode(String content,int size) {
int width = 100;
int height = 100;
// 二维码的图片格式
String format = "jpg";
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("C:/111/" + "new.jpg");
ZxingUtil.writeToFile(bitMatrix, format, outputFile);
//字符流输出
ZxingUtil.writeToStream(bitMatrix, format, System.out);
}
public String DecoderQRCode(String imgPath) {
try {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File(imgPath);
BufferedImage image = ImageIO.read(file);;
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
Result result = formatReader.decode(binaryBitmap,hints);
System.out.println("result = "+ result.toString());
System.out.println("resultFormat = "+ result.getBarcodeFormat());
System.out.println("resultText = "+ result.getText());
} catch (Exception e) {
e.printStackTrace();
}
}