再pom.xml中添加dependency
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
图片处理:
复制图片
package com.spring.orm.Aop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
/**
* Created by User on 2019/3/11.
*/
public class ImageCopy {
private static final Logger logger= LoggerFactory.getLogger(ImageCopy.class);
public void copyImage()throws IOException{
logger.info("开始将图片从D盘复制到E盘...");
File srcFile =new File("D:/img1.jpg");
File destFile=new File("E:/img1.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();
}
}
添加水印及其他操作
package com.spring.orm.Aop;
import cn.hutool.core.util.ImageUtil;
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.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by User on 2019/3/11.
*/
@Aspect
public class ImageHandler {
private static final Logger logger = LoggerFactory.getLogger(ImageHandler.class);
// 切点函数,必须为空
@Pointcut("execution(* com.spring.orm.Aop.ImageCopy.copyImage())")
public void handleImage(){
}
@Before(value = "handleImage()")
public void pressTextOnImage()throws IOException{
logger.info("开始给图片添加水印");
File srcFile = new File("D:/img.jpg");
InputStream input= new FileInputStream(srcFile);
BufferedImage srcImg = ImageIO.read(input);
int width=srcImg.getWidth();
int height=srcImg.getHeight();
System.out.println("原图宽:"+width+",原图高:"+height);
File destFile=new File("D:/img1.jpg");
Color color =new Color(211,71,38);
int size=20;
Font font =new Font("微软雅黑",Font.BOLD,size);
String text="zrl";
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:/img1.jpg");
File destFile=new File("E:/img2.jpg");
ImageUtil.gray(srcFile,destFile);
}
}
运行类
public class ImageCopyApp {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("Spring.xml");
ImageCopy imageCopy=context.getBean(ImageCopy.class);
try{
imageCopy.copyImage();
}catch(IOException e){
e.printStackTrace();
}
}
}
生成网站二维码:
package com.spring.orm.Aop;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import java.awt.*;
/**
* Created by User on 2019/3/11.
*/
public class QrCode {
public static void main(String[] args) {
QrConfig config=new QrConfig(300,300);
config.setImg("d:/img.jpg");
config.setMargin(3);
Color foreColor=new Color(173,255,47);
Color bgColor=new Color(240,255,240);
int foreColorRGB=foreColor.getRGB();
int bgColorRGB=bgColor.getRGB();
config.setForeColor(foreColorRGB);
config.setBackColor(bgColorRGB);
QrCodeUtil.generate("https://www.jianshu.com/u/ce6045cb864d",config,FileUtil.file("E:/Logo.jpg"));
}
}