用到是google的zxing
jar包maven仓库地址
com.google.zxing
core
3.3.3
废话不多说,直接上代码吧
/**
* 生成二维码工具类
*/
public class QRCodeUtil {
/**
* @Title: generalQRCode
* @Description: 生成二维码并使用Base64编码
* @param url 要生成的二维码内容
* @param logoPath logo所在的网络地址
* @return String 返回的base64格式的图片字符串(png格式)
* @throws
*/
public static String generalQRCode(String url,String logoPath) {
Hashtable hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, Constant.QRCODE_MARGIN); //设置白边
String binary = null;
try {
//生成二维码矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
ByteArrayOutputStream out = new ByteArrayOutputStream();
//二维码中画入logo
BufferedImage image = writeLogoToQrcode(bitMatrix,logoPath);
//文件转换为字节数组
ImageIO.write(image, "png", out);
byte[] bytes = out.toByteArray();
//进行base64编码
binary = BASE64Encoder.encode(bytes).trim();
} catch (Exception e) {
e.printStackTrace();
}
//返回png格式的base64编码数据 (如果需要其他的,请自行处理)
return "data:image/png;base64,"+binary;
}
/**
*
* @Title: toBufferedImage
* @Description: 二维码矩阵转换为BufferedImage
* @param matrix
* @return BufferedImage 返回类型
* @throws
*/
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++) {
//Constant.QRCODE_COLOR和Constant.QRCODE_BACKGROUND_COLOR为二维码颜色和背景颜色
image.setRGB(x, y, matrix.get(x, y) ? Constant.QRCODE_COLOR : Constant.QRCODE_BACKGROUND_COLOR);
}
}
return image;
}
/**
*
* @param matrix 二维码矩阵相关
* @param logoPath logo路径
* @throws IOException
*/
public static BufferedImage writeLogoToQrcode(BitMatrix matrix,String logoUrl) throws IOException {
//二维码矩阵转换为BufferedImage
BufferedImage image = toBufferedImage(matrix);
//是否传入了logo地址
if(StringUtils.isNotBlank(logoUrl)){
URL url = new URL(logoUrl);
//取得二维码图片的画笔
Graphics2D gs = image.createGraphics();
int ratioWidth = image.getWidth()*2/10;
int ratioHeight = image.getHeight()*2/10;
//读取logo地址
Image img = ImageIO.read(url);
int logoWidth = img.getWidth(null)>ratioWidth?ratioWidth:img.getWidth(null);
int logoHeight = img.getHeight(null)>ratioHeight?ratioHeight:img.getHeight(null);
//设置logo图片的位置
int x = (image.getWidth() - logoWidth) / 2;
int y = (image.getHeight() - logoHeight) / 2;
//开画
//gs.drawImage(Image logo, int logo横坐标, int logo纵坐标, int logo宽, int logo高, null);
gs.drawImage(img, (int)(x), (int)(y), logoWidth, logoHeight, null);
gs.dispose();
img.flush();
}
return image;
}
}
通用配置常量类
/**
*
* @ClassName: BMemRewardConstant
* @Description: 生成base64二维码通用常量类
* @author wujun
* @date 2018年8月10日 上午8:48:15
*
*/
public class Constant implements Serializable{
/**
* 推荐二维码颜色 0xFF 后的六位为16进制的颜色值#A72325
*/
public static final int QRCODE_COLOR = 0xFFA72325;
/**
* 推荐二维码背景颜色
*/
public static final int QRCODE_BACKGROUND_COLOR = 0xFFFFFFFF;
/**
* 推荐二维码的白边设置(0-4) 0为无白边
*/
public static final int QRCODE_MARGIN = 0;
}
到这里生成二维码就结束了
测试
public class Test {
@Test
public void test(){
String qrcodeWithLogo = QRCodeUtil.generalQRCode("http://www.baidu.com?id=M000001&name=nickname", "http://chuantu.biz/t6/355/1533885835x-1566688556.png");
String qrcodeWithoutLogo = QRCodeUtil.generalQRCode("http://www.baidu.com?id=M000001&name=nickname", null);
System.out.println("带有logo的二维码:" + qrcodeWithLogo);
System.out.println("不带logo的二维码:" + qrcodeWithoutLogo);
}
}
base64编码串
带有logo的二维码:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAIAAAAP3aGbAAASeklEQVR42u3dC5BVdR0HcAp5y0MzNUtFhBWUp4mipqgIqKjZNE1IUzapoeC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PtvLr58wJxx6b/nr44WHq1PDcc+n53r3DwIHhhBOcEgILWMBqidEaGtL28svp8UknbQKosTE9/9WvhoceCtMnhxlXhSeeCDfeGMaPr2zf+EYYNy5t112XXn/GGeG449J/P+KINEJ88gOw4oN77wUWsFoArGrew/qZ8h2ZzcGKsmwL1tVXV46kLr00neXFByNGpNfErXxI9d3vptdff316MHRoOOuscNllYdKk9Ocxx4TTTksP4pHaDnr/3i0iamj1AgtYHxusaM3556fn586tgLV2bTjllHSiF88Zy2C9+Wb40Y/Syz50i4rNng0sYAELWNsfrJgLLkjPL1wYBgxIDw45JMyYEQ4+OJx7bvrr4MFh0aJk2QdbPD38YIuvGTIknHcesIAFLGC1EFgvvRQefTRdhdC3b7joovD3v6cLEQ48sAJWPESKVL3wQvqWXxQqPhkBigdcl1yS/nr00eGddyrYbbv16pWudaiFxgMWsIBVI2BFfeKBUkTnllvSO+vxmYhUPIyKNr34YnInHivFlE8Jn3wyHHRQen9q6dL012nT0j9de2246aawZk16q+vii9N7WGedFe6/P3278MILw8qVwAIWsIDVQmBFU5YtCxMmpCOs6M6CBenJW28N3/lOmDcvqTRz5iaw4lleBG7QoATTvvumqyKWLAk9e4b99gvDh6fDrsmT0xvtUa6vfz09ecABlSM1YAELWMD6WInnemWzttpOPjlMmRKefrryBlYkKSYeNJVP9PbZJ73pHj266670/DPPhPvuS2+EXXFFej7+9+hUPEYbPz5dWbp2beVlwAIWsIBVtwEWsIAFLGABC1jAEmABC1jAAhawgAUsYAELWMAClliWwGoeWNXc7e4ksX2X49JHWvv3UGSlfOXjiKmVr3yt/7kBC1gf/UG7fDLfs3s+ZkR+6ffCnJvD44+F9etbeMm/uz6sWpbfc0f2s0nZhaeWRn6m9IWdgQUsYAGrybuQd22bd2ubd2+b92ib79I233WnfLd2ef998pOH5+d9M79ySrjphjD/tnD/ovDU4+GlF8Kr/whvvpG28i80XLsmba+/Ela/EFY9FZb+KV98V/6bG/NZV+STzszOPD4b01Aa1rU0rEvp8J1LR+w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WtPT7Ddesua13A7CAVUywqrn6BRjt170bbu9zQDxHu3fQ4EcOHbp6aMO/Dt793SHdsyFdtrgKdNhHXAX6X6iK54Bf2nXD6T1L4w/LfzIh3LMwafX66+lnFbOsTiSq5rhbA7BqbzTHucACFrCABSxgAQtYwAIWsIAFLGABC1jAAhawgAUsYAELWMACFrCABSxgAQtYwAIWsIAFLGABC1jAAhawgAUsYAELWMACFrCABSytUm1bAVatOS1eZ7XgaMACFrCABSxgAQtYwAIWsNhhToEFLGABC1jAAhawgAUsYAELWOYUWMACFrCABSxgAQtYwAKWxQ0scwosYAHLBwUWsIAFLGABC1gWN7CABazq/UF2U168jirACqmfRd76LQ8sYAHLLgALWMACFrCABSxgAQtYwAIWsOwCsIAFLGABC1jAAhawgAUsYAELWMACFrCABSxgAQtYwAIWsICl24EFLGABC1jAAhawgAUsYNX8lFdz3NSheP1ZP8S0/gcFFrCABSxgAQtYwAIWsIAFLGABC1jAApZdABawgAUsYAELWMACFrCABSxgAQtYwAIWsIAFLGABC1jAAhawgAUsYAELWMACFrCABSxgAavm59LirqGtAJOlvNvvcwMWsHSU8gILWMAClvICC1g6CljAAhawgKW8wAIWsIAFLGABS0cBC1jAAhawlBdYwAIWsIAFLGDpKGABC1jAApbyAgtYwAIWsIAFLB0FrHoHS5qXar4vRTXfAMCcVsOcAgtYwDKnwAKWxQ0scwossbiBBSxgWdzAMqfAApbFDSxzCiyxuIEFLGBZ3MAyp8AClsUNLHMKLLG4gQUsYFncwDKnwAKWxQ0scwossbiBBazavltD/WzVvIDqpwcKMAvWWzMCLGABC1jAAhawzAKwgKVVgAUsYAELWMACFrCABSyzACxgaRVgAQtYwLKAgAUsYAELWGYBWMDSKsACFrCABSyzACxgAQtYZgFYwNIqwAIWsOrlR/YLgILy1mcb189CAhawlBdYwAKWdQYsYAFLRwELWMACFrCUF1jAApZ1BixgAUtHAQtYwAIWsJQXWMACFrCABSxg6ShgAQtYwAKW8gILWDoKWMACFrB0lPICC1h+er5O99QdONSt2joLWPbUpm7AApbGA5a6AQtYGk/dgKWN7anGUzdgAUvjAUvdgAUsjaduwNLG9lTjqRuwgKXxgKVuwAKWxlM3YGljYGk8dQMWsDQesNQNWMDSeOoGLG1cNP6CNCvVfMOJOlmWwLIyBFjAAhawgAUsYAELWMACFrCsDAEWsIAFLGABC1jAAhawgAUsK0OABSxgAQtYwAIWsIAFLGABC1gCLGABC1jAAhawgAUsYAELWEUbTbe790MVFgRYwAIWsIAFLGABC1jAAhawgAUsYAELWMACFrCABSxgAQtYwAIWsIAFLGABC1jAAhawgAUsYAELWMACFrCABSxgAQtYwAIWsIAFLGABq009tApiqqFubnVQvF0AFrCAZU6BBSxgAQtYwAKWugELWMACFrDMKbCABSxgAQtYwFI3YAELWMACFrCABSxgAQtYwAKWugELWMACFrCABSyNByxgAQtYwFI3YAGrSsHyo+fVsDJs1aBkNX89qPW6AQtYNmABC1jAAhawgAUsG7CApVWAZU6BBSxgAQtYwAIWsGzAApZWAZY5BRawgAUsjQcsYAHLBixgaRVgmVNgAQtYwAIWsIAFLBuw6h0sEZEqCbBEBFgiIsASEWCJiABLRARYIgIsERFgiYgAS0SAJSICLBGRZuU/9CAOM9La1ysAAAAASUVORK5CYII=
美滋滋
本人也是菜鸟一枚,找了些资料,看到了两位大佬写的一些,整合了一下,原地址我写完给忘掉了,抱歉抱歉!!