java 實現給圖片添加浮水印,java圖片添加浮水印
原圖如下:
代碼如下:
package test;import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class ImageMark {/** * 添加文字浮水印 * @param targetImg 靶心圖表片路徑,如:D://fj.jp * @param pressText 浮水印文字, 如:XingKong22star * @param fontName 字型名稱, 如:宋體 * @param fontStyle 字型樣式,如:粗體和斜體(Font.BOLD|Font.ITALIC) * @param fontSize 字型大小,單位為像素 * @param color 字型顏色 * @param x 浮水印文字距離靶心圖表片左側的位移量,如果x<0, 則在正中間 * @param y 浮水印文字距離靶心圖表片上側的位移量,如果y<0, 則在正中間 * @param alpha 透明度(0.0 -- 1.0, 0.0為完全透明,1.0為完全不透明) */ public static boolean createMark(String filePath,String newFilePath) {float watermarkAlpha = (float) 0.5;// 讀取原圖片ImageIcon imgIcon = new ImageIcon(filePath);Image theImg = imgIcon.getImage();int width = theImg.getWidth(null);int height = theImg.getHeight(null);// 建立一個和原圖片同大小的新空白圖片BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = bimage.createGraphics();// bimage = g.getDeviceConfiguration().createCompatibleImage(width,// height, Transparency.TRANSLUCENT);// g.dispose();// g = bimage.createGraphics(); //設定字型 Font font = new Font("SansSerif", Font.BOLD, 18); g.setFont(font);// 設定前景色彩 g.setColor(Color.white);// 設定背景色g.setBackground(Color.white);// 畫原圖g.drawImage(theImg, 0, 0, null);// 值從0f-1.0fg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, watermarkAlpha));// 畫字 g.drawString("XingKong22star", imgIcon.getIconWidth() - 150 ,imgIcon.getIconHeight() - 20);// 透明度設定 結束g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));g.dispose();FileOutputStream out = null;try {String newWaterFile = newFilePath;out = new FileOutputStream(newWaterFile);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);param.setQuality(50f, true);encoder.encode(bimage, param);} catch (Exception e) {System.out.println("---產生失敗---");return false;} finally {if (out != null) {try {out.close();out = null;} catch (Exception e) {}}}System.out.println("浮水印文字載入完畢");return true;} /** * 把圖片印刷到圖片上 * * @param iconPath -- * 浮水印檔案 * @param srcImgPath -- * 目標檔案 * @param targerPath * 儲存位置 * @param x * --x座標 * @param y * --y座標 */ public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree,int x,int y) { OutputStream os = null; try { Image srcImg = ImageIO.read(new File(srcImgPath)); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); // 得到畫筆對象 // Graphics g= buffImg.getGraphics(); Graphics2D g = buffImg.createGraphics(); // 設定對線段的鋸齒狀邊緣處理 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg .getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); if (null != degree) { // 設定浮水印旋轉 g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg .getHeight() / 2); } // 浮水印圖象的路徑 浮水印一般為gif或者png的,這樣可設定透明度 ImageIcon imgIcon = new ImageIcon(iconPath); // 得到Image對象。 Image img = imgIcon.getImage(); float alpha = 0.5f; // 透明度 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 表示浮水印圖片的位置 g.drawImage(img, x, y, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); g.dispose(); os = new FileOutputStream(targerPath); // 產生圖片 ImageIO.write(buffImg, "JPG", os); System.out.println("圖片完成添加Icon。。。。。。"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) {String filePath = "D:/fj.jpg";String newFilePath = "D:/fj_water.jpg";ImageMark.createMark(filePath,newFilePath); String srcImgPath = "D:/fj.jpg"; String iconPath = "D:/3.png"; String targerPath = "D:/markImageByIcon.jpg"; // 給圖片添加浮水印 ImageMark.markImageByIcon(iconPath, srcImgPath, targerPath, null, 800, 730);}}
運行結果如下:
浮水印文字效果:
圖片浮水印效果
浮水印圖片:
添加完後:
java 給圖片加文字浮水印
用過濾器,等我上線給你,不要結問題哦
package com.jc.ts.services;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
*添加圖片浮水印的服務類
* */
public class WaterMark {
/**
* @param sizeContext添加浮水印文字
* @param request 請求流對象
* @param request 響應流對象
* */
@SuppressWarnings("deprecation")
public static void createMarkSize(String sizeContext,HttpServletRequest request,HttpServletResponse response) {
try {
String path=request.getRealPath(request.getServletPath());
FileInputStream in=new FileInputStream(path);
Image src=ImageIO.read(in);
int w=src.getWidth(null);
int h=src.getHeight(null);
BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);//構建畫板
Graphics g=img.getGraphics();//得到畫筆
g.drawImage(src,0,0,w,h,null);//把源圖片寫入畫板
g.setColor(Color.red);
g.drawString(sizeContext,10,5); // 添加文字
g.dispose();//產生圖片
JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream());
e.encode(img);
response.getO......餘下全文>>
java 給圖片加文字浮水印
用過濾器,等我上線給你,不要結問題哦
package com.jc.ts.services;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
*添加圖片浮水印的服務類
* */
public class WaterMark {
/**
* @param sizeContext添加浮水印文字
* @param request 請求流對象
* @param request 響應流對象
* */
@SuppressWarnings("deprecation")
public static void createMarkSize(String sizeContext,HttpServletRequest request,HttpServletResponse response) {
try {
String path=request.getRealPath(request.getServletPath());
FileInputStream in=new FileInputStream(path);
Image src=ImageIO.read(in);
int w=src.getWidth(null);
int h=src.getHeight(null);
BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);//構建畫板
Graphics g=img.getGraphics();//得到畫筆
g.drawImage(src,0,0,w,h,null);//把源圖片寫入畫板
g.setColor(Color.red);
g.drawString(sizeContext,10,5); // 添加文字
g.dispose();//產生圖片
JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream());
e.encode(img);
response.getO......餘下全文>>