美文网首页
对图片进行处理、有趣的二维码

对图片进行处理、有趣的二维码

作者: 忆曦雪_ | 来源:发表于2019-03-12 18:06 被阅读0次

    1、对图片进行拷贝、加水印和黑白处理

    • ImageCopy类
    package com.spring.aop;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.io.*;
    
    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:/city.jpg");
            File destFile=new File("E:/city1.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();
        }
    }
    
    • ImageHandle类
    package com.spring.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;
    
    @Aspect
    public class ImageHandler {
        private static final Logger logger= LoggerFactory.getLogger(ImageCopy.class);
    
    //    切点函数,必须为空
        @Pointcut("execution(* com.spring.aop.ImageCopy.copyImage())")
        public  void handleImage(){
    
        }
        @Before(value="handleImage()")
        public void pressTextOnImage()throws IOException {
            logger.info("开始给图片添加水印...");
            File srcFile=new File("D:/city.jpg");
    //        通过字节输入流创建一个BufferImage对象
            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:/city1.jpg");
            Color color=new Color(250, 220, 107);
            int size=20;
            Font font =new Font("微软雅黑", Font.BOLD,size);
            String text="Wuyou专用";
           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:/city1.jpg");
            File destFile=new File("E:/city2.jpg");
            ImageUtil.gray(srcFile,destFile);
        }
    
    }
    
    
    • ImageCopyApp类
    package com.spring.aop;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    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();
           }
       }
    }
    
    • 运行结果


      city.jpg
    city1.jpg
    city2.jpg

    2、有趣的二维码

    • erweima类
    package com.spring.aop;
    
    import cn.hutool.core.io.FileUtil;
    import cn.hutool.extra.qrcode.QrCodeUtil;
    import cn.hutool.extra.qrcode.QrConfig;
    
    import java.awt.*;
    
    public class erweima {
       public static void main(String[] args) {
           QrConfig config = new QrConfig(300, 300);
    // 设置边距,既二维码和背景之间的边距
           config.setMargin(3);
           Color foreColor = new Color(250, 117, 8);
           Color bgColor = new Color(200, 179, 47);
    
           int foreColorRGB=foreColor.getRGB();
           int bgColorRGB=bgColor.getRGB();
           // 设置前景色,
           config.setForeColor(foreColorRGB);
           // 设置背景色
           config.setBackColor(bgColorRGB);
           config.setImg("d:/jianshu.jpg");
           // 生成二维码到文件,也可以到流
           QrCodeUtil.generate("https://www.jianshu.com/u/15950825da52", config, FileUtil.file("d:/qrcode.jpg"));
       }
    }
    
    • 运行结果(运行结果正确与否以能否扫出为准)


      qrcode.jpg

    相关文章

      网友评论

          本文标题:对图片进行处理、有趣的二维码

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