/** * 为图片添加水印 * * @param input 图片文件本地路径 * @param output * @param waterText * @throws IOException * @throws PanshengException */ public static void watermarkImage(String input, String output, List<String> waterText) throws IOException, PanshengException { File srcImageFile = new File(input); if (!srcImageFile.exists()) throw new PanshengException("源图片文件不存在:" + input); Image srcImage = ImageIO.read(srcImageFile); int srcWidth = srcImage.getWidth(null);//获取图片的宽 int srcHeight = srcImage.getHeight(null);//获取图片的高 //原图片 BufferedImage bufferedImage = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB); //得画笔 Graphics2D g = bufferedImage.createGraphics(); //设置对线段的锯齿状边缘处理 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(srcImage, 0, 0, srcWidth, srcHeight, null); //设置倾斜角度 g.rotate(Math.toRadians(-30), (double) bufferedImage.getWidth() / 2, (double) bufferedImage .getHeight() / 2); //设置颜色和字体 g.setColor(Color.LIGHT_GRAY); g.setFont(new Font("宋体",Font.BOLD,20)); //设置文字的透明度 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.3f)); //水印的坐标 int x=srcWidth/3; int y=srcHeight/9; //画水印 int waterSize = 0; for (int j = 7; j > -1; j--) { if (waterSize >= waterText.size()) { waterSize = 0; } for (int k = 2; k > -1; k--) { if (j % 2 != 0) { g.drawString(waterText.get(waterSize), x * k + x / 4, y * j + y / 4); } else { g.drawString(waterText.get(waterSize), x * k + x / 6, y * j + y / 4); } } waterSize++; } //释放资源 g.dispose(); //向目标路径输出图像 FileOutputStream outputStream = new FileOutputStream(output); ImageIO.write(bufferedImage, "jpg", outputStream); outputStream.flush(); outputStream.close(); }
给图片添加文字水印并铺满图片
猜你喜欢
转载自blog.csdn.net/donghua19900508/article/details/79639417
今日推荐
周排行