java程序代码中下载图片并且添加水印后进行上传

阅读: 评论:0

java程序代码中下载图片并且添加水印后进行上传

java程序代码中下载图片并且添加水印后进行上传

需求:将从数据库中获取出来的图片添加水印展示在app端

先从数据库获取图片,使用了文件服务器minio

1. 拿到图片的链接地址如下:

47.108.116.186:9500/ukt-admin/a9f1ebe1-f605-4ae5-8587-313ac59edaf6

2. 根据图片的链接地址下载图片 代码工具类如下:

package com.ruoyi.business.utils;import org.springframework.beans.factory.annotation.Value;import java.awt.*;
import java.io.*;
import java.URL;
import java.URLConnection;
import java.util.Date;/*** @description: 根据文件链接地址下载文件* @author: wcb* @date: 2021/8/3 10:47*/
public class DownloadFile {@Value("${dbv.tbf}")private String tbf;/*** 根据链接地址下载文件* @param downloadUrl 文件链接地址* @param path        下载存放文件地址 + 文件名*/public  String downloadA(String downloadUrl,String path) {//水印内容String font = "www.uktian";URL url = null;DataInputStream dataInputStream = null;FileOutputStream fileOutputStream = null;try {url = new URL(downloadUrl);dataInputStream = new DataInputStream(url.openStream());fileOutputStream = new FileOutputStream(new File(path));ByteArrayOutputStream output = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int length;while ((length = ad(buffer)) > 0) {output.write(buffer, 0, length);}fileOutputStream.ByteArray());String timestamp = String.valueOf(Date.parse(String.valueOf(new Date())));String uuid = new WaterMark().waterPress(path,tbf+"营业执照"+timestamp+".jpg", ay, 50, font);return uuid;} catch (IOException e) {e.printStackTrace();return null;}}/*** @param downloadUrl 文件链接地址* @param filename    保存文件名* @param filePath    保存文件路径*/private static void downloadB(String downloadUrl, String filename, String filePath) {URL url = null;InputStream inputStream = null;OutputStream outputStream = null;try {url = new URL(downloadUrl);// 打开连接URLConnection con = url.openConnection();// 请求超时:5scon.setConnectTimeout(5 * 1000);inputStream = InputStream();byte[] bytes = new byte[1024];// 读取到的数据长度int length;File savePath = new File(filePath);if (!ists()) {// 如果不存在当前文件夹,则创建该文件夹boolean mkdir = savePath.mkdirs();if (!mkdir) {System.out.println("创建文件夹失败");return;}}outputStream = new Path() + "\" + filename);// 读取while ((length = ad(bytes)) != -1) {outputStream.write(bytes, 0, length);}} catch (IOException e) {e.printStackTrace();}}
}

下载完成后 添加水印 如下:

package com.ruoyi.business.utils;import com.fig.MinioConfig;
import com.ller.TestFileUploadController;
import io.minio.MinioClient;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.UUID;/***  满屏水印* @version 1.0* @author: wcb* @date: 2021-08-03 10:11*/
public class WaterMark {@Autowiredprivate MinioConfig minioConfig;/*** 水印之间的横向间隔*/private static  final  int XMOVE = 80;/*** 水印之间的纵向间隔*/private static  final  int YMOVE = 80;/*** 图片添加水印** @param outImgPath       添加水印后图片输出路径* @param markContentColor 水印文字的颜色* @param fontSize         文字大小* @param waterMarkContent 水印的文字,多排水印请使用"||"分割* @param srcImgPath       需要添加水印的图片的路径*/@SneakyThrowspublic String waterPress(String srcImgPath, String outImgPath, Color markContentColor, int fontSize, String waterMarkContent) {try {String[] waterMarkContents = waterMarkContent.split("\|\|");// 读取原图片信息File srcImgFile = new File(srcImgPath);Image srcImg = ad(srcImgFile);// 原图宽度int srcImgWidth = Width(null);// 原图高度int srcImgHeight = Height(null);// 加水印BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);// 得到画笔对象Graphics2D g = ateGraphics();g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);// Font font = new Font("Courier New", Font.PLAIN, 12);// 字体Font font = new Font("宋体", Font.PLAIN, fontSize);// 根据图片的背景设置水印颜色g.setColor(markContentColor);// 设置水印文字字体g.setFont(font);// 设置水印旋转g.Radians(-45), (double) Width() / 2, (double) Height() / 2);// 获取其中最长的文字水印的大小int maxLen = 0;int maxHigh = 0;for (int i = 0; i < waterMarkContents.length; i++) {waterMarkContent = waterMarkContents[i];int fontlen = getWatermarkLength(waterMarkContent, g);if (fontlen >= maxLen) {maxLen = fontlen;}maxHigh = maxHigh + (i + 1) * fontSize + 10;break;}// 文字长度相对于图片宽度应该有多少行int line = srcImgWidth * 2 / maxLen;int co = srcImgHeight * 2 / maxHigh;int yz = 0;// 填充Y轴方向for (int a = 0; a < co; a++) {int t = 0;for (int j = 0; j < waterMarkContents.length; j++) {waterMarkContent = waterMarkContents[j];int y = (j + 1) * fontSize + 10 + t;// 文字叠加,自动换行叠加,注意符号int tempX = -srcImgWidth / 2;int tempY = -srcImgHeight / 2 + y + yz;// 单字符长度int tempCharLen = 0;// 单行字符总长度临时计算int tempLineLen = 0;StringBuffer sb = new StringBuffer();for (int i = 0; i < waterMarkContent.length(); i++) {char tempChar = waterMarkContent.charAt(i);tempCharLen = getCharLen(tempChar, g);tempLineLen += tempCharLen;// 和图片的长度进行对应的比较操作if (tempLineLen >= srcImgWidth) {// 长度已经满一行,进行文字叠加g.String(), tempX, tempY);t = t + fontSize;// 清空内容,重新追加sb.delete(0, sb.length());tempY += fontSize;tempLineLen = 0;}// 追加字符sb.append(tempChar);}// 填充X轴for (int z = 0; z < line; z++) {// 最后叠加余下的文字g.String(), tempX, tempY);tempX = tempX + maxLen + XMOVE;}}yz = yz + maxHigh + YMOVE;}g.dispose();String uuid = UUID.randomUUID().toString();// 输出图片FileOutputStream outImgStream = new FileOutputStream(outImgPath);InputStream inputStream = new FileInputStream(outImgPath);ImageIO.write(bufImg, "jpg", outImgStream);outImgStream.flush();outImgStream.close();System.out.println("打印成功");//调用上传接口MinioClient minioClient = new MinioClient("47.108.116.186:9500","minioadmin","minioadmin");minioClient.putObject("ukt-admin", uuid, inputStream, inputStream.available(), "image/jpeg");System.out.println(uuid);return uuid;} catch (Exception e) {e.printStackTrace();return null;}}public int getCharLen(char c, Graphics2D g) {Font()).charWidth(c);}/*** 获取水印文字总长度** @paramwaterMarkContent水印的文字* @paramg* @return水印文字总长度*/public int getWatermarkLength(String waterMarkContent, Graphics2D g) {Font()).CharArray(), 0, waterMarkContent.length());}//    public static void main(String[] args) {
//        // 原图位置, 输出图片位置, 水印文字颜色, 水印文字
//        String font = "www.uktian";
//        new WaterMark().waterPress("C:/Users/Administrator/Pictures/Saved Pictures/营业执照副本.jpg",
//                "C:/Users/Administrator/Pictures/Saved Pictures/营业执照副本-测试水印.jpg", ay, 30, font);
//    }}

查询代码中直接调用下载图片工具类


代码:

String uuid =  new DownloadFile().Uuid()),tbf+"营业执照"+timestamp+".jpg");

最后再拼接返回图片连接,记住在下载图片和添加水印后需要返回一个UUID,同时为了能过预览图片,需要将图片上传到文件服务器

还有一个不要忘记了,我们在下载图片的时候需要创建一个临时存放图片的位置,这个位置不能写死在代码中,最好是配置在配置文件中,然后从方法中区获取配置文件的路径,如下:

配置文件:

引用配置文件

 @Value("${dbv.tbf}")private String tbf;

使用文件路径:

最好图片显示额效果图如下

以上为个人笔记,分享出来,大家一起参考,有不到之处请多多指教

本文发布于:2024-01-31 06:35:00,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170665410326281.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23