生成带logo的二维码

生成QR二维码的jar包常见的有日本的QRCode.jar和google的zxing.jar,两种方式我都试过,觉得还是google的更胜一筹。
生成二维码个人认为麻烦的地方是中间带logo,因为logo的大小与整体二维码的比例需要在一定范围内,不然不会被识别。二维码允许存在logo是利用了二维码的容错功能。QR二维码容错级别分为四个层次:L,M,Q,H(从低到高排序)。
代码如下:

package com.util;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
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.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;


public class QRCodeHelper {

private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;

private static HashMap<Integer,ErrorCorrectionLevel> levelMap=new HashMap<Integer,ErrorCorrectionLevel>(){
{
put(1,ErrorCorrectionLevel.L);
put(2,ErrorCorrectionLevel.M);
put(3,ErrorCorrectionLevel.Q);
put(4,ErrorCorrectionLevel.H);
}
};

public QRCodeHelper(){}

private static BufferedImage toBufferedImage(BitMatrix matrix,String logoPath) {
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);
}
}
try {
if(logoPath!=null&&(logoPath.length()>0)){
insertImage(image, logoPath, true);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}


private static void writeToFile(BitMatrix matrix, String format, File file,String logoPath)
throws IOException {
BufferedImage image = toBufferedImage(matrix,logoPath);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}


private static void writeToStream(BitMatrix matrix, String format, OutputStream stream,String logoPath)
throws IOException {
BufferedImage image = toBufferedImage(matrix,logoPath);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}


/**
* 插入LOGO
*
* @param source
* 二维码图片
* @param imgPath
* LOGO图片地址
* @param needCompress
* 是否压缩
* @throws Exception
*/
private static void insertImage(BufferedImage source, String logoPath,
boolean needCompress) throws Exception {
File file = new File(logoPath);
if (!file.exists()) {
System.err.println(""+logoPath+" 该文件不存在!");
return;
}
Image src = ImageIO.read(new File(logoPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
int grWidth=source.getWidth();
int grHeigth=source.getHeight();
int loginWidth=grWidth/6;
int loginHeight=grHeigth/6;
// 压缩LOGO
if (needCompress) {
if (width > loginWidth) {
width = loginWidth;
}
if (height > loginHeight) {
height = loginHeight;
}
//得到一个按照指定宽度和高度缩放以后的Image实例
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 = (grWidth - width) / 2;
int y = (grHeigth - 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();
}



/**
* 生产二维码主方法
* @param content 二维码的内容
* @param width 宽度
* @param height 高度
* @param qrPath 生产的二维码图片路径
* @param logoPath 要添加的logo图片,不需要的话为null
* @param level 校验级别 1-4
* @throws Exception
*/
public void createQRCode(String content,int width,int height,String qrPath,String logoPath,int level) throws Exception{
int index=qrPath.lastIndexOf(".");
//二维码的图片格式
String format = qrPath.substring(index+1);
Hashtable hints = new Hashtable();
//内容所使用编码
//设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
hints.put(EncodeHintType.ERROR_CORRECTION, levelMap.get(Integer.valueOf(level)));
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(
content,
BarcodeFormat.QR_CODE,
width,
height,
hints
);
//生成二维码
File outputFile = new File(qrPath);
writeToFile(bitMatrix, format, outputFile,logoPath);
}

public Result analyseQRCode(String qrPath) throws Exception{
File file = new File(qrPath);
System.out.println(file.getName());
BufferedImage bufferedImage = null;
bufferedImage = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable hints = new Hashtable();
//hints.put(EncodeHintType.ERROR_CORRECTION, levelMap.get(Integer.valueOf(level)));
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
Result result = null;
result = new MultiFormatReader().decode(bitmap, hints);
return result;
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值