public static String getLogoQRCode(String qrUrl,String logoFile,String productName,String tel,String outfile,int width,int height)
{
String content = qrUrl;
try
{
BufferedImage bim = getQR_CODEBufferedImage(content, BarcodeFormat.QR_CODE, width, height, getDecodeHintType());
String result= addLogo_QRCode(bim, new File(logoFile), new LogoConfig(), productName,tel,outfile);
if("true".equals(result)){
return "SUCCESS";
}else{
return "FAILED";
}
}
catch (Exception e)
{
logger.error("getLogoQRCode:"+e.getMessage(), e);
e.printStackTrace();
return "FAILED";
}
}
/** * 设置二维码的格式参数 * * @return */
public static Map
{
// 用于设置QR二维码参数
Map
// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 设置编码方式
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 0);
// hints.put(EncodeHintType.MAX_SIZE, 350);
// hints.put(EncodeHintType.MIN_SIZE, 100);
return hints;
}
/** * 生成二维码bufferedImage图片 * * @param content * 编码内容 * @param barcodeFormat * 编码类型 * @param width * 图片宽度 * @param height * 图片高度 * @param hints * 设置参数 * @return */
public static BufferedImage getQR_CODEBufferedImage(String content, BarcodeFormat barcodeFormat, int width, int height, Map
{
MultiFormatWriter multiFormatWriter = null;
BitMatrix bm = null;
BufferedImage image = null;
try
{
multiFormatWriter = new MultiFormatWriter();
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
bm = multiFormatWriter.encode(content, barcodeFormat, width, height, hints);
int w = bm.getWidth();
int h = bm.getHeight();
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
}
catch (WriterException e)
{
e.printStackTrace();
}
return image;
}
public static String addLogo_QRCode(BufferedImage bim, File logoPic, LogoConfig logoConfig, String productName,String tel,String outFile)
{
try
{
/** * 读取二维码图片,并构建绘图对象 */
BufferedImage image = bim;
Graphics2D g = image.createGraphics();
/** * 读取Logo图片 */
BufferedImage logo = ImageIO.read(logoPic);
/** * 设置logo的大小,本人设置为二维码图片的20%,因为过大会盖掉二维码 */
int widthLogo = logo.getWidth(null)>image.getWidth()*3/10?(image.getWidth()*3/10):logo.getWidth(null),
heightLogo = logo.getHeight(null)>image.getHeight()*3/10?(image.getHeight()*3/10):logo.getWidth(null);
/** * logo放在中心 */
int x = (image.getWidth() - widthLogo) / 2;
int y = (image.getHeight() - heightLogo) / 2;
/** * logo放在右下角 * int x = (image.getWidth() - widthLogo); * int y = (image.getHeight() - heightLogo); */
//开始绘制图片
g.drawImage(logo, x, y, widthLogo, heightLogo, null);
g.dispose();
//新的图片,把带logo的二维码下面加上文字
BufferedImage outImage = new BufferedImage(401, 466, BufferedImage.TYPE_4BYTE_ABGR_PRE);
Graphics2D outg = outImage.createGraphics();
outg.setBackground(new Color(243, 112, 34));
outg.clearRect(0, 0,401, 466);//通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
//画二维码到新的面板
outg.drawImage(image, 1, 1, image.getWidth()-1, image.getHeight()-1, null);
//画文字到新的面板
outg.setColor(Color.white);
outg.setFont(new Font("微软雅黑",Font.BOLD,20)); //字体、字型、字号
if(productName == null)productName = "";
if(tel == null)tel = "";
outg.drawString("联系人:"+productName, 20 , image.getHeight()+25); //画文字
outg.drawString("联系电话:"+tel, 20 , image.getHeight() + 50); //画文字
outg.dispose();
outImage.flush();
image = outImage;
logo.flush();
image.flush();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.flush();
ImageIO.write(image, "png", baos);
ImageIO.write(image, "png", new File(outFile)); //TODO
}
catch (Exception e)
{
logger.error("addLogo_QRCode:"+e.getMessage(), e);
e.printStackTrace();
return "false";
}
return "true";
}