水印開發是web開發中比較常見的功能,實現的程式碼很簡單,這篇文章主要介紹了JAVA圖片水印開發案例,具有一定的參考價值,有興趣的小夥伴們可以參考一下
寫在最前面
上週零零碎碎花了一週的時間研究水印的開發,現在終於寫了個入門級的Demo,做下筆記同時分享出來供大家參考。
Demo是在我上次寫的 JAVA實用案例之文件導入導出(POI方式) 框架基礎上搭建的,基於Spring+SpringMVC。如果有錯誤還請大家指正。
簡單介紹
水印開發是web開發中比較常見的功能,實現的程式碼很簡單,具體的實作步驟我也會以代碼為基礎詳細講述。其實以我個人的理解,我把水印的類型和開發流程分成以下幾種。
水印的類型:
單文字浮水印
單一圖片浮水印
多文字浮水印
多重圖片浮水印
水印的開發流程:
#建立圖片快取物件
#建立Java繪圖工具物件
使用繪圖工具工具物件將原圖繪製到快取圖片物件
使用繪圖工具物件將浮水印(文字/圖片)繪製到快取圖片
#建立影像編碼工具類別
使用影像編碼工具類,輸出快取影像到目標檔案
效果圖:
# 上傳頁:
原圖:
單一文字浮水印:
#單一圖片浮水印:
多重文字浮水印:
多圖片浮水印:
##單文字浮水印開發
所謂但文字浮水印,就是在一張圖片上加上一條文字浮水印。其中我們主要的流程是透過ImageIO工具類解碼對應的圖片,然後創建BufferImage對象,透過BufferImage對象建立Graphics2D對象,再透過Graphics2D物件繪製原圖到BufferImage物件。然後,我們還可以使用Graphics2D物件來設定浮水印的相關訊息,例如浮水印內容、字體大小、字體風格等。 這裡要說明的是我們需要計算水印文字的寬度,中文長度即文字寬度,英文長度為文字寬度的二分之一。具體可以參考我原始碼中的相關內容。
//计算水印文本长度 //1、中文长度即文本长度 2、英文长度为文本长度二分之一 public 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; }
//添加单条文字水印方法 public String textWaterMark(MultipartFile myFile,String imageFileName) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 700; try { //使用ImageIO解码图片 Image image = ImageIO.read(myFile.getInputStream()); //计算原始图片宽度长度 int width = image.getWidth(null); int height = image.getHeight(null); //创建图片缓存对象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //创建java绘图工具对象 Graphics2D graphics2d = bufferedImage.createGraphics(); //参数主要是,原图,坐标,宽高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //使用绘图工具将水印绘制到图片上 //计算文字水印宽高值 int waterWidth = FONT_SIZE*getTextLength(MARK_TEXT); int waterHeight = FONT_SIZE; //计算水印与原图高宽差 int widthDiff = width-waterWidth; int heightDiff = height-waterHeight; //水印坐标设置 if (X > widthDiff) { X = widthDiff; } if (Y > heightDiff) { Y = heightDiff; } //水印透明设置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); //纵坐标在下方,不增加字体高度会靠上 graphics2d.drawString(MARK_TEXT, X, Y+FONT_SIZE); graphics2d.dispose(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); //创建图像编码工具类 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用图像编码工具类,输出缓存图像到目标文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; }
#單一圖片浮水印開發
單一圖片浮水印和上面單文字的程式碼流程大致一致,這裡只講解不同之處。首先我們需要獲得水印圖片的路徑,然後創建水印文件對象,同樣透過ImageIO工具類解碼水印圖片,中間我們就不需要計算文本長寬了,因為單文字中的長寬即是我們水印圖片的長寬。
//水印图片路径 //水印坐标设置 String logoPath = "/img/logo.png"; String realPath = request.getSession().getServletContext().getRealPath(logoPath); File logo = new File(realPath); Image imageLogo = ImageIO.read(logo); int widthLogo = imageLogo.getWidth(null); int heightLogo = imageLogo.getHeight(null); int widthDiff = width-widthLogo; int heightDiff = height-heightLogo; //水印坐标设置 if (X > widthDiff) { X = widthDiff; } if (Y > heightDiff) { Y = heightDiff; } //水印透明设置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.drawImage(imageLogo, X, Y, null);
多文字浮水印開發
#其實多文字浮水印開發和單文字也是類似的,主要的不同點是我們需要將BufferImage物件進行旋轉。因為繪製浮水印並不支援旋轉浮水印繪製,所以我們需要對原圖進行旋轉繪製,然後透過循環,我們就可以將一個文字浮水印多次繪製在原圖上了。//旋转原图,注意旋转角度为弧度制。后面两个参数为旋转的坐标中心 graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); int x = -width/2; int y = -height/2; while(x < width*1.5){ y = -height/2; while(y < height*1.5){ graphics2d.drawString(MARK_TEXT, x, y); y+=waterHeight+100; } x+=waterWidth+100; }
多重圖片浮水印開發
與上文相同,多圖片浮水印需要先讀取浮水印圖片,然後對浮水印設定透明度,在對原圖進行旋轉,然後透過循環,我們可以將一個圖片水印多次繪製在原始圖上。//水印图片路径 String logoPath = "/img/logo.png"; String realPath = request.getSession().getServletContext().getRealPath(logoPath); File logo = new File(realPath); Image imageLogo = ImageIO.read(logo); int widthLogo = imageLogo.getWidth(null); int heightLogo = imageLogo.getHeight(null); //水印透明设置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); int x = -width/2; int y = -height/2; while(x < width*1.5){ y = -height/2; while(y < height*1.5){ graphics2d.drawImage(imageLogo, x, y, null); y+=heightLogo+100; } x+=widthLogo+100; }
#
import java.awt.AlphaComposite; import java.awt.Color; 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.InputStream; import java.io.OutputStream; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.allan.service.WaterMarkService; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; @Service public class WaterMarkServiceImpl implements WaterMarkService{ //定义上传的文件夹 private static final String UPLOAD_PATH = "E:/save"; //定义水印文字样式 private static final String MARK_TEXT = "小卖铺的老爷爷"; private static final String FONT_NAME = "微软雅黑"; private static final int FONT_STYLE = Font.BOLD; private static final int FONT_SIZE = 60; private static final Color FONT_COLOR = Color.black; private static final float ALPHA = 0.3F; //1、上传图片 public String uploadImage(MultipartFile myFile,String imageFileName) { InputStream is =null; OutputStream os =null; try{ is = myFile.getInputStream(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); byte[] buffer =new byte[1024]; int len = 0; while ((len=is.read(buffer))>0){ os.write(buffer); } }catch(Exception e){ e.printStackTrace(); }finally{ if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e2) { e2.printStackTrace(); } } } return "success"; } //添加单条文字水印 public String textWaterMark(MultipartFile myFile,String imageFileName) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 700; try { Image image = ImageIO.read(myFile.getInputStream()); //计算原始图片宽度长度 int width = image.getWidth(null); int height = image.getHeight(null); //创建图片缓存对象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //创建java绘图工具对象 Graphics2D graphics2d = bufferedImage.createGraphics(); //参数主要是,原图,坐标,宽高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //使用绘图工具将水印绘制到图片上 //计算文字水印宽高值 int waterWidth = FONT_SIZE*getTextLength(MARK_TEXT); int waterHeight = FONT_SIZE; //计算水印与原图高宽差 int widthDiff = width-waterWidth; int heightDiff = height-waterHeight; //水印坐标设置 if (X > widthDiff) { X = widthDiff; } if (Y > heightDiff) { Y = heightDiff; } //水印透明设置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.drawString(MARK_TEXT, X, Y+FONT_SIZE); graphics2d.dispose(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); //创建图像编码工具类 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用图像编码工具类,输出缓存图像到目标文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } //添加单图片水印 public String imageWaterMark(MultipartFile myFile,String imageFileName,HttpServletRequest request) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 763; try { Image image = ImageIO.read(myFile.getInputStream()); //计算原始图片宽度长度 int width = image.getWidth(null); int height = image.getHeight(null); //创建图片缓存对象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //创建java绘图工具对象 Graphics2D graphics2d = bufferedImage.createGraphics(); //参数主要是,原图,坐标,宽高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //水印图片路径 String logoPath = "/img/logo.png"; String realPath = request.getSession().getServletContext().getRealPath(logoPath); File logo = new File(realPath); Image imageLogo = ImageIO.read(logo); int widthLogo = imageLogo.getWidth(null); int heightLogo = imageLogo.getHeight(null); int widthDiff = width-widthLogo; int heightDiff = height-heightLogo; //水印坐标设置 if (X > widthDiff) { X = widthDiff; } if (Y > heightDiff) { Y = heightDiff; } //水印透明设置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.drawImage(imageLogo, X, Y, null); graphics2d.dispose(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); //创建图像编码工具类 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用图像编码工具类,输出缓存图像到目标文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } //添加多条文字水印 public String moreTextWaterMark(MultipartFile myFile,String imageFileName) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 763; try { Image image = ImageIO.read(myFile.getInputStream()); //计算原始图片宽度长度 int width = image.getWidth(null); int height = image.getHeight(null); //创建图片缓存对象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //创建java绘图工具对象 Graphics2D graphics2d = bufferedImage.createGraphics(); //参数主要是,原图,坐标,宽高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //使用绘图工具将水印绘制到图片上 //计算文字水印宽高值 int waterWidth = FONT_SIZE*getTextLength(MARK_TEXT); int waterHeight = FONT_SIZE; //水印透明设置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); int x = -width/2; int y = -height/2; while(x < width*1.5){ y = -height/2; while(y < height*1.5){ graphics2d.drawString(MARK_TEXT, x, y); y+=waterHeight+100; } x+=waterWidth+100; } graphics2d.dispose(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); //创建图像编码工具类 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用图像编码工具类,输出缓存图像到目标文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } //多图片水印 public String moreImageWaterMark(MultipartFile myFile,String imageFileName,HttpServletRequest request) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 763; try { Image image = ImageIO.read(myFile.getInputStream()); //计算原始图片宽度长度 int width = image.getWidth(null); int height = image.getHeight(null); //创建图片缓存对象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //创建java绘图工具对象 Graphics2D graphics2d = bufferedImage.createGraphics(); //参数主要是,原图,坐标,宽高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //水印图片路径 String logoPath = "/img/logo.png"; String realPath = request.getSession().getServletContext().getRealPath(logoPath); File logo = new File(realPath); Image imageLogo = ImageIO.read(logo); int widthLogo = imageLogo.getWidth(null); int heightLogo = imageLogo.getHeight(null); //水印透明设置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); int x = -width/2; int y = -height/2; while(x < width*1.5){ y = -height/2; while(y < height*1.5){ graphics2d.drawImage(imageLogo, x, y, null); y+=heightLogo+100; } x+=widthLogo+100; } graphics2d.dispose(); os = new FileOutputStream(UPLOAD_PATH+"/"+imageFileName); //创建图像编码工具类 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用图像编码工具类,输出缓存图像到目标文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } //计算水印文本长度 //1、中文长度即文本长度 2、英文长度为文本长度二分之一 public 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; } }
以上是Java如何為圖片加上浮水印的範例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!