- 将水印添加到Java中的图像
- 将对角水印添加到Java中的图像
为了给图像添加水印,我们将使用Aspose.Imaging,它是一种图像处理API,可让您从Java应用程序中处理多种图像格式。,还没使用过的朋友可以最新版
整合所有格式的Aspose.Total永久授权正在火热促销中,联系客服立马1分钟了解全部咨询!
使用Java将水印添加到图像
以下是使用Aspose.Imaging向图像添加水印的步骤。
- 使用Image类加载图像文件。
- 创建Graphics类的对象,并使用Image对象对其进行初始化。
- 创建并初始化Font和SolidBrush对象。
- 使用Graphics.drawString(String s,Font font,Brush brush,float x,float y)方法添加水印。
- 使用Image.save()方法保存图像。
下面的代码示例演示如何使用Java向图像添加水印。
// Load imagecom.aspose.imaging.Image image = com.aspose.imaging.Image.load("image.png");// Create and initialize an instance of Graphics classGraphics graphics= new Graphics(image);// Creates an instance of FontFont font = new Font("Times New Roman", 16, FontStyle.Bold);// Create an instance of SolidBrush and set its propertiesSolidBrush brush = new SolidBrush();brush.setColor(Color.getBlack());brush.setOpacity(100);// Draw a string using the SolidBrush and Font objects at specific pointgraphics.drawString("Watermark by Aspose.Imaging for Java", font, brush, new PointF(image.getWidth()-100, image.getHeight()-100));// Save imageimage.save("watermarked-image.png");
将对角水印添加到Java中的图像
在某些情况下,水印对角地应用于图像。对于这种情况,Aspose.Imaging for Java提供了水印转换选项,您可以使用这些选项旋转水印。以下是向图像添加对角水印的步骤。
- 使用Image类加载图像文件。
- 创建Graphics类的对象,并使用Image对象对其进行初始化。
- 创建并初始化Font和SolidBrush对象。
- 在尺寸对象中获取图像尺寸。
- 创建一个新的Matrix对象,并将平移和变换设置为所需的角度。
- 使用Graphics.setTransform(Matrix)方法设置水印的转换。
- 使用Graphics.drawString(String s,Font font,Brush brush,float x,float y)方法添加水印。
- 使用Image.save()方法保存图像。
以下代码示例显示了如何使用Java向图像添加对角水印。
// Load imagecom.aspose.imaging.Image image = com.aspose.imaging.Image.load("image.png");// Create and initialize an instance of Graphics classGraphics graphics= new Graphics(image);// Creates an instance of FontFont font = new Font("Times New Roman", 16, FontStyle.Bold);// Create an instance of SolidBrush and set its propertiesSolidBrush brush = new SolidBrush();brush.setColor(Color.getBlack());brush.setOpacity(100);Size sz = graphics.getImage().getSize(); // Create an object of Matrix class for transformationMatrix matrix = new Matrix();// First a translation then a rotation matrix.translate(sz.getWidth() / 2, sz.getHeight() / 2); matrix.rotate(-45.0f);// Set the Transformation through Matrixgraphics.setTransform(matrix);// Draw a string using the SolidBrush and Font objects at specific pointgraphics.drawString("Watermark by Aspose.Imaging for Java", font, brush, 0, 0);// Save imageimage.save("watermarked-image.png");
还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!