モダン API を使用した画像処理の強化
はじめに
Historically, Aspose Slides has a dependency on java.awt and has in the public API the following classes from there:
As of version 24.4, this public API is declared deprecated.
In order to get rid of dependencies on these classes, we added the so-called “Modern API” - i.e. the API that should be used instead of the deprecated one, whose signatures contain dependencies on BufferedImage. Graphics2D is declared deprecated and its support is removed from the public Slides API.
Removal of the deprecated public API with dependencies on System.Drawing will be in release 24.8.
モダン API
Added the following classes and enums to the public API:
- IImage - represents the raster or vector image.
- ImageFormat - represents the file format of the image.
- Images - methods to instantiate and work with the IImage interface.
Please note that IImage is disposable (it implements the IDisposable interface and its use should be wrapped in using or dispose-it in another convenient way).
A typical scenario of using the new API may look as follows:
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 Dimension(1920, 1080));
try {
// 画像をディスクに保存します。
slideImage.save("slide1.jpeg", ImageFormat.Jpeg);
} finally {
if (slideImage != null) slideImage.dispose();
}
} finally {
if (pres != null) pres.dispose();
}
古いコードを Modern API に置き換える
In general, you will need to replace the call to the old method using ImageIO with the new one.
旧:
BufferedImage slideImage = pres.getSlides().get_Item(0).getThumbnail(new Dimension(1920, 1080));
try {
ImageIO.write(slideImage, "PNG", new File("image.png"));
} catch (IOException e) {
e.printStackTrace();
}
新:
IImage slideImage = pres.getSlides().get_Item(0).getImage(new Dimension(1920, 1080));
try {
slideImage.save("image.png", ImageFormat.Png);
} finally {
if (slideImage != null) slideImage.dispose();
}
スライド サムネイルの取得
Code using a deprecated API:
Presentation pres = new Presentation("pres.pptx");
try {
BufferedImage slideImage = pres.getSlides().get_Item(0).getThumbnail();
try {
ImageIO.write(slideImage, "PNG", new File("slide1.png"));
} catch (IOException e) {
e.printStackTrace();
}
} finally {
if (pres != null) pres.dispose();
}
Modern 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();
}
シェイプ サムネイルの取得
Code using a deprecated API:
Presentation pres = new Presentation("pres.pptx");
try {
BufferedImage shapeImage = pres.getSlides().get_Item(0).getShapes().get_Item(0).getThumbnail();
try {
ImageIO.write(shapeImage, "PNG", new File("shape.png"));
} catch (IOException e) {
e.printStackTrace();
}
} finally {
if (pres != null) pres.dispose();
}
Modern 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();
}
プレゼンテーション サムネイルの取得
Code using a deprecated API:
Presentation pres = new Presentation("pres.pptx");
try {
BufferedImage[] bitmaps = pres.getThumbnails(new RenderingOptions(), new Dimension(1980, 1028));
for (int index = 0; index < bitmaps.length; index++)
{
try
{
BufferedImage thumbnail = bitmaps[index];
ImageIO.write(thumbnail, "PNG", new File("slide" + index + ".png"));
}
catch (IOException e)
{
e.printStackTrace();
}
}
} finally {
if (pres != null) pres.dispose();
}
Modern API:
Presentation pres = new Presentation("pres.pptx");
try {
IImage[] images = pres.getImages(new RenderingOptions(), new Dimension(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();
}
プレゼンテーションへの画像追加
Code using a deprecated API:
Presentation pres = new Presentation();
try {
IPPImage ppImage = null;
try {
BufferedImage bufferedImages = ImageIO.read(new File("image.png"));
ppImage = pres.getImages().addImage(bufferedImages);
} catch (IOException e) {
e.printStackTrace();
}
pres.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);
} finally {
if (pres != null) pres.dispose();
}
Modern 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();
}
削除されるメソッドと Modern API における置換
プレゼンテーション
| メソッド シグネチャ | 置換 メソッド シグネチャ |
|---|---|
| public final BufferedImage[] getThumbnails(IRenderingOptions options) | public final IImage[] getImages(IRenderingOptions options) |
| public final BufferedImage[] getThumbnails(IRenderingOptions options, float scaleX, float scaleY) | public final IImage[] getImages(IRenderingOptions options, float scaleX, float scaleY) |
| public final BufferedImage[] getThumbnails(IRenderingOptions options, int[] slides) | public final IImage[] getImages(IRenderingOptions options, int[] slides) |
| public final BufferedImage[] getThumbnails(IRenderingOptions options, int[] slides, float scaleX, float scaleY) | public final IImage[] getImages(IRenderingOptions options, int[] slides, float scaleX, float scaleY) |
| public final BufferedImage[] getThumbnails(IRenderingOptions options, int[] slides, Dimension imageSize) | public final IImage[] getImages(IRenderingOptions options, int[] slides, Dimension imageSize) |
| public final BufferedImage[] getThumbnails(IRenderingOptions options, Dimension imageSize) | public final IImage[] getImages(IRenderingOptions options, Dimension imageSize) |
シェイプ
| メソッド シグネチャ | 置換 メソッド シグネチャ |
|---|---|
| public final BufferedImage getThumbnail() | public final IImage getImage() |
| public final BufferedImage getThumbnail(int bounds, float scaleX, float scaleY) | public final IImage getImage(int bounds, float scaleX, float scaleY) |
スライド
| メソッド シグネチャ | 置換 メソッド シグネチャ |
|---|---|
| public final BufferedImage getThumbnail() | public final IImage getImage() |
| public final BufferedImage getThumbnail(float scaleX, float scaleY) | public final IImage getImage(float scaleX, float scaleY) |
| public final BufferedImage getThumbnail(IRenderingOptions options) | public final IImage getImage(IRenderingOptions options) |
| public final BufferedImage getThumbnail(IRenderingOptions options, float scaleX, float scaleY) | public final IImage getImage(IRenderingOptions options) |
| public final BufferedImage getThumbnail(IRenderingOptions options, Dimension imageSize) | public final IImage getImage(IRenderingOptions options, Dimension imageSize) |
| public final BufferedImage getThumbnail(ITiffOptions options) | public final IImage getImage(ITiffOptions options) |
| public final BufferedImage getThumbnail(Dimension imageSize) | public final IImage getImage(Dimension imageSize) |
| public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics) | Will be deleted completely |
| public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics, float scaleX, float scaleY) | Will be deleted completely |
| public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics, Dimension renderingSize) | Will be deleted completely |
出力
| メソッド シグネチャ | 置換 メソッド シグネチャ |
|---|---|
| public final IOutputFile add(String path, BufferedImage image) | public final IOutputFile add(String path, IImage image) |
ImageCollection
| メソッド シグネチャ | 置換 メソッド シグネチャ |
|---|---|
| public final IPPImage addImage(BufferedImage image) | public final IPPImage addImage(IImage image) |
PPImage
| メソッド シグネチャ | 置換 メソッド シグネチャ |
|---|---|
| public final BufferedImage getSystemImage() | public final IImage getImage() |
PatternFormat
| メソッド シグネチャ | 置換 メソッド シグネチャ |
|---|---|
| public final BufferedImage getTileImage(Color styleColor) | public final IImage getTile(Color styleColor) |
| public final BufferedImage getTileImage(Color background, Color foreground) | public final IImage getTile(Color background, Color foreground) |
PatternFormatEffectiveData
| メソッド シグネチャ | 置換 メソッド シグネチャ |
|---|---|
| public final java.awt.image.BufferedImage getTileImage(Color background, Color foreground) | public final IImage getTileIImage(Color background, Color foreground) |
Graphics2D の API サポートは終了します
Methods with Graphics2D are declared deprecated and their support will be removed from the public API.
The part of the API that uses it will be removed:
- public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics)
- public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics, float scaleX, float scaleY)
- public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics, Dimension renderingSize)
FAQ
なぜ java.awt.Graphics2D が廃止されたのですか?
Support for Graphics2D is being removed from the public API to unify work with rendering and images, eliminate ties to platform-specific dependencies, and switch to a cross-platform approach with IImage. All rendering methods to Graphics2D will be removed.
BufferedImage と比べた IImage の実用的な利点は何ですか?
IImage はラスタ画像とベクタ画像の両方を統一的に扱い、ImageFormat を通じてさまざまな形式への保存を簡素化します。
Modern API はサムネイル生成のパフォーマンスに影響しますか?
getThumbnail から getImage への切り替えはシナリオを悪化させません。新しいメソッドはオプションやサイズを指定した画像生成機能を同等に提供し、レンダリングオプションのサポートも保持します。具体的な性能向上または低下はシナリオ次第ですが、機能的には置換は等価です。