博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java生成图片,添加水印
阅读量:3925 次
发布时间:2019-05-23

本文共 2900 字,大约阅读时间需要 9 分钟。

private static final String URL = "D:/test/template.png";public static String certificateBuilder(String nickname, BigDecimal totalAmount) {        //得到图片缓冲区        FileInputStream fileInputStream = null;        String savePath = "";        try {            File file = new File(URL);            if (!file.exists()) {                log.info("模板不存在");            } else {                fileInputStream = new FileInputStream(file);                BufferedImage image = ImageIO.read(fileInputStream);                BufferedImage image2 = ImageIO.read(new File("D:/test/stamp.png"));                //获取图片的宽                int width = image.getWidth(null);                //获取图片的高                int height = image.getHeight(null);                log.info("模板位置:{}.宽:{}.高:{}.", URL, width, height);                //初始化画板                BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);                //得到它的绘制环境                Graphics2D g2 = (Graphics2D) bi.getGraphics();                g2.fillRect(0, 0, width, height);                //设置颜色                g2.setColor(Color.WHITE);                // 将图片大小设置为大约4cm*4cm 具体根据纸张大小设定                g2.drawImage(image, 0, 0, width, height, null);                g2.drawRect(0, 0, width, height);                g2.setFont(new Font("微软雅黑", Font.PLAIN, 30));                g2.setColor(Color.gray);                g2.drawString("证书编号:H11111122222233333", 315, 410);                //设置背景颜色                g2.setColor(Color.BLACK);                //向图片上写字符串                g2.setFont(new Font("微软雅黑", Font.PLAIN, 42));                g2.drawString("大慈善家", 320, 570);                g2.setFont(new Font("微软雅黑", Font.PLAIN, 42));                g2.drawString("2020年9月20日", 720, 1330);                g2.setFont(new Font("微软雅黑", Font.PLAIN, 42));                g2.setColor(Color.RED);                g2.drawString("5.12元", 450, 1030);                //盖章                g2.drawImage(image2, 810, 1174, image2.getWidth(null), image2.getHeight(null), null);                // 合成图片                StringBuffer path = new StringBuffer();                path.append("D:/test/");                path.append(UUIDUtils.gen());                path.append("-");                path.append(nickname);                path.append(".png");                boolean bool = ImageIO.write(bi, "PNG", new FileOutputStream(path.toString()));                log.info("结果:{}.", bool);                if (bool) {                    savePath = path.toString();                }            }        } catch (Exception e) {            log.error("生成图片错误", e);        } finally {            //关流            if (fileInputStream != null) {                try {                    fileInputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            return savePath;        }    }

 

转载地址:http://bicgn.baihongyu.com/

你可能感兴趣的文章
Android平台下OpenGL初步使用
查看>>
hadoop系列:zookeeper(2)——zookeeper核心原理(选举)
查看>>
hadoop系列:zookeeper(3)——zookeeper核心原理(事件)
查看>>
Android 输入系统 与 按键
查看>>
INIT_WORK和INIT_DELAYED_WORK详解
查看>>
Android 中多点触摸协议
查看>>
android 电容屏(四):驱动调试之驱动程序分析篇 -- FocalTech
查看>>
android 电容屏(三):驱动调试之驱动程序分析篇
查看>>
android 电容屏(二):驱动调试之基本概念篇
查看>>
android 电容屏(一):电容屏基本原理篇
查看>>
Linux 2.6内核与Linux 3.0 的区别!
查看>>
struct device的成员变量bus_id到哪里去了?
查看>>
Linux内核中ioremap映射的透彻理解
查看>>
Android 通过串口模拟 模拟物理按键
查看>>
驱动函数poll 和 应用里的 select
查看>>
Linux内核的等待队列
查看>>
在kernel 2.6.36 中已经完全删除了struct file_operations 中的ioctl 函数指针
查看>>
android长按home键流程
查看>>
Android有关Home按键的TYPE_KEYGUARD作用
查看>>
安卓4.0 中如何在app 中屏蔽HOME键
查看>>