欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java将本地图片复制添加水印并导出到本地

程序员文章站 2022-05-26 23:53:09
...

模板信息

java将本地图片复制添加水印并导出到本地

package com.example.demo.ChartGraphics;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Date;

/** 导出照片,添加水印*/
public class ChartGraphics2 {
    private BufferedImage image;
    private int imageWidth = 400; // 图片的宽度默认
    private int imageHeight = 400; // 图片的高度默认
    private static String[] files={};
/*获取原图片宽高*/
    public void getImageSizeByBufferedImage(String src) {
                 long beginTime = new Date().getTime();
                 File file = new File(src);
                 FileInputStream is = null;
                 try {
                     is = new FileInputStream(file);
                 } catch (FileNotFoundException e2) {
                     e2.printStackTrace();
                 }
             BufferedImage sourceImg = null;
             try {
                     sourceImg = javax.imageio.ImageIO.read(is);
                     System.out.println("width:" + sourceImg.getWidth());
                     System.out.println("height:" + sourceImg.getHeight());
                     imageWidth=sourceImg.getWidth();
                     imageHeight=sourceImg.getHeight();
                 System.out.println("imageWidth:" + imageWidth);
                 System.out.println("imageHeight:" + imageHeight);
                 } catch (IOException e1) {
                     e1.printStackTrace();
                 }
             long endTime = new Date().getTime();
             System.out.println("使用[BufferedImage]获取图片尺寸耗时:[" + (endTime - beginTime)+"]ms");
         }
         
         /*给图片添加水印信息,绘制图片*/
    public void graphicsGeneration(String imgurl) {
//        int H_tip = 60; // 文字的高度

        image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        String fontName="宋体";
        String[] pressText={"0001","南京市道路规划展览馆","040307","1000","2000","3000","4000","5000","6000","23456立方米","2","0010,0020","宣武门22号","0.10","未展开","2020年07月28日"};
        int fontStyle=Font.TRUETYPE_FONT;
        int fontSize=14;
        Color color=Color.BLACK;
        float alpha=1f;
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        /** 不加年份日期,16组数据,加年份半年18组数据*/
        //各个属性对应坐标:页码,单位,用户代码,月1-月6,定额总量,水表块数量,户号,水表地址,增减系数,截止时间,日期
        int[] xn={580,122,546,123,206,289,378,466,546,356,185,148,216,197,291,500};
        int[] yn={10,153,153,314,314,314,314,314,314,387,438,482,517,566,632,927};
        // 设置图片的背景色
//        Graphics2D main = image.createGraphics();
//        main.setColor(Color.white);
//        main.fillRect(0, 0, imageWidth, imageHeight);

        // ***********************模板照片**************
        Graphics mainPic = image.getGraphics();
        BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        try {
            bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
        } catch (Exception e) {

        }

        if (bimg != null) {
            mainPic.drawImage(bimg, 0, 0, imageWidth, imageHeight, null);
            mainPic.dispose();
        }
        creatNewImg(pressText,image,width,height,fontName,fontStyle,fontSize,color,xn,yn,alpha);
    }
    private static boolean creatNewImg(String[] pressText,BufferedImage image,int width,int height,String fontName,int fontStyle,int fontSize,
                                       Color color,int[] xn,int[] yn,float alpha){
        // ***********************设置水印内容****************

        Graphics2D tip = image.createGraphics();
        tip.drawImage(image,0,0, width, height, null);
        tip.setFont(new Font(fontName, fontStyle, fontSize));
        tip.setColor(color);
        tip.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

        int width_wi = 0;
        int height_wi = fontSize;

        int widthDiff = width-width_wi;
        int heightDiff = height-height_wi;
        for (int n=0;n<xn.length;n++){
            width_wi=fontSize*getTextLength(pressText[n]);
            if(xn[n]<0){
                xn[n] = widthDiff/2;
            }else if(xn[n]>widthDiff){
                xn[n]=widthDiff;
            }

            if(yn[n]<0){
                yn[n] = heightDiff/2;
            }else if(yn[n]>heightDiff){
                yn[n] = heightDiff;
            }
            tip.drawString(pressText[n], xn[n], yn[n]+height_wi);//水印文件

        }
        tip.dispose();


        createImage(image,"D:\\person2.png");
        System.out.println("生成图片成功");
        return false;
    }
    /**
     * 计算文字像素长度
     * @param text
     * @return
     */
    private static int getTextLength(String text){
        int textLength = text.length();
        int length = textLength;
        for (int i = 0; i < textLength; i++) {
            int wordLength = String.valueOf(text.charAt(i)).getBytes().length;
            if(wordLength > 1){
                length+=(wordLength-1);
            }
        }

        return length%2==0 ? length/2:length/2+1;
    }

    // 生成图片文件
    @SuppressWarnings("restriction")
    public static void createImage(BufferedImage image,String fileLocation) {
        BufferedOutputStream bos = null;
        if (image != null) {
            try {
                FileOutputStream fos = new FileOutputStream(fileLocation);
                bos = new BufferedOutputStream(fos);

                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
                encoder.encode(image);
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {// 关闭输出流
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        ChartGraphics2 cg = new ChartGraphics2();
        try {
            cg.getImageSizeByBufferedImage("D:\\shang.jpg");
            cg.graphicsGeneration("D:\\shang.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

最终效果

java将本地图片复制添加水印并导出到本地