Làm việc với Hình ảnh trong Bản trình chiếu — Aspose.Slides FOSS cho Java

Aspose.Slides FOSS for Java cho phép bạn nhúng hình ảnh vào bộ sưu tập hình ảnh chung của bản trình chiếu và hiển thị chúng trên các slide bằng cách sử dụng các hình dạng PictureFrame. Hình ảnh cũng có thể được sử dụng làm nền cho hình dạng thông qua FillType.PICTURE.


Thêm hình ảnh từ tệp

Tải các byte hình ảnh từ đĩa và thêm chúng vào bộ sưu tập hình ảnh của bản trình chiếu bằng prs.getImages().addImage(). Sau đó đặt hình ảnh lên một slide dưới dạng PictureFrame:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    // Add the image to the shared collection
    byte[] imageData = Files.readAllBytes(Path.of("logo.png"));
    var img = prs.getImages().addImage(imageData);

    // Place it on the slide as a PictureFrame
    var slide = prs.getSlides().get(0);
    slide.getShapes().addPictureFrame(ShapeType.RECTANGLE, 50, 50, 300, 200, img);

    prs.save("with-image.pptx", SaveFormat.PPTX);
}

Bốn đối số vị trí cho addPictureFrame() là: x, y, width, height tính bằng điểm.


Thêm hình ảnh từ byte

Nếu bạn đã có dữ liệu byte của hình ảnh (ví dụ: tải xuống từ URL hoặc đọc từ cơ sở dữ liệu), hãy truyền chúng trực tiếp tới addImage():

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

// Simulate having bytes in memory
byte[] imageBytes = Files.readAllBytes(Path.of("photo.jpg"));

try (Presentation prs = new Presentation()) {
    var img = prs.getImages().addImage(imageBytes);
    prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 100, 80, 400, 250, img);
    prs.save("from-bytes.pptx", SaveFormat.PPTX);
}

Định vị và Định kích thước PictureFrame

PictureFrame được trả về bởi addPictureFrame() kế thừa tất cả các thuộc tính hình học Shape và có thể được định vị lại sau khi tạo:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] imageData = Files.readAllBytes(Path.of("photo.jpg"));
    var img = prs.getImages().addImage(imageData);

    var pf = prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 0, 0, 100, 100, img);

    // Reposition and resize after creation
    pf.setX(50);
    pf.setY(100);
    pf.setWidth(350);
    pf.setHeight(250);

    prs.save("positioned.pptx", SaveFormat.PPTX);
}

Sử dụng hình ảnh làm nền cho hình dạng

Bất kỳ hình dạng nào (không chỉ PictureFrame) đều có thể sử dụng hình ảnh làm nền tô. Đặt FillType.PICTURE và gán hình ảnh cho getPictureFillFormat().getPicture().setImage():

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.FillType;
import org.aspose.slides.foss.PictureFillMode;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] imageData = Files.readAllBytes(Path.of("background.png"));
    var img = prs.getImages().addImage(imageData);

    var slide = prs.getSlides().get(0);
    var shape = slide.getShapes().addAutoShape(ShapeType.ROUND_CORNER_RECTANGLE, 50, 50, 400, 250);
    shape.getFillFormat().setFillType(FillType.PICTURE);
    shape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.STRETCH);
    shape.getFillFormat().getPictureFillFormat().getPicture().setImage(img);

    prs.save("picture-fill.pptx", SaveFormat.PPTX);
}

PictureFillMode.STRETCH điều chỉnh kích thước hình ảnh để lấp đầy toàn bộ hình dạng. Sử dụng TILE cho các mẫu lặp lại.


Thêm Nhiều Hình Ảnh Trên Các Slide

Các hình ảnh được thêm vào prs.getImages() được chia sẻ trên tất cả các slide. Cùng một đối tượng IPPImage có thể được sử dụng trên nhiều slide mà không sao chép dữ liệu:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] logoData = Files.readAllBytes(Path.of("logo.png"));
    var logo = prs.getImages().addImage(logoData);

    // Add the same image to the first slide
    prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 600, 10, 100, 40, logo);

    prs.save("shared-image.pptx", SaveFormat.PPTX);
}

Xem Thêm

 Tiếng Việt