OpenJWeb平台提供了免费的二维码图片生成的在线工具,可通过下面的地址访问:
http://www.tuozhong.com/portal/apps/b2c/pcenter/qrcode.jsp
下面是页面效果:
二维码代码生成程序(支持反向解析):
package com.openjweb.erp.barcode;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Reader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.encoder.ByteMatrix;
//http://hi.baidu.com/baid/blog/item/fd446e06d199a77702088102.html
//http://www.4ucode.com/Study/Topic/1162485
//http://www.baishaode.com/blog/?p=436
public class QRCodeGen
{
/**
* 生成二维码图片
* @param code 待生成图片的二维码
* @param destFile 生成二维码图片存储的文件
*/
public static void createQRCodePic(String code,String destFile)
{
BitMatrix byteMatrix;
try
{
byteMatrix = new MultiFormatWriter()
.encode(code,BarcodeFormat.QR_CODE, 200, 200);
File file = new File(destFile);
MatrixToImageWriter.writeToFile(byteMatrix, destFile.substring(destFile.lastIndexOf(".")+1), file);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (WriterException e1)
{
e1.printStackTrace();
}
}
public static void createBarCodePic(String code,String codeType,String destFile)
{
BitMatrix byteMatrix;
try
{
BarcodeFormat barcodeFormat =BarcodeFormat.QR_CODE; //默认
if(codeType!=null&&codeType.trim().length()>0)
{
if(codeType.equals("QR_CODE"))barcodeFormat=BarcodeFormat.QR_CODE;
if(codeType.equals("CODABAR"))barcodeFormat=BarcodeFormat.CODABAR; //这个不支持
if(codeType.equals("CODE_128"))barcodeFormat=BarcodeFormat.CODE_128;
if(codeType.equals("CODE_39"))barcodeFormat=BarcodeFormat.CODE_39;
if(codeType.equals("CODE_93"))barcodeFormat=BarcodeFormat.CODE_93;
if(codeType.equals("ITF"))barcodeFormat=BarcodeFormat.ITF;
if(codeType.equals("EAN_13"))barcodeFormat=BarcodeFormat.EAN_13;
//如果有更多的格式请在下面添加,上面只有QR_CODE为二维码
}
byteMatrix = new MultiFormatWriter()
.encode(code,barcodeFormat, 200, 200);
File file = new File(destFile);
MatrixToImageWriter.writeToFile(byteMatrix, destFile.substring(destFile.lastIndexOf(".")+1), file);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (WriterException e1)
{
e1.printStackTrace();
}
}
/**
* 反向解析二维码
* @param picFile
* @return
*/
public static String decodeQRCode(String imgPath,String charset)
{
String code = "";
try
{
Reader reader = new MultiFormatReader();
File file = new File(imgPath);
BufferedImage image;
try
{
image = ImageIO.read(file);
if (image == null)
{
System.out.println("Could not decode image");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints= new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, charset); //utf-8
//解码设置编码方式为:utf-8,
result = new MultiFormatReader().decode(bitmap,hints);
String resultStr = result.getText();
code = resultStr;
System.out.println("解析后内容:"+resultStr);
}
catch (IOException ioe)
{
System.out.println(ioe.toString());
}
catch (ReaderException re)
{
System.out.println(re.toString());
}
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
return code;
}
public static void main(String[] args) {
String str = "test";//二维码内容
String path = "d:/test";
BitMatrix byteMatrix;
try
{
byteMatrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 200, 200);
File file = new File(path + ".png");
MatrixToImageWriter.writeToFile(byteMatrix, "png", file);
} catch (IOException e) {
e.printStackTrace();
} catch (WriterException e1) {
e1.printStackTrace();
}
}
}