LogoConfig.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package com.fdkankan.common.util;
  2. import org.springframework.core.io.ClassPathResource;
  3. import javax.imageio.ImageIO;
  4. import java.awt.*;
  5. import java.awt.geom.RoundRectangle2D;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9. public class LogoConfig {
  10. /**
  11. * 设置 logo
  12. * @param matrixImage 源二维码图片
  13. * @return 返回带有logo的二维码图片
  14. * @throws IOException
  15. * @author Administrator sangwenhao
  16. */
  17. public BufferedImage LogoMatrix(BufferedImage matrixImage, String logoPath) throws IOException{
  18. /**
  19. * 读取二维码图片,并构建绘图对象
  20. */
  21. Graphics2D g2 = matrixImage.createGraphics();
  22. int matrixWidth = matrixImage.getWidth();
  23. int matrixHeigh = matrixImage.getHeight();
  24. /**
  25. * 读取Logo图片
  26. */
  27. if(logoPath == null){
  28. // logoPath = this.getClass().getResource("/static/img/logo.png").getPath();
  29. ClassPathResource classPathResource = new ClassPathResource("static/img/logo.jpg");
  30. logoPath =classPathResource.getURL().getPath();
  31. }
  32. BufferedImage logo = ImageIO.read(new File(logoPath));
  33. //开始绘制图片
  34. g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制
  35. BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
  36. g2.setStroke(stroke);// 设置笔画对象
  37. //指定弧度的圆角矩形
  38. RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);
  39. g2.setColor(Color.white);
  40. g2.draw(round);// 绘制圆弧矩形
  41. //设置logo 有一道灰色边框
  42. BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
  43. g2.setStroke(stroke2);// 设置笔画对象
  44. RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);
  45. g2.setColor(new Color(128,128,128));
  46. g2.draw(round2);// 绘制圆弧矩形
  47. g2.dispose();
  48. matrixImage.flush() ;
  49. return matrixImage ;
  50. }
  51. // public static void main(String[] args) {
  52. // LogoConfig config = new LogoConfig()
  53. // this.getClass().getResource("/static/img/logo.png").getPath();
  54. // }
  55. }