LogoConfig.java 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.fdkankan.common.util;
  2. import cn.hutool.core.util.StrUtil;
  3. import java.io.InputStream;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.core.io.ClassPathResource;
  6. import javax.imageio.ImageIO;
  7. import java.awt.*;
  8. import java.awt.geom.RoundRectangle2D;
  9. import java.awt.image.BufferedImage;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import org.springframework.core.io.Resource;
  13. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  14. import org.springframework.core.io.support.ResourcePatternResolver;
  15. @Slf4j
  16. public class LogoConfig {
  17. /**
  18. * 设置 logo
  19. * @param matrixImage 源二维码图片
  20. * @return 返回带有logo的二维码图片
  21. * @throws IOException
  22. * @author Administrator sangwenhao
  23. */
  24. public BufferedImage LogoMatrix(BufferedImage matrixImage, boolean logo, String logoPath) throws IOException{
  25. /**
  26. * 读取二维码图片,并构建绘图对象
  27. */
  28. Graphics2D g2 = matrixImage.createGraphics();
  29. if(logo){
  30. int matrixWidth = matrixImage.getWidth();
  31. int matrixHeigh = matrixImage.getHeight();
  32. File file = null;
  33. BufferedImage logoBuffer = null;
  34. try {
  35. /**
  36. * 读取Logo图片
  37. */
  38. if(StrUtil.isEmpty(logoPath)){
  39. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  40. Resource[] resources = resolver.getResources("static/img/logo.jpg");
  41. Resource resource = resources[0];
  42. //获得文件流,因为在jar文件中,不能直接通过文件资源路径拿到文件,但是可以在jar包中拿到文件流
  43. InputStream inputStream = resource.getInputStream();
  44. logoBuffer = ImageIO.read(inputStream);
  45. }else {
  46. file = new File(logoPath);
  47. logoBuffer = ImageIO.read(file);
  48. }
  49. }catch (IOException e){
  50. log.info("读取图片流失败,path="+ logoPath, e);
  51. }
  52. //开始绘制图片
  53. g2.drawImage(logoBuffer,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制
  54. BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
  55. g2.setStroke(stroke);// 设置笔画对象 44
  56. //指定弧度的圆角矩形
  57. RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20);
  58. g2.setColor(Color.white);
  59. g2.draw(round);// 绘制圆弧矩形
  60. //设置logo 有一道灰色边框
  61. BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
  62. g2.setStroke(stroke2);// 设置笔画对象
  63. RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);
  64. g2.setColor(new Color(128,128,128));
  65. g2.draw(round2);// 绘制圆弧矩形
  66. }
  67. g2.dispose();
  68. matrixImage.flush() ;
  69. return matrixImage ;
  70. }
  71. // public static void main(String[] args) {
  72. // LogoConfig config = new LogoConfig()
  73. // this.getClass().getResource("/static/img/logo.png").getPath();
  74. // }
  75. }