首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老美开发的,barcode4j对一维条形码处理的很好,而且支持的格式很多,当然也可以对二维码进行处理,效果个人感觉没有前两种好;ZXing对j2me,j2se,
code:
private static final String CHARSET = "utf-8";
// 二维码尺寸
private static final int DEFAULT_QRCODE_SIZE = 300;
// LOGO宽度
private static final int DEFAULT_LOGO_WIDTH = 50;
// LOGO高度
private static final int DEFAULT_LOGO_HEIGHT = 50;
private static final String IMAGE_FORMAT_NAME = "JPG";
/**
* 生成二维码图片
*
* @param content
* 二维码包含内容
* @param QRcodeSize
* 正方形二维码图片的大小(单位:像素)
* @throws Exception
*/
public static BufferedImage createImage(String content, int QRcodeSize) throws Exception {
if(QRcodeSize<=0){
QRcodeSize=DEFAULT_QRCODE_SIZE;
}
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, QRcodeSize, QRcodeSize, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000
: 0xFFFFFFFF);
}
}
return image;
}
/**
* 向二维码图片中插入LOGO
*
* @param source
* 二维码图片对象
* @param QRcodeSize
* 正方形二维码图片的大小(单位:像素)
* @param logoImageFile
* 二维码图片文件对象
* @param logoNeedCompress
* logo图片是否压缩
* @param logoWidth
* logo图片宽度
* @param logoHeight
* logo图片高度
* @throws Exception
*/
public static void insertLogoImage(BufferedImage source, int QRcodeSize, InputStream logoInputStream,
boolean logoNeedCompress,int logoWidth, int logoHeight) throws Exception {
/*if (!logoImageFile.exists()) {
System.err.println("logo文件不存在!");
return;
}*/
Image src = ImageIO.read(logoInputStream);
int width = src.getWidth(null);
int height = src.getHeight(null);
if(logoWidth<=0)logoWidth=DEFAULT_LOGO_WIDTH;
if(logoHeight<=0)logoHeight=DEFAULT_LOGO_HEIGHT;
if (logoNeedCompress) { // 压缩LOGO
if (width > logoWidth) {
width = logoWidth;
}
if (height > logoHeight) {
height = logoHeight;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRcodeSize - width) / 2;
int y = (QRcodeSize - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
/**
* 生成二维码(内嵌LOGO)
*
* @param content
* 内容
* @param imgPath
* LOGO地址
* @param destPath
* 存储地址
* @throws Exception
*/
public static File encode(String content, InputStream logoInputStream,int QRcodeSize)
throws Exception {
File codeFile=null;
if (StringUtil.isBlank(content))return null;
try {
BufferedImage codeImage = QRCodeUtil.createImage(content,QRcodeSize);
if(null!=logoInputStream){
QRCodeUtil.insertLogoImage(codeImage, QRcodeSize, logoInputStream, true, DEFAULT_LOGO_WIDTH, DEFAULT_LOGO_HEIGHT);
}
codeFile = File.createTempFile("QR"+System.currentTimeMillis(),"."+IMAGE_FORMAT_NAME);
ImageIO.write(codeImage, IMAGE_FORMAT_NAME, codeFile);
}catch(Exception e){
System.err.println("生成二维码失败!"+e.getMessage());
}
return codeFile;
}
/**
* 解析二维码
*
* @param file
* 二维码图片
* @return
* @throws Exception
*/
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}