6.Spring中的图片复制和二维码生成
2019-03-11 本文已影响0人
长生藤
图片本地间操作
- 图片的复制
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:/bg.jpg"); //原图片位置
File destFile = new File("E:/bg1.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);
}
//关闭io流
in.close();
out.close();
}
}
- 图片加水印和黑白效果
该效果用前置增强类型和后置增强类型进行操作,需要添加依赖,版本号在<properties>中添加
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
该依赖使用参考Hutool
@Aspect//引用aspectj依赖
public class ImageHandler {
private static final Logger logger = LoggerFactory.getLogger(ImageCopy.class);
private int width;
private int height;
@Pointcut("execution(* com.spring.image.ImageCopy.copyImage())")
public void handleImage(){}
@Before(value = "handleImage()")
public void pressTextOnImage(){
logger.info("开始加水印");
File srcFile = new File("D:/bg.jpg");
File destFile = new File("D:/bg1.jpg");
Image image;
try {
image = ImageIO.read(srcFile);//读取图片
width =image.getWidth(null);
height = image.getHeight(null);
} catch (IOException e) {
e.printStackTrace();
}
Color color = new Color(211,71,38);//水印颜色
Font font = new Font("微软雅黑",Font.BOLD,40);//水印字体
//hutool依赖中的方法
ImageUtil.pressText(srcFile,destFile,"Spring AOP水印",color,font,width/4+font.getSize(),height/2-font.getSize(),1.0f);
}
@AfterReturning("handleImage()")
public void grayImage(){
logger.info("开始转黑白");
File srcFile = new File("E:/bg1.jpg");
File destFile = new File("E:/bg2.jpg");
//hutool依赖中的方法
ImageUtil.gray(srcFile,destFile);
}
}
之后对两个类在**.xml文件中进行配置
<aop:aspectj-autoproxy/>
<bean id="imageCopy" class="com.spring.image.ImageCopy"/>
<bean id="imageHandler" class="com.spring.image.ImageHandler"/>
最后执行结果
public class ImageCopyApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
ImageCopy imageCopy = context.getBean(ImageCopy.class);
try {
imageCopy.copyImage();
} catch (IOException e) {
e.printStackTrace();
}
}
}
二维码的生成
使用时同样需要依赖hutool,并且需要引入新的依赖,才能使用二维码工具QrCodeUtil
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
public class Qr {
public static void main(String[] args) {
QrConfig qrConfig = new QrConfig(300,300);
qrConfig.setMargin(3);
Color foreColor = new Color(210, 180, 140);
Color bgColor = new Color(216, 191, 216);
int foreColorRGB = foreColor.getRGB();
int bgColorRGB = bgColor.getRGB();
/*
* content:需要生成二维码的网站
* setImg()需要插入二维码中心的图标
* FileUtil.file()生成二维码的位置
* */
QrCodeUtil.generate(
"https://www.baidu.com/",
QrConfig.create().setImg("d:/top.jpg").setBackColor(bgColorRGB).setForeColor(foreColorRGB),
FileUtil.file("e:/qrcodeWithLogo.jpg")
);
}
}