특정 코드:
// 水印透明度 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(); } } }
(추천 동영상 튜토리얼: java video)
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("文件读取失败:" + e.getMessage(), e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); IOUtils.closeQuietly(out); } }
추천 튜토리얼: java 진입 프로그램
위 내용은 Java는 이미지에 이미지 워터마크 추가를 구현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!
성명
이 기사는 csdn에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
어 ass 신 크리드 그림자 : 조개 수수께끼 솔루션
2 몇 주 전ByDDD
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기

드림위버 CS6
시각적 웹 개발 도구

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음
