zxing实现带logo的二维码图片生成功能实现

爪哇屌丝 爪哇屌丝 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public class CreateQRCodeImg {

private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
public static BufferedImage toBufferedImage(BitMatrix matrix){
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);
}
}
return image;
}

public static void writeToFile(BitMatrix matrix,String format,File file,String logoPath) throws IOException {
BufferedImage image = toBufferedImage(matrix);
Graphics2D gs = image.createGraphics();

//载入logo

Image img = ImageIO.read(new File(logoPath));
gs.drawImage(img, 125, 125, null);
gs.dispose();
img.flush();
if(!ImageIO.write(image, format, file)){
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}


/**
* 最终调用该方法生成二维码图片
* @param url
* @param imgPath 二维码生成的绝对路径
* @param logoPath 二维码中间logo绝对地址

* @throws Exception
*/
public static void get2CodeImage(String url,String imgPath,String logoPath) throws Exception{
String format = "png";
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 300, 300, hints);
File outputFile = new File(imgPath);
CreateQRCodeImg.writeToFile(bitMatrix, format, outputFile,logoPath);
}

}

PS:zxing相关jar可到网上搜寻,或者在本人资源分享里面下载!
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
爪哇屌丝
Java 51,407 成员 86,128 内容