這篇文章給大家分享的是java給圖片加水印的方法,相信大部分人都還沒學會這個技能,為了讓大家更加了解,給大家總結了以下內容,話不多說,一起往下看吧。
具體代碼:
// 水印透明度
private static float alpha = 0.5f;
/**
* 給圖片添加水印圖片、可設置水印圖片旋轉角度
*
* @param iconPath 水印圖片路徑
* @param srcImgPath 源圖片路徑
* @param location 水印圖片位置
* @param degree 水印圖片旋轉角度
*/
public static void markImageByIcon(HttpServletRequest request, HttpServletResponse response,
String iconPath, String srcImgPath, String location, Integer degree) {
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);
// 得到畫筆對象
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);
} else {
//根據三角形相關定理,計算對角線角度
double lengthOfDiagonal = Math.sqrt(buffImg.getWidth() * buffImg.getWidth() + buffImg.getHeight() * buffImg.getHeight());
double v = (Math.pow(buffImg.getWidth(), 2) + Math.pow(lengthOfDiagonal, 2) - Math.pow(buffImg.getHeight(), 2)) / (2 * buffImg.getWidth() * lengthOfDiagonal);
double acos = Math.acos(v);
double myDegree = Math.toDegrees(acos);
g.rotate(-Math.toRadians(myDegree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
}
// 水印圖片的路徑 水印圖片一般為gif或者png的,這樣可設置透明度
ImageIcon imgIcon = new ImageIcon(iconPath);
Image img = imgIcon.getImage();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
// 水印圖片的位置
int x = 0, y = 0;
if (StringUtils.equals(location, "left-top")) {
x = 30;
y = 30;
} else if (StringUtils.equals(location, "right-top")) {
x = buffImg.getWidth() - img.getWidth(null) - 30;
y = 30;
} else if (StringUtils.equals(location, "left-bottom")) {
x += 30;
y = buffImg.getHeight() - img.getHeight(null) - 30;
} else if (StringUtils.equals(location, "right-bottom")) {
x = buffImg.getWidth() - img.getWidth(null) - 30;
y = buffImg.getHeight() - img.getHeight(null) - 30;
} else {
x = (buffImg.getWidth() - img.getWidth(null)) / 2;
y = (buffImg.getHeight() - img.getHeight(null)) / 2;
}
g.drawImage(img, x, y, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
// os = new FileOutputStream(targerPath);
os = response.getOutputStream();
ImageIO.write(buffImg, "JPG", os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}smbfile時也可以寫成這樣:
// 展示時 添加水印
public void showRemarkImage(String filePath, String iconPath, HttpServletRequest request, HttpServletResponse response) {
InputStream is = null;
OutputStream os = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
SmbFile smbFile = new SmbFile(filePath);
is = smbFile.getInputStream();
os = response.getOutputStream();
Image srcImg = ImageIO.read(is);
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 得到畫筆對象
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);
// 旋轉角度處理(根據三角形相關定理,計算對角線角度)
// double lengthOfDiagonal = Math.sqrt(buffImg.getWidth() * buffImg.getWidth() + buffImg.getHeight() * buffImg.getHeight());
// double v = (Math.pow(buffImg.getWidth(), 2) + Math.pow(lengthOfDiagonal, 2) - Math.pow(buffImg.getHeight(), 2)) / (2 * buffImg.getWidth() * lengthOfDiagonal);
// double acos = Math.acos(v);
// double myDegree = Math.toDegrees(acos);
// g.rotate(-Math.toRadians(myDegree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
// 水印圖片的路徑 水印圖片一般為gif或者png的,這樣可設置透明度
ImageIcon imgIcon = new ImageIcon(iconPath);
Image img = imgIcon.getImage();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
// 水印圖片居中
int x = 0, y = 0;
x = (buffImg.getWidth() - img.getWidth(null)) / 2;
y = (buffImg.getHeight() - img.getHeight(null)) / 2;
g.drawImage(img, x, y, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
ImageIO.write(buffImg, "png", out);
byte[] b = out.toByteArray();
response.addHeader("Content-Length", String.valueOf(b.length));
os.write(b, 0, b.length);
os.flush();
} catch (Exception e) {
LogUtil.error("附件下載失敗,時間:" + DateUtil.formatToDateTimeText(new Date()) + "原因:" + e.getMessage());
// throw new ApplicationException("文件讀取失?。?quot; + e.getMessage(), e);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(out);
}
}以上就是java給圖片加水印的方法,代碼詳細清楚,如果在日常工作遇到這個問題,希望你能通過這篇文章解決問題。如果想了解更多相關內容,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。