美文网首页
day05 基于XML的AOP例子使用hutool工具包

day05 基于XML的AOP例子使用hutool工具包

作者: 山下_26 | 来源:发表于2019-03-11 16:36 被阅读0次

Hutool是一个Java工具包类库,对文件、流、加密解密、转码、正则、线程、XML等JDK方法进行封装,组成各种Util工具类

图片工具-ImageUtil

在使用hutool工具包之前我们需要在maven中添加依赖

    <!--hutool工具依赖-->
    <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>4.5.1</version>
    </dependency>
1.在java中新建一个imageCopy类,在D盘的根路径下准备好一张图片1.jpg
public class ImageCopy {

    public void copyImage() throws IOException {
        System.out.println("开始将图片从D盘复制到E盘...");
        File srcFile=new File("D:/1.jpg");
        File destFile=new File("E:/1.jpg");
        InputStream in = new FileInputStream(srcFile);
        OutputStream out=new FileOutputStream(destFile);
        byte[] bytes=new byte[(int) srcFile.length()];
        int len;
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
        in.close();
        out.close();
    }
}

2.新建一个imageHandler类
package com.spring.image;

import cn.hutool.core.util.ImageUtil;
import com.spring.image.ImageCopy;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

@Aspect
public class ImageHandler {
    private static final Logger logger = LoggerFactory.getLogger(ImageCopy.class);

    @Pointcut("execution(* com.spring.image.ImageCopy.copyImage())")
    public void handleImage(){}

    @Before(value = "handleImage()")
    public void pressTextOnImage() throws IOException {
        logger.info("开始加水印");
        File srcFile = new File("D:/1.jpg");
        //通过字节输入流创建一个BufferedImage对象
        InputStream input=new FileInputStream(srcFile);
        BufferedImage srcImg= ImageIO.read(input);
        //自动获得图片的尺寸
        int width=srcImg.getWidth();
        int height=srcImg.getHeight();
        File destFile = new File("D:/2.jpg");
        Color color = new Color(74, 181, 245);
        int size=20;
        Font font = new Font("微软雅黑",Font.BOLD,size);
        String text="by @26";
        //给图片添加水印
        ImageUtil.pressText(srcFile,destFile,text,color,font,(width-text.length()*size)/2,height/2-size,1.0f);
    }

    @AfterReturning("handleImage()")
    public void grayImage(){
        logger.info("开始转黑白");
        File srcFile = new File("E:/1.jpg");
        File destFile = new File("E:/2.jpg");
        //将图片变为黑白
        ImageUtil.gray(srcFile,destFile);
    }

}

3.在spring.xml中进行aop配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:aspectj-autoproxy/>
    <bean id="imageCopy" class="com.spring.image.ImageCopy"/>
    <bean id="imageHandler" class="com.spring.image.ImageHandler"/>

</beans>
4.运行main方法类
package com.spring.image;

import com.spring.aop.Tiger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ImageCopyApp {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        ImageCopy imageCopy=context.getBean(ImageCopy.class);
        try {
            imageCopy.copyImage();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

可以看到d盘中多了一张带水印的图片,e盘中多了一张原图和一张黑白图片

二维码工具-QrCodeUtil

制定一张本人简书主页的二维码
public class Qrcode {
    public static void main(String[] args) {
        QrConfig config = new QrConfig(300, 300);
        // 设置边距,即二维码和背景之间的边距
        config.setMargin(3);
        Color foreColor=new Color(197, 193, 170);
        Color bgColor=new Color(245, 255, 250);
        int foreColorRGB=foreColor.getRGB();
        int bgColorRGB=bgColor.getRGB();
        // 设置前景色
        config.setForeColor(foreColorRGB);
        // 设置背景色
        config.setBackColor(bgColorRGB);
        //将简书logo添加进二维码
        config.setImg("e:/jianshu.jpg");
        // 生成二维码到文件,也可以到流
        QrCodeUtil.generate("https://www.jianshu.com/u/32f964ed1fbc", config, FileUtil.file("e:/qrcode.jpg"));
    }
}
效果如下:

相关文章

网友评论

      本文标题:day05 基于XML的AOP例子使用hutool工具包

      本文链接:https://www.haomeiwen.com/subject/wbjbpqtx.html