使用现代 API 增强图像处理

介绍

过去,Aspose Slides 依赖于 java.awt,并且在公共 API 中包含了以下来自该库的类:

自 24.4 版本起,此公共 API 已被标记为已弃用。

为了摆脱对这些类的依赖,我们添加了所谓的“现代 API”——即应该替代已弃用 API 的新 API,其签名不再依赖 Bitmap。Canvas 已被标记为已弃用,并且其在公共 Slides API 中的支持已被移除。

带有 System.Drawing 依赖的已弃用公共 API 的移除将在 24.8 发行版中完成。

现代 API

向公共 API 添加了以下类和枚举:

  • IImage - 表示光栅或矢量图像。
  • ImageFormat - 表示图像的文件格式。
  • Images - 用于实例化和操作 IImage 接口的方法。

请注意,IImage 可释放(它实现了 IDisposable 接口,使用时应放在 using 块中或以其他方便的方式进行释放)。

使用新 API 的典型场景如下所示:

Presentation pres = new Presentation();
try {
    IPPImage ppImage;
    // 实例化一个可释放的 IImage 实例,来自磁盘上的文件。
    IImage image = Images.fromFile("image.png");
    try {
        // 通过向演示文稿的图像集合中添加 IImage 实例来创建 PowerPoint 图像。
        ppImage = pres.getImages().addImage(image);
    } finally {
        if (image != null) image.dispose();
    }

    // 在幻灯片 #1 上添加图片形状
    pres.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);

    // 获取表示幻灯片 #1 的 IImage 实例。
    IImage slideImage = pres.getSlides().get_Item(0).getImage(new Size(1920, 1080));
    try {
        // 将图像保存到磁盘。
        slideImage.save("slide1.jpeg", ImageFormat.Jpeg);
    } finally {
        if (slideImage != null) slideImage.dispose();
    }
} finally {
    if (pres != null) pres.dispose();
}

用现代 API 替换旧代码

通常,您需要将使用 ImageIO 的旧方法调用替换为新的方法。

旧:

Presentation pres = new Presentation();
try {
    Bitmap slideImage = pres.getSlides().get_Item(0).getThumbnail(new Size(1920, 1080));
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream("image.png");
        slideImage.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
} finally {
    if (pres != null) pres.dispose();
}

新:

Presentation pres = new Presentation();
try {
    IImage slideImage = pres.getSlides().get_Item(0).getImage(new Size(1920, 1080));
    try {
        slideImage.save("image.png", ImageFormat.Png);
    } finally {
        if (slideImage != null) slideImage.dispose();
    }
} finally {
    if (pres != null) pres.dispose();
}

获取幻灯片缩略图

使用已弃用 API 的代码:

Presentation pres = new Presentation("pres.pptx");
try {
    Bitmap slideImage = pres.getSlides().get_Item(0).getThumbnail();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream("slide1.png");
        slideImage.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

Presentation pres = new Presentation("pres.pptx");
try {
    IImage slideImage = pres.getSlides().get_Item(0).getImage();
    try {
        slideImage.save("slide1.png", ImageFormat.Png);
    } finally {
        if (slideImage != null) slideImage.dispose();
    }
} finally {
    if (pres != null) pres.dispose();
}

获取形状缩略图

使用已弃用 API 的代码:

Presentation pres = new Presentation("pres.pptx");
try {
    Bitmap shapeImage = pres.getSlides().get_Item(0).getShapes().get_Item(0).getThumbnail();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream("shape.png");
        shapeImage.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

Presentation pres = new Presentation("pres.pptx");
try {
    IImage shapeImage = pres.getSlides().get_Item(0).getShapes().get_Item(0).getImage();
    try {
        shapeImage.save("shape.png");
    } finally {
        if (shapeImage != null) shapeImage.dispose();
    }
} finally {
    if (pres != null) pres.dispose();
}

获取演示文稿缩略图

使用已弃用 API 的代码:

Presentation pres = new Presentation("pres.pptx");
try {
    Bitmap[] bitmaps = pres.getThumbnails(new RenderingOptions(), new Size(1980, 1028));
    for (int index = 0; index < bitmaps.length; index++)
    {
        android.graphics.Bitmap thumbnail = bitmaps[index];
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("slide" + index + ".png");
            thumbnail.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

Presentation pres = new Presentation("pres.pptx");
try {
    IImage[] images = pres.getImages(new RenderingOptions(), new Size(1980, 1028));
    try
    {
        for (int index = 0; index < images.length; index++)
        {
            IImage thumbnail = images[index];
            thumbnail.save("slide" + index + ".png", ImageFormat.Png);
        }
    }
    finally
    {
        for (IImage image : images)
        {
            image.dispose();
        }
    }
} finally {
    if (pres != null) pres.dispose();
}

向演示文稿添加图片

使用已弃用 API 的代码:

Presentation pres = new Presentation();
try {
    IPPImage ppImage = null;
    File file = new File("image.png");
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    ppImage = pres.getImages().addImage(bitmap);

    pres.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

Presentation pres = new Presentation();
try {
    IPPImage ppImage;
    IImage image = Images.fromFile("image.png");
    try {
        ppImage = pres.getImages().addImage(image);
    } finally {
        if (image != null) image.dispose();
    }

    pres.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);
} finally {
    if (pres != null) pres.dispose();
}

将被删除的方法及其在现代 API 中的替代方案

Presentation

方法签名 替代方法签名
public final Bitmap[] getThumbnails(IRenderingOptions options) public final IImage[] getImages(IRenderingOptions options)
public final Bitmap[] getThumbnails(IRenderingOptions options, Size imageSize) public final IImage[] getImages(IRenderingOptions options, Size imageSize)
public final Bitmap[] getThumbnails(IRenderingOptions options, float scaleX, float scaleY) public final IImage[] getImages(IRenderingOptions options, float scaleX, float scaleY)
public final Bitmap[] getThumbnails(IRenderingOptions options, int[] slides) public final IImage[] getImages(IRenderingOptions options, int[] slides)
public final Bitmap[] getThumbnails(IRenderingOptions options, int[] slides, Size imageSize) public final IImage[] getImages(IRenderingOptions options, int[] slides, Size imageSize)
public final Bitmap[] getThumbnails(IRenderingOptions options, int[] slides, float scaleX, float scaleY) public final IImage[] getImages(IRenderingOptions options, int[] slides, float scaleX, float scaleY)

Shape

方法签名 替代方法签名
public final Bitmap getThumbnail() public final IImage getImage()
public final Bitmap getThumbnail(int bounds, float scaleX, float scaleY) public final IImage getImage(int bounds, float scaleX, float scaleY)

Slide

方法签名 替代方法签名
public final Bitmap getThumbnail() public final IImage getImage()
public final Bitmap getThumbnail(Size imageSize) public final IImage getImage(Size imageSize)
public final Bitmap getThumbnail(float scaleX, float scaleY) public final IImage getImage(float scaleX, float scaleY)
public final Bitmap getThumbnail(IRenderingOptions options) public final IImage getImage(IRenderingOptions options)
public final Bitmap getThumbnail(IRenderingOptions options, Size imageSize) public final IImage getImage(IRenderingOptions options, Size imageSize)
public final Bitmap getThumbnail(IRenderingOptions options, float scaleX, float scaleY) public final IImage getImage(IRenderingOptions options, float scaleX, float scaleY)
public final Bitmap getThumbnail(ITiffOptions options) public final IImage getImage(ITiffOptions options)
public final void renderToGraphics(IRenderingOptions options, Canvas graphics) Will be deleted completely
public final void renderToGraphics(IRenderingOptions options, Canvas graphics, Size renderingSize) Will be deleted completely
public final void renderToGraphics(IRenderingOptions options, Canvas graphics, float scaleX, float scaleY) Will be deleted completely

Output

方法签名 替代方法签名
public final IOutputFile add(String path, Bitmap image) public final IOutputFile add(String path, IImage image)

ImageCollection

方法签名 替代方法签名
public final IPPImage addImage(Bitmap image) public final IPPImage addImage(IImage image)

PPImage

方法签名 替代方法签名
public final Bitmap getSystemImage() public final IImage getImage()

PatternFormat

方法签名 替代方法签名
public final Bitmap getTileImage(Integer styleColor) public final IImage getTile(Integer styleColor)
public final Bitmap getTileImage(Integer background, Integer foreground) public final IImage getTile(Integer background, Integer foreground)

PatternFormatEffectiveData

方法签名 替代方法签名
public final Bitmap getTileImage(Integer background, Integer foreground) public final IImage getTileIImage(Integer background, Integer foreground)

Canvas 的 API 支持将停止

带有 Canvas 的方法已被标记为已弃用,其支持将从公共 API 中移除。

使用该类的 API 部分将被删除:

Slide

常见问题

为什么移除了 android.graphics.Canvas?

为了统一渲染和图像的工作,消除对平台特定依赖的关联,并转向使用跨平台的 IImageCanvas 的支持正从公共 API 中移除。所有针对 Canvas 的渲染方法都将被删除。

IImage 相对于 BufferedImage 的实际优势是什么?

IImage 将光栅图像和矢量图像的处理统一起来,并通过 ImageFormat 简化了保存为各种格式的操作。

Modern API 会影响生成缩略图的性能吗?

getThumbnail 切换到 getImage 不会在大多数场景下降低性能:新方法在提供相同的选项和尺寸生成图像的能力的同时,仍然保留对渲染选项的支持。具体的提升或下降取决于使用场景,但功能上两者是等价的。