在 Android 上管理演示文稿中的图片框

介绍

图片框是一种包含图像的形状——它就像框中的照片。

您可以通过图片框向幻灯片添加图像。这样,您可以通过格式化图片框来格式化图像。

创建图片框

  1. 创建一个 Presentation 类的实例。
  2. 通过索引获取幻灯片的引用。
  3. 通过向与演示文稿对象关联的 IImagescollection 添加图像,创建一个 IPPImage 对象,以用于填充形状。
  4. 指定图像的宽度和高度。
  5. 通过引用的幻灯片关联的形状对象公开的 AddPictureFrame 方法,基于图像的宽度和高度创建一个 PictureFrame
  6. 将包含图片的图片框添加到幻灯片。
  7. 将修改后的演示文稿写入 PPTX 文件。

下面的 Java 代码演示如何创建图片框:

// 实例化表示 PPTX 文件的 Presentation 类
Presentation pres = new Presentation();
try {
    // 获取第一张幻灯片
    ISlide sld = pres.getSlides().get_Item(0);
    
    // 实例化 Image 类
    IPPImage imgx = pres.getImages().addImage(new FileInputStream(new File("asp1.jpg")));
    
    // 添加一个图片框,其高度和宽度与图片等效
    sld.getShapes().addPictureFrame(ShapeType.Rectangle, 50, 150, imgx.getWidth(), imgx.getHeight(), imgx);
    
    // 将 PPTX 文件写入磁盘
    pres.save("RectPicFrame.pptx", SaveFormat.Pptx);
} catch (IOException e) {
} finally {
    if (pres != null) pres.dispose();
}

创建具有相对缩放的图片框

通过改变图像的相对缩放,您可以创建更复杂的图片框。

  1. 创建一个 Presentation 类的实例。
  2. 通过索引获取幻灯片的引用。
  3. 向演示文稿的图像集合中添加图像。
  4. 通过向与演示文稿对象关联的 IImagescollection 添加图像,创建一个 IPPImage 对象,以用于填充形状。
  5. 在图片框中指定图像的相对宽度和高度。
  6. 将修改后的演示文稿写入 PPTX 文件。

下面的 Java 代码演示如何创建具有相对缩放的图片框:

// 实例化表示 PPTX 的 Presentation 类
Presentation pres = new Presentation();
try {
    // 获取第一张幻灯片
    ISlide sld = pres.getSlides().get_Item(0);
    
    // 实例化 Image 类
    IPPImage imgx = pres.getImages().addImage(new FileInputStream(new File("asp1.jpg")));
    
    
    // 添加一个图片框,其高度和宽度等同于图片
    IPictureFrame pf = sld.getShapes().addPictureFrame(ShapeType.Rectangle, 50, 150, imgx.getWidth(), imgx.getHeight(), imgx);
    
    // 设置相对缩放的宽度和高度
    pf.setRelativeScaleHeight(0.8f);
    pf.setRelativeScaleWidth(1.35f);
    
    // 将 PPTX 文件写入磁盘
    pres.save("RectPicFrame.pptx", SaveFormat.Pptx);
} catch (IOException e) {
} finally {
    if (pres != null) pres.dispose();
}

从图片框中提取光栅图像

您可以从 PictureFrame 对象中提取光栅图像,并将其保存为 PNG、JPG 等格式。下面的代码示例演示如何从文档 “sample.pptx” 中提取图像并以 PNG 格式保存。

Presentation presentation = new Presentation("sample.pptx");

try {
    ISlide firstSlide = presentation.getSlides().get_Item(0);
    IShape firstShape = firstSlide.getShapes().get_Item(0);

    if (firstShape instanceof IPictureFrame) {
        IPictureFrame pictureFrame = (IPictureFrame) firstShape;
        try {
			IImage slideImage = pictureFrame.getPictureFormat().getPicture().getImage().getImage();
			slideImage.save("slide_1_shape_1.png", ImageFormat.Png);
		} finally {
			if (slideImage != null) slideImage.dispose();
		}
    }
} catch (IOException e) {
} finally {
    presentation.dispose();
}

从图片框中提取 SVG 图像

当演示文稿在 PictureFrame 形状中包含 SVG 图形时,Aspose.Slides for Android via Java 可让您以完整保真度检索原始矢量图像。通过遍历幻灯片的形状集合,您可以识别每个 PictureFrame,检查底层的 IPPImage 是否包含 SVG 内容,然后将该图像以其原生 SVG 格式保存到磁盘或流中。

以下代码示例演示如何从图片框中提取 SVG 图像:

Presentation presentation = new Presentation("sample.pptx");

try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IShape shape = slide.getShapes().get_Item(0);

    if (shape instanceof IPictureFrame) {
        IPictureFrame pictureFrame = (IPictureFrame) shape;
        ISvgImage svgImage = pictureFrame.getPictureFormat().getPicture().getImage().getSvgImage();

        FileOutputStream fos = new FileOutputStream("output.svg");
        fos.write(svgImage.getSvgData());
        fos.close();
    }
} catch (IOException e) {
    System.out.println(e.getMessage());
} finally {
    presentation.dispose();
}

获取图像的透明度

Aspose.Slides 允许您获取应用于图像的透明度效果。下面的 Java 代码演示此操作:

Presentation presentation = new Presentation("Test.pptx");

var pictureFrame = (IPictureFrame) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
var imageTransform = pictureFrame.getPictureFormat().getPicture().getImageTransform();
for (var effect : imageTransform) {
    if (effect instanceof IAlphaModulateFixed) {
        var alphaModulateFixed = (IAlphaModulateFixed) effect;
        var transparencyValue = 100 - alphaModulateFixed.getAmount();
        System.out.println("Picture transparency: " + transparencyValue);
    }
}

获取图像的亮度和对比度

Aspose.Slides 允许您获取应用于图像的亮度和对比度效果。[ILuminance](https://reference.aspose.com/slides/zh/androidjava/com.aspose.slides/iluminance/) 接口表示此图像变换效果。

下面的 Java 代码演示如何从图片框获取亮度和对比度设置:

Presentation presentation = new Presentation("sample.pptx");

try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IShape shape = slide.getShapes().get_Item(0);
    IPictureFrame pictureFrame = (IPictureFrame) shape;

    IImageTransformOperationCollection imageTransform = pictureFrame.getPictureFormat().getPicture().getImageTransform();
    for (IImageTransformOperation effect : imageTransform) {
        if (effect instanceof ILuminance) {
            ILuminanceEffectiveData luminance = ((ILuminance) effect).getEffective();
            float brightness = luminance.getBrightness();
            float contrast = luminance.getContrast();

            System.out.println("Brightness: " + brightness);
            System.out.println("Contrast: " + contrast);
        }
    }
} finally {
    presentation.dispose();
}

图片框格式化

Aspose.Slides 提供许多可应用于图片框的格式化选项。使用这些选项,您可以更改图片框以满足特定需求。

  1. 创建一个 Presentation 类的实例。
  2. 通过索引获取幻灯片的引用。
  3. 通过向与演示文稿对象关联的 IImagescollection 添加图像,创建一个 IPPImage 对象,以用于填充形状。
  4. 指定图像的宽度和高度。
  5. 通过 IShapes 对象公开的 AddPictureFrame 方法,基于图像的宽度和高度创建一个 PictureFrame
  6. 将包含图片的图片框添加到幻灯片。
  7. 设置图片框的线条颜色。
  8. 设置图片框的线条宽度。
  9. 通过给予正值或负值旋转图片框。
    • 正值顺时针旋转图像。
    • 负值逆时针旋转图像。
  10. 将图片框(包含图片)再次添加到幻灯片。
  11. 将修改后的演示文稿写入 PPTX 文件。

下面的 Java 代码演示图片框格式化过程:

// 实例化表示 PPTX 的 Presentation 类
Presentation pres = new Presentation();
try {
    // 获取第一张幻灯片
    ISlide sld = pres.getSlides().get_Item(0);
    
    // 实例化 Image 类
    IPPImage imgx = pres.getImages().addImage(new FileInputStream(new File("asp1.jpg")));
    
    // 添加一个图片框,其高度和宽度等同于图片
    IPictureFrame pf = sld.getShapes().addPictureFrame(ShapeType.Rectangle, 50, 150, imgx.getWidth(), imgx.getHeight(), imgx);
    
    // 对 PictureFrameEx 应用一些格式设置
    pf.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    pf.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
    pf.getLineFormat().setWidth(20);
    pf.setRotation(45);
    
    // 将 PPTX 文件写入磁盘
    pres.save("RectPicFrame.pptx", SaveFormat.Pptx);
} catch (IOException e) {
} finally {
    if (pres != null) pres.dispose();
}

将图像添加为链接

为避免演示文稿体积过大,您可以通过链接添加图像(或视频),而不是直接将文件嵌入演示文稿。下面的 Java 代码演示如何向占位符中添加图像和视频:

Presentation presentation = new Presentation("input.pptx");
try {
    ArrayList<IShape> shapesToRemove = new ArrayList<IShape>();
    int shapesCount = presentation.getSlides().get_Item(0).getShapes().size();

    for (int i = 0; i < shapesCount; i++)
    {
        IShape autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(i);

        if (autoShape.getPlaceholder() == null)
        {
            continue;
        }

        switch (autoShape.getPlaceholder().getType())
        {
            case PlaceholderType.Picture:
                IPictureFrame pictureFrame = presentation.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle,
                        autoShape.getX(), autoShape.getY(), autoShape.getWidth(), autoShape.getHeight(), null);

                pictureFrame.getPictureFormat().getPicture().setLinkPathLong(
                        "https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg");

                shapesToRemove.add(autoShape);
                break;

            case PlaceholderType.Media:
                IVideoFrame videoFrame = presentation.getSlides().get_Item(0).getShapes().addVideoFrame(
                        autoShape.getX(), autoShape.getY(), autoShape.getWidth(), autoShape.getHeight(), "");

                videoFrame.getPictureFormat().getPicture().setLinkPathLong(
                        "https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg");

                videoFrame.setLinkPathLong("https://youtu.be/t_1LYZ102RA");

                shapesToRemove.add(autoShape);
                break;
        }
    }

    for (IShape shape : shapesToRemove)
    {
        presentation.getSlides().get_Item(0).getShapes().remove(shape);
    }

    presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
    if (presentation != null) presentation.dispose();
}

裁剪图像

下面的 Java 代码演示如何裁剪幻灯片上已有的图像:

Presentation pres = new Presentation();
// 创建新图像对象
try {
    IPPImage picture;
    IImage image = Images.fromFile(imagePath);
    try {
        picture = pres.getImages().addImage(image);
    } finally {
        if (image != null) image.dispose();
    }

    // 向幻灯片添加 PictureFrame
    IPictureFrame picFrame = pres.getSlides().get_Item(0).getShapes().addPictureFrame(
            ShapeType.Rectangle, 100, 100, 420, 250, picture);

    // 裁剪图像(百分比值)
    picFrame.getPictureFormat().setCropLeft(23.6f);
    picFrame.getPictureFormat().setCropRight(21.5f);
    picFrame.getPictureFormat().setCropTop(3);
    picFrame.getPictureFormat().setCropBottom(31);

    // 保存结果
    pres.save(outPptxFile, SaveFormat.Pptx);
} catch (IOException e) {
} finally {
    if (pres != null) pres.dispose();
}

删除图片框的裁剪区域

如果您想删除框中图像的裁剪区域,可以使用 deletePictureCroppedAreas() 方法。该方法在无需裁剪时返回原始图像。

下面的 Java 代码演示该操作:

Presentation presentation = new Presentation("PictureFrameCrop.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    // 获取首张幻灯片中的 PictureFrame
    IPictureFrame picFrame = (IPictureFrame)slide.getShapes().get_Item(0);

    // 删除 PictureFrame 图像的裁剪区域并返回裁剪后的图像
    IPPImage croppedImage = picFrame.getPictureFormat().deletePictureCroppedAreas();

    // 保存结果
    presentation.save("PictureFrameDeleteCroppedAreas.pptx", SaveFormat.Pptx);
} finally {
    if (presentation != null) presentation.dispose();
}

压缩图像

您可以使用 IPictureFillFormat.compressImage 方法压缩演示文稿中的图片。此方法通过根据形状大小和指定分辨率降低图像大小,并可选择删除裁剪区域。

它的效果类似于 PowerPoint 的 图片格式 > 压缩图片 > 分辨率 功能。

下面的 Java 示例演示如何通过指定目标分辨率并可选删除裁剪区域来压缩演示文稿中的图像:

Presentation presentation = new Presentation("demo.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IPictureFrame pictureFrame = (IPictureFrame)slide.getShapes().get_Item(0);

    // 使用目标分辨率 150 DPI(网页分辨率)压缩图像并删除裁剪区域。
    boolean result = pictureFrame.getPictureFormat().compressImage(true, PicturesCompression.Dpi150);

    // 检查压缩的结果。
    if (result) {
        System.out.println("Image successfully compressed.");
    } else {
        System.out.println("Image compression failed or no changes were necessary.");
    }

    presentation.save("CompressedImage.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

或直接使用自定义 DPI 值:

Presentation presentation = new Presentation("demo.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IPictureFrame pictureFrame = (IPictureFrame)slide.getShapes().get_Item(0);

    // 压缩图像至 150 DPI(网页分辨率),并删除裁剪区域。
    pictureFrame.getPictureFormat().compressImage(true, 150f);

    presentation.save("CompressedImage.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

锁定宽高比

如果您希望包含图像的形状在更改图像尺寸后仍保持宽高比,可使用 setAspectRatioLocked 方法设置 锁定宽高比

下面的 Java 代码演示如何锁定形状的宽高比:

Presentation pres = new Presentation("pres.pptx");
try {
    ILayoutSlide layout = pres.getLayoutSlides().getByType(SlideLayoutType.Custom);
    ISlide emptySlide = pres.getSlides().addEmptySlide(layout);
    IPPImage picture;
    IImage image = Images.fromFile("image.png");
    try {
        picture = pres.getImages().addImage(image);
    } finally {
        if (image != null) image.dispose();
    }
    IPictureFrame pictureFrame = emptySlide.getShapes().addPictureFrame(
            ShapeType.Rectangle, 50, 150, presImage.getWidth(), presImage.getHeight(), picture);

    // 设置形状在调整大小时保持宽高比
    pictureFrame.getPictureFrameLock().setAspectRatioLocked(true);
} catch(IOException e) {
} finally {
    if (pres != null) pres.dispose();
}

使用 StretchOff 属性

使用来自 IPictureFillFormat 接口和 PictureFillFormat 类的 StretchOffsetLeftStretchOffsetTopStretchOffsetRightStretchOffsetBottom 属性,您可以指定填充矩形。

当为图像指定拉伸时,源矩形会被缩放以适应指定的填充矩形。填充矩形的每条边由相对于形状边界框对应边的百分比偏移定义。正百分比表示内缩,负百分比表示外扩。

  1. 创建一个 Presentation 类的实例。
  2. 通过索引获取幻灯片的引用。
  3. 添加一个矩形 AutoShape
  4. 创建图像。
  5. 设置形状的填充类型。
  6. 设置形状的图片填充模式。
  7. 添加用于填充形状的设定图像。
  8. 指定图像相对于形状边界框相应边的偏移。
  9. 将修改后的演示文稿写入 PPTX 文件。

下面的 Java 代码演示使用 StretchOff 属性的过程:

// 实例化表示 PPTX 文件的 Presentation 类
Presentation pres = new Presentation();
try {
    // 获取第一张幻灯片
    ISlide slide = pres.getSlides().get_Item(0);

    // 实例化 ImageEx 类
    IPPImage picture;
    IImage image = Images.fromFile("aspose-logo.jpg");
    try {
        picture = pres.getImages().addImage(image);
    } finally {
        if (image != null) image.dispose();
    }

    // 添加一个设置为 Rectangle 的 AutoShape
    IAutoShape aShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 300, 300);

    // 设置形状的填充类型
    aShape.getFillFormat().setFillType(FillType.Picture);

    // 设置形状的图片填充模式
    aShape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Stretch);

    // 设置图像以填充形状
    aShape.getFillFormat().getPictureFillFormat().getPicture().setImage(picture);

    // 指定图像相对于形状边界框对应边的偏移量
    aShape.getFillFormat().getPictureFillFormat().setStretchOffsetLeft(25);
    aShape.getFillFormat().getPictureFillFormat().setStretchOffsetRight(25);
    aShape.getFillFormat().getPictureFillFormat().setStretchOffsetTop(-20);
    aShape.getFillFormat().getPictureFillFormat().setStretchOffsetBottom(-10);
    
    //Writes the PPTX file to disk
    pres.save("StretchOffsetLeftForPictureFrame_out.pptx", SaveFormat.Pptx);
} catch (IOException e) {
} finally {
    if (pres != null) pres.dispose();
}

常见问题

如何查找 PictureFrame 支持的图像格式?
Aspose.Slides 支持光栅图像(PNG、JPEG、BMP、GIF 等)和矢量图像(例如 SVG),这些图像通过分配给 PictureFrame 的图像对象进行使用。支持的格式列表通常与幻灯片和图像转换引擎的功能相匹配。

添加大量大图像会如何影响 PPTX 大小和性能?
嵌入大图像会增加文件大小和内存占用;使用链接图像可保持演示文稿体积较小,但需要确保外部文件仍可访问。Aspose.Slides 提供通过链接添加图像的功能以降低文件大小。

如何防止图像对象意外移动/缩放?
PictureFrame 使用 shape locks(例如禁用移动或缩放)即可。锁定机制支持包括 PictureFrame 在内的多种形状类型。

导出演示文稿为 PDF/图像时,SVG 矢量保真度是否得到保留?
Aspose.Slides 允许从 PictureFrame 提取原始 SVG 矢量。当 导出为 PDF光栅格式 时,结果可能会根据导出设置被栅格化;但提取行为已证实原始 SVG 仍以矢量形式存储。