PSD格式控件Aspose.PSD功能演示:使用 Java 裁剪或旋转 PSD 图像

PSD图像文件用于为徽标、横幅、小册子和许多其他目的设计图片。在某些情况下,您可能需要在 Java 应用程序中裁剪或旋转 PSD 文件。本文介绍了如何使用 Java 以编程方式裁剪或旋转 PSD 文件。

PSD图像文件用于为徽标、横幅、小册子和许多其他目的设计图片。在某些情况下,您可能需要在 Java 应用程序中裁剪或旋转 PSD 文件。本文介绍了如何使用 Java 以编程方式裁剪或旋转 PSD 文件。您需要配置Aspose.PSD for Java API才能使用几种受支持的文件格式。如果你还没有用过Aspose.PSD可以点击这里下载最新版测试。

使用 Java 使用 Shift 方法裁剪 PSD 图像

您可以根据需要通过移动内容来裁剪 PSD 图像。请按照以下步骤通过移动内容裁剪图像:

  • 将输入 PSD 文件加载到RasterImage类对象中。
  • 使用cacheData方法缓存图像。
  • 定义移位值并将它们传递给crop方法。
  • 保存输出裁剪的图像。

以下代码显示了如何通过使用 Java 以编程方式移动内容来裁剪 PSD 图像:

// Load an existing image into an instance of RasterImage classRasterImage rasterImage = (RasterImage)Image.load("Test.psd");// Before cropping, the image should be cached for better performanceif (!rasterImage.isCached()){    rasterImage.cacheData();}// Define shift values for all four sidesint leftShift = 10;int rightShift = 10;int topShift = 10;int bottomShift = 10;// Based on the shift values, apply the cropping on image.// Crop method will shift the image bounds toward the center of image and Save the results to diskrasterImage.crop(leftShift, rightShift, topShift, bottomShift);// Save output in JPEG,PSD or any other format.rasterImage.save("output.jpg", new JpegOptions());rasterImage.save("output.psd", new PsdOptions());

在 Java 中使用矩形坐标裁剪 PSD 图像

您可以通过指定坐标来裁剪 PSD 图像中的任何矩形区域。您需要按照以下步骤裁剪 PSD 图像:

  • 将现有图像加载到RasterImage类的实例中。
  • 缓存图像以获得更好的性能。
  • 创建具有所需大小的Rectangle类实例。
  • 使用裁剪方法裁剪图像并保存输出文件。

下面的代码片段演示了如何使用 Java 裁剪具有直角坐标的 PSD 文件:

String sourceFile = "sample.psd";String destName = "Cropping-PSD_out.jpg";// Load an existing image into an instance of RasterImage classRasterImage rasterImage = (RasterImage)Image.load(sourceFile); // Cache the image for better performanceif (!rasterImage.isCached()){   rasterImage.cacheData();}// Create an instance of Rectangle class with desired size.Rectangle rectangle = new Rectangle(20, 20, 20, 20);// Perform the crop operation on object of Rectangle classrasterImage.crop(rectangle);// Save the results to diskrasterImage.save(destName, new JpegOptions());

使用 Java 以任意角度旋转 PSD 图像

在某些情况下,您可能需要以特定角度旋转 PSD 图像。请按照以下步骤旋转 PSD 图像:

  • 使用RasterImage类加载源 PSD 图像文件。
  • 在旋转之前,应缓存图像以提高性能。
  • 在旋转方法中指定旋转角度。
  • 保存输出文件。

下面的代码示例解释了如何使用 Java 旋转 PSD 图像:

String sourceFile = "sample.psd";String destName = "Rotate-PSD_out.jpg";  // Load input PSD image fileRasterImage image = (RasterImage)Image.load(sourceFile);// Before rotation, the image should be cached for better performanceif (!image.isCached()){    image.cacheData();}// Rotate the PSD image on 20 degree angle while keeping the image size proportional with red background colorimage.rotate(20f, true, Color.getRed());// Save the result to a new fileimage.save(destName, new JpegOptions());

如果你想试用Aspose的全部完整功能,可 联系在线客服获取30天临时授权体验。


如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年10月1日
下一篇 2021年10月1日

相关推荐

发表回复

登录后才能评论