美文网首页
day06-01 图片复制、水印以及黑白

day06-01 图片复制、水印以及黑白

作者: 黑桃_06ea | 来源:发表于2019-03-11 16:51 被阅读0次
    • 将D盘的一张图片复制到E盘,再给图片添加水印,处理成黑白
    import org.slf4j.LoggerFactory;
    
    import java.io.*;
    
    /**
     * Created by Administrator on 2019/3/11.
     */
    public class ImageCopy {
        private static final org.slf4j.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:/bg.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();
        }
    }
    
    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.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 Administrator on 2019/3/11.
     */
    @Aspect
    public class ImageHandler {
        private static final org.slf4j.Logger logger= LoggerFactory.getLogger(ImageHandler.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:/bg.jpg");
            InputStream input=new FileInputStream(srcFile);
            BufferedImage srcImg= ImageIO.read(input);
            int width=srcImg.getWidth();
            int height=srcImg.getHeight();//获取图片大小,用于计算,让水印在右下角
            int size=20;
            File destFile=new File("D:/bg1.jpg");
            Color color=new Color(211,71,38);
            Font font=new Font("微软雅黑",Font.BOLD,size);
            String text="海贼王";
            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:/bg.jpg");
            File destFile=new File("E:/bg2.jpg");
            ImageUtil.gray(srcFile,destFile);
        }
    }
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    /**
     * Created by Administrator on 2019/3/11.
     */
    public class ImageCopyApp {
        public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
            ImageCopy imageCopy=context.getBean(ImageCopy.class);
            try {
                imageCopy.copyImage();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 二维码生成
    import cn.hutool.core.io.FileUtil;
    import cn.hutool.extra.qrcode.QrCodeUtil;
    import cn.hutool.extra.qrcode.QrConfig;
    import java.awt.*;
    /**
     * Created by Administrator on 2019/3/11.
     */
    public class QrCode {
        public static void main(String[] args) {
            QrConfig config = new QrConfig(300, 300);
            config.setImg("D:/lufei04.jpg");
            // 设置边距,既二维码和背景之间的边距
            config.setMargin(3);
            // 设置前景色,既二维码颜色(青色)
            config.setForeColor(Color.CYAN.getRGB());
            // 设置背景色(灰色)
            config.setBackColor(Color.GRAY.getRGB());
            // 生成二维码到文件,也可以到流
            QrCodeUtil.generate("https://www.jianshu.com/u/ec5ebfec0565",config, FileUtil.file("d:/my_code.jpg"));
        }
    }
    
    my_code.jpg

    相关文章

      网友评论

          本文标题:day06-01 图片复制、水印以及黑白

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