模板信息

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将本地图片复制添加水印并导出到本地相关推荐

  1. 如何将Chrome本地安装的扩展应用导出到本地

    有时由于种种原因,我们不能直接使用Chrome web store进行Chrome扩展应用的安装,这时可以让一位已经安装了某Chrome扩展应用的朋友将他的应用导出到本地成为.crx文件,然后发给你, ...

  2. android picasso 显示本地图片,剖析Picasso加载压缩本地图片流程(解决Android 5.0部分机型无法加载本地图片的问题)...

    之前项目中使用Picasso遇到了一个问题:在Android 5.0以上版本的部分手机上使用Picasso加载本地图片会失败.为了解决这个问题,研究了一下Picasso加载和压缩本地图片的流程,才有了 ...

  3. 今天搞一下 MultipartFile java后台接收图片参数,并转存到本地

    public Stringuploadtaskpic(@RequestParam("multipartFile") MultipartFile multipartFile,@Api ...

  4. php接受本地图片上传,用php把本地要上传的图片经过剪切,存入数据库

    //获取上传临时文件路径 $uploadedfile = $_FILES['uploadfile']['tmp_name']; //获取源文件资源句柄.接收参数为图片路径,返回句柄 $source = ...

  5. android本地图片,Android中ImageView实现选择本地图片并显示功能

    运行结果: 模拟器图库就三张 没办法~画质挺感人~ 一个隐式意图 布局文件: xmlns:tools="http://schemas.android.com/tools" andr ...

  6. 有道云笔记Markdown中引用本地图片

    用过有道云笔记的同志都知道,用markdown写文章时引用本地图片要会员(csdn多好,免费给你上传本地图片),这里给大家介绍一种方法可以应用本地图片. Step 1:将本地图片上传到有道云 Step ...

  7. 如何在CSND上传本地图片

    如何在CSND上传本地图片 1.全选markdown的内容,粘贴到msdn上, 2.根据本地图片的路径,以及文件名,找到本地图片的地址, 3.复制图片名称, 4.选择图片,进行本地图片的上传 5.上传 ...

  8. 在MarkDown中插入本地图片

    在MarkDown中插入本地图片 用法: ![图片描述](图片网络地址) ![图片描述](本地图片相对路径) 注意: 插入本地图片的时候一定是相对路径 例如: ![项目布局](./Pictures/1 ...

  9. Webview加载本地图片的方案对比

    前言 先讲讲为什么会有这篇blog,话说前几天做个模块,要求是这样的: 做一个webview的页面,功能类似于微信发朋友圈一样,要求能上传本地图片到webview中进行展示,并按用户喜好添加和删除,当 ...

最新文章

  1. ant design vue table 高度自适应_html之table表格
  2. 创建github账号
  3. 一起学nRF51xx 12 -  flash
  4. TClientDataSet[3]: 手动建立数据集
  5. 新乡台达服务器驱动器维修,台达DELTA伺服驱动器维修
  6. jzoj4226-A【图论】
  7. 13c语言中的文件是一种流式文件,读写时均以字符为单位.,C语言判断题部分.doc...
  8. Bash脚本删除相对路径下小于指定大小的文件
  9. android平台的一款mud工具,论剑mud脚本
  10. 圆柱表面积在线计算机,体积计算器在线计算
  11. sqlite3数据库-常用命令
  12. 根据数据文件sql生成报告rpt
  13. 基于FPGA的简单视频传输方案设计
  14. Kali安装中文输入法
  15. 蓝桥杯 算法训练 ALGO-114 黑白无常
  16. 如何简单轻松的把360度全景图变成视频
  17. 等保2.0控制点总结
  18. 1012-RPA与目标使用者
  19. Kubelet Eviction Manager工作机制
  20. Linux shell脚本

热门文章

  1. Unity3D常用游戏开发插件测评总结
  2. 猫猫学IOS(六)UI之iOS热门游戏_超级猜图
  3. 打造绿色数据中心,Colt DCS 是认真的!
  4. SICP ex 1-27
  5. POJ 2349 Arctic Network (MST中的第K长路)
  6. 深入理解JVM虚拟机第三版疑问解析
  7. AT89C52RC定时器T2
  8. 从零开始玩rv1126core板(零)
  9. 订单管理系统 订货系统平台建设三阶段分享
  10. 怎么在微信里呼起手淘