您当前的位置:网站首页>JAVA>java实现给图片添加图片水印 找Java教程就上ki4.cc (>^ω^<),java,图片,图片水印

java实现给图片添加图片水印 找Java教程就上ki4.cc (>^ω^<),java,图片,图片水印

2020年05月11日 投稿作者: 围观人数:29

java实现将大写字母转换为小写字母 找Java教程就上ki4.cc (>^ω^<)

java实现将大写字母转换为小写字母的方法:可以用toLowerCase()函数进行转换,如【String str = "AbC"; str.toLowerCase());】。

具体代码:

 // 水印透明度
    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视频)

java中servlet容器有什么作用 找Java教程就上ki4.cc (>^ω^<)

java中servlet容器的作用:1、编译jsp,jsp被服务器jsp引擎编译成servlet;2、控制转发;3、管理web应用,servlet容器可以管理多个Context容器。

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实现给图片添加图片水印的详细内容,更多请关注ki4导航其它相关文章!

jre是java的什么 找Java教程就上ki4.cc (>^ω^<)

jre是java的运行时环境。jdk、jvm和jre共同构成了用于开发和运行java应用程序的java平台组件的强大功能。jre是一个在计算机操作系统之上运行的软件层,提供特定于java的附加服务。

标签

版权说明
免责声明:本文文章内容由ki4导航网发布,但不代表本站的观点和立场,具体内容可自行甄别.