package com.myimooc.watermark.service;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
/**
* 圖片水印服務(wù)類
* @author ZhangCheng on 2017-07-21
*
*/
public interface MarkService {
/** 水印文字內(nèi)容 */
public static final String MARK_TEXT = "妙手空空";
/** 水印文字類型 */
public static final String FONT_NAME = "微軟雅黑";
/** 水印文字樣式 */
public static final int FONT_STYLE = Font.BOLD;
/** 水印文字大小 */
public static final int FONT_SIZE= 120;// 單位:像素
/** 水印文字顏色 */
public static final Color FONT_COLOR= Color.BLACK;
/** 水印文字位置X軸 */
public static final int X = 10;
/** 水印文字位置Y軸 */
public static final int Y = 10;
/** 水印文字透明度*/
public static final float ALPHA = 0.3F;
/** 水印圖片*/
public static final String LOGO = "logo.png";
/**
* 功能:將傳入的圖片添加水印并保存到服務(wù)器中
* @param file
* @param uploadPath
* @param realUploadPath
* @return 添加水印后圖片的URL相對地址
*/
String watermake(File imageFile,String imageFileName,String uploadPath,String realUploadPath);
}
4-5 添加單個文字水印
代碼演示
package com.myimooc.watermark.service;
import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Service;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 圖片水印服務(wù)類,添加文字水印
* @author ZhangCheng on 2017-07-21
*
*/
//@Service
@SuppressWarnings("unused")
public class TextMarkServiceImpl implements MarkService {
@Override
public String watermake(File imageFile,String imageFileName, String uploadPath, String realUploadPath) {
String logoFileName = "logo_" + imageFileName;
OutputStream os = null;
try {
Image image = ImageIO.read(imageFile);
int width = image.getWidth(null);// 原圖寬度
int height = image.getHeight(null);// 原圖高度
// 創(chuàng)建圖片緩存對象
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
// 創(chuàng)建繪繪圖工具對象
Graphics2D g = bufferedImage.createGraphics();
// 使用繪圖工具將原圖繪制到緩存圖片對象
g.drawImage(image, 0, 0, width,height,null);
// 設(shè)置水印文字字體信息
g.setFont(new Font(FONT_NAME,FONT_STYLE,FONT_SIZE));
// 設(shè)置水印文字顏色
g.setColor(FONT_COLOR);
int markWidth = FONT_SIZE * getTextLength(MARK_TEXT);
int markHeight = FONT_SIZE;
// 水印的高度和寬度之差
int widthDiff = width - markWidth;
int heightDiff = height - markHeight;
int x = X;
int y = Y;
// 判斷設(shè)置的值是否大于圖片大小
if(x > widthDiff){
x = widthDiff;
}
if(y > heightDiff){
y =heightDiff;
}
// 設(shè)置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
// 添加水印
g.drawString(MARK_TEXT, x, y + FONT_SIZE);
g.dispose();
os = new FileOutputStream(realUploadPath + "/" + logoFileName);
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
en.encode(bufferedImage);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return uploadPath + "/" + logoFileName;
}
/**
* 功能:獲取文本長度。漢字為1:1,英文和數(shù)字為2:1
*/
private int getTextLength(String text){
int length = text.length();
for(int i = 0 ; i < text.length(); i++){
String s = String.valueOf(text.charAt(i));
if(s.getBytes().length > 1){
length++;
}
}
length = length % 2 == 0 ? length / 2 : length / 2 + 1;
return length;
}
}
第五章:添加單個圖片水印5-1 圖片水印添加
代碼演示:
package com.myimooc.watermark.service;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Service;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 圖片水印服務(wù)類,添加圖片水印
* @author ZhangCheng on 2017-07-22
*
*/
//@Service
@SuppressWarnings("unused")
public class ImageMarkServiceImpl implements MarkService {
@Override
public String watermake(File imageFile, String imageFileName, String uploadPath, String realUploadPath) {
String logoFileName = "logo_" + imageFileName;
OutputStream os = null;
// 圖片地址
String logoPath = realUploadPath + "/" + LOGO;
try {
Image image = ImageIO.read(imageFile);
int width = image.getWidth(null);// 原圖寬度
int height = image.getHeight(null);// 原圖高度
// 創(chuàng)建圖片緩存對象
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
// 創(chuàng)建繪繪圖工具對象
Graphics2D g = bufferedImage.createGraphics();
// 使用繪圖工具將原圖繪制到緩存圖片對象
g.drawImage(image, 0, 0, width,height,null);
// 讀取Logo圖片
File logo = new File(logoPath);
Image imageLogo = ImageIO.read(logo);
// 獲取Logo圖片的寬度和高度
int markWidth = imageLogo.getWidth(null);
int markHeight = imageLogo.getHeight(null);
// 原圖和Logo圖片的高度和寬度之差
int widthDiff = width - markWidth;
int heightDiff = height - markHeight;
int x = X;
int y = Y;
// 判斷設(shè)置的值是否大于圖片大小
if(x > widthDiff){
x = widthDiff;
}
if(y > heightDiff){
y =heightDiff;
}
// 設(shè)置水印透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
// 添加水印
g.drawImage(imageLogo, x, y, null);
g.dispose();
os = new FileOutputStream(realUploadPath + "/" + logoFileName);
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
en.encode(bufferedImage);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return uploadPath + "/" + logoFileName;
}
}
第六章:添加多個文字水印6-1 添加多個文字水印
代碼演示:
package com.myimooc.watermark.service;
import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Service;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 圖片水印服務(wù)類,添加多個文字水印
* @author ZhangCheng on 2017-07-22
*
*/
//@Service
@SuppressWarnings("unused")
public class MoreTextMarkServiceImpl implements MarkService {
@Override
public String watermake(File imageFile, String imageFileName, String uploadPath, String realUploadPath) {
String logoFileName = "logo_" + imageFileName;
OutputStream os = null;
try {
Image image = ImageIO.read(imageFile);
int width = image.getWidth(null);// 原圖寬度
int height = image.getHeight(null);// 原圖高度
// 創(chuàng)建圖片緩存對象
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
// 創(chuàng)建繪繪圖工具對象
Graphics2D g = bufferedImage.createGraphics();
// 使用繪圖工具將原圖繪制到緩存圖片對象
g.drawImage(image, 0, 0, width,height,null);
// 設(shè)置水印文字字體信息
g.setFont(new Font(FONT_NAME,FONT_STYLE,FONT_SIZE));
// 設(shè)置水印文字顏色
g.setColor(FONT_COLOR);
int markWidth = FONT_SIZE * getTextLength(MARK_TEXT);
int markHeight = FONT_SIZE;
// 設(shè)置水印透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
// 旋轉(zhuǎn)圖片
g.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2);
int x = -width / 2;
int y = -height / 2;
int xmove = 200;// 水印之間的間隔
int ymove = 200;// 水印之間的間隔
// 循環(huán)添加
while (x < width * 1.5){
y = -height / 2;
while(y < height * 1.5){
g.drawString(MARK_TEXT, x, y);
y += markHeight + ymove;
}
x += markWidth + xmove;
}
g.dispose();
os = new FileOutputStream(realUploadPath + "/" + logoFileName);
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
en.encode(bufferedImage);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return uploadPath + "/" + logoFileName;
}
/**
* 功能:獲取文本長度。漢字為1:1,英文和數(shù)字為2:1
*/
private int getTextLength(String text){
int length = text.length();
for(int i = 0 ; i < text.length(); i++){
String s = String.valueOf(text.charAt(i));
if(s.getBytes().length > 1){
length++;
}
}
length = length % 2 == 0 ? length / 2 : length / 2 + 1;
return length;
}
}
第七章:添加多個圖片水印7-1 添加多個圖片水印
代碼演示:
package com.myimooc.watermark.service;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Service;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 圖片水印服務(wù)類,添加多個圖片水印
* @author ZhangCheng on 2017-07-22
*
*/
@Service
public class MoreImageMarkServiceImpl implements MarkService {
@Override
public String watermake(File imageFile, String imageFileName, String uploadPath, String realUploadPath) {
String logoFileName = "logo_" + imageFileName;
OutputStream os = null;
try {
Image image = ImageIO.read(imageFile);
int width = image.getWidth(null);// 原圖寬度
int height = image.getHeight(null);// 原圖高度
// 創(chuàng)建圖片緩存對象
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
// 創(chuàng)建繪繪圖工具對象
Graphics2D g = bufferedImage.createGraphics();
// 使用繪圖工具將原圖繪制到緩存圖片對象
g.drawImage(image, 0, 0, width,height,null);
// 圖片地址
String logoPath = realUploadPath + "/" + LOGO;
// 讀取Logo圖片
File logo = new File(logoPath);
Image imageLogo = ImageIO.read(logo);
// Logo圖片的寬度和高度
int markWidth = imageLogo.getWidth(null);
int markHeight = imageLogo.getHeight(null);
// 設(shè)置水印透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
// 旋轉(zhuǎn)圖片
g.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2);
int x = -width / 2;
int y = -height / 2;
int xmove = 200;// 水印之間的間隔
int ymove = 200;// 水印之間的間隔
// 循環(huán)添加
while (x < width * 1.5){
y = -height / 2;
while(y < height * 1.5){
// 添加水印
g.drawImage(imageLogo, x, y, null);
y += markHeight + ymove;
}
x += markWidth + xmove;
}
g.dispose();
os = new FileOutputStream(realUploadPath + "/" + logoFileName);
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
en.encode(bufferedImage);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return uploadPath + "/" + logoFileName;
}
}