主要用JAVA 的 awt 給FTP圖片添加水印
添加水印核心方法
/**
* 添加水印核心方法
* @param bufImg
* @param img
* @param text
* @param font
* @param color
* @param x
* @param y
*/
private static void mark(BufferedImage bufImg, Image img, List<String> text, Font font, Color color, int x, List<Integer> y) {
Graphics2D g = bufImg.createGraphics();
g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);
g.setColor(color);
g.setFont(font);
if( text != null && text.size() > 0){
for (int i = 0; i < text.size(); i++) {
g.drawString(text.get(i), x, y.get(i));
}
}
g.dispose();
}
給圖片添加文字水印
/**
* 給圖片增加文字水印
*
* @param inputStream
* -文件的IO流
* @param longitude
* -經度
* @param latitude
* -緯度
* @param address
* -地址
* @param currentTime
* -時間
* @param color
* -顏色
*/
private static InputStream mark(InputStream inputStream, String longitude ,String latitude,String address,String currentTime, Color color) {
try {
Image img = ImageIO.read(inputStream);
// 讀取原圖片信息
int imgWidth = img.getWidth(null);
int imgHeight = img.getHeight(null);
int x = 50;
// 加水印
BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
Font font = new Font("宋體", Font.PLAIN, 40);
java.util.List<String> textList = Lists.newArrayList();
textList.add("經度: "+longitude);
textList.add("緯度: "+latitude);
textList.add("地址: "+address);
textList.add("時間: "+currentTime);
List<Integer> yList = Lists.newArrayList();
yList.add(imgHeight-180);
yList.add(imgHeight-130);
yList.add(imgHeight-80);
yList.add(imgHeight-30);
mark(bufImg, img, textList, font, color, x, yList);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(bufImg, "jpg", imOut);
InputStream is = new ByteArrayInputStream(bs.toByteArray());
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
編輯FTP圖片
/**
* 上傳文件
*
* @param pathname 路徑名稱
* @param fileName 文件名稱
* @param longitude 經度
* @param latitude 緯度
* @param address 地址
* @param currentTime 時間
* @return
*/
public static boolean waterMark(String pathname, String fileName, String longitude , String latitude, String address, String currentTime) {
InputStream inputStream = null;
InputStream is = null;
FTPClient ftpClient = null;
try {
ftpClient = initFtpClient();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
CreateDirecroty(pathname,ftpClient);
ftpClient.makeDirectory(pathname);
ftpClient.changeWorkingDirectory(pathname);
ftpClient.enterLocalPassiveMode();
log.info("開始添加水印,路徑:{},文件名:{}", pathname, fileName);
inputStream = ftpClient.retrieveFileStream(fileName);
is = mark(inputStream, longitude,latitude,address,currentTime, Color.WHITE);
//操作多文件時候,必須調用completePendingCommand釋放,否則FTP會斷開連接
ftpClient.completePendingCommand();
ftpClient.storeFile(attach.getAttachFilename(), is);
ftpClient.logout();
log.info("水印添加成功");
} catch (Exception e) {
log.info("水印添加失敗");
e.printStackTrace();
return false;
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* 初始化ftp服務器
*/
private static FTPClient initFtpClient() {
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("utf-8");
try {
log.info("connecting...ftp服務器:" + HOST_NAME + ":" + PORT);
//連接ftp服務器
ftpClient.connect(HOST_NAME, PORT);
//登錄ftp服務器
ftpClient.login(USERNAME, PASSWORD);
//是否成功登錄服務器
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
log.info("connect failed...ftp服務器:" + HOST_NAME + ":" + PORT);
} else {
log.info("connect successful...ftp服務器:" + HOST_NAME + ":" + PORT);
}
ftpClient.setBufferSize(bufferSize);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ftpClient;
}
方法在WINDOWS下運行,簡直六到飛起,一部署到docker中,中文就亂碼!!!
各種給LIUNX系統中加字體都沒有解決,最後是通過Dockerfile添加字體,在容器內部直接加載字體,OK,完美解決。
首先在Dockerfile中創建fonts目錄,將需要的字體放入該目錄下,WINDOWS下字體在 C:\Windows\Fonts 目錄下。
COPY fonts /usr/share/fonts/
RUN yum -y install fontconfig \
&& cd /usr/share/fonts \
&& fc-cache
Dockerfile文件追加這部分內容,中文亂碼完全OK~~~
最後貼張效果圖