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

爪哇屌丝 2014-06-23 01:09:55
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可到网上搜寻,或者在本人资源分享里面下载!
...全文
853 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
业余草 2014-06-26
  • 打赏
  • 举报
回复
http://download.csdn.net/download/xmt1139057136/6944383 这个事我写的例子,你看看吧, 如果还有不懂,请加qq群:135430763共同学习!!!
爪哇屌丝 2014-06-23
  • 打赏
  • 举报
回复
自己先做沙发,zxing的jar包分享地址http://download.csdn.net/detail/wendysunday123/7538779

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧