Java의 PowerPoint PPT에서 이미지 추출

어떤 경우에는 텍스트와 함께 PowerPoint 프레젠테이션에서 이미지를 추출해야 할 수도 있습니다. 이를 달성하기 위해 이 기사에서는 Java에서 프로그래밍 방식으로 PowerPoint PPT 또는 PPTX에서 이미지를 추출하는 방법을 다룹니다. 또한 PPT 슬라이드의 배경이나 모양에서만 이미지를 추출하는 방법을 배우게 됩니다.

PowerPoint PPT에서 이미지를 추출하는 Java API

Java용 Aspose.Slides는 PowerPoint 프레젠테이션을 원활하게 만들고 조작할 수 있는 인기 있고 기능이 풍부한 API입니다. PPT/PPTX 파일에서 이미지를 추출하는 데 이 API를 사용합니다. API의 JAR을 다운로드하거나 다음 Maven 구성을 사용하여 설치할 수 있습니다.

저장소:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

의존:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>22.1</version>
    <classifier>jdk16</classifier>
</dependency>

Java의 PowerPoint PPT에서 이미지 추출

PowerPoint 프레젠테이션에서 슬라이드에 사용된 모든 이미지는 이미지 컬렉션에 저장됩니다. 이 컬렉션에 액세스할 수 있으며 각 이미지를 파일로 저장할 수 있습니다. 다음은 Java에서 PPT 프레젠테이션의 모든 이미지를 추출하는 단계입니다.

  • 먼저 Presentation 클래스를 사용하여 PPT/PPTX 프레젠테이션을 로드합니다.
  • 그런 다음 Presentation.getImages() 메서드를 사용하여 프레젠테이션의 이미지 컬렉션에 액세스합니다.
  • 마지막으로 각 이미지의 유형과 형식을 가져와서 저장합니다.

다음 코드 샘플은 Java에서 PowerPoint PPTX 파일에서 이미지를 추출하는 방법을 보여줍니다.

// 프레젠테이션 로드
Presentation pres = new Presentation("presentation.pptx");

int imageIndex = 1;
String imageType = "";
String imagePath = "Image_";

// 이미지 반복
for (IPPImage image : pres.getImages()) {
  // 이미지 유형 가져오기
  imageType = image.getContentType();
  imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length());

  // 이미지를 저장
  try {
    ImageIO.write(image.getSystemImage(), imageType, new File(imagePath + imageIndex + "." + imageType.toString()));
  } catch (IOException ex) {
    System.out.println(ex.getMessage());
  }
  imageIndex++;
}

PPT의 모양에서 이미지 추출

PPT 슬라이드의 도형에서만 이미지를 추출할 수도 있습니다. 다음은 이를 수행하는 단계입니다.

  • 먼저 Presentation 클래스를 사용하여 프레젠테이션 파일을 로드합니다.
  • 그런 다음 Presentation.getSlides() 메서드를 사용하여 슬라이드 컬렉션에 액세스합니다.
  • 각 슬라이드에 대해 ISlide.getShapes() 메서드를 사용하여 해당 모양에 액세스합니다.
  • 컬렉션의 각 모양에 대해 다음 단계를 수행합니다.
    • 모양이 자동 모양이고 그림으로 채워져 있는지 확인한 다음 getImage() 메서드를 사용하여 이미지를 추출합니다.
    • 모양이 액자인지 확인한 다음 getImage() 메서드를 사용하여 이미지를 추출합니다.
    • 마지막으로 이미지를 파일로 저장합니다.

다음 코드 샘플은 Java를 사용하여 PPT의 모양에서 이미지를 추출하는 방법을 보여줍니다.

// 프레젠테이션 로드
Presentation pres = new Presentation("presentation.pptx");

com.aspose.slides.IPPImage img = null;
int slideIndex = 0;
String imageType = "";
boolean isImageFound = false;

// 슬라이드 반복
for (int i = 0; i < pres.getSlides().size(); i++) {
  slideIndex++;

  // 슬라이드 액세스
  ISlide sl = pres.getSlides().get_Item(i);

  for (int j = 0; j < sl.getShapes().size(); j++) {
    // 도형에 액세스
    IShape sh = sl.getShapes().get_Item(j);

    // 자동 모양인지 확인
    if (sh instanceof IAutoShape) {
      IAutoShape ashp = (IAutoShape) sh;
      if (ashp.getFillFormat().getFillType() == FillType.Picture) {
        img = ashp.getFillFormat().getPictureFillFormat().getPicture().getImage();
        imageType = img.getContentType();
        imageType = imageType.substring(0, imageType.indexOf("/") + 1);
        isImageFound = true;
      }
    }

    // 모양이 액자라면
    else if (sh instanceof IPictureFrame) {
      IPictureFrame pf = (IPictureFrame) sh;
      img = pf.getPictureFormat().getPicture().getImage();
      imageType = img.getContentType();
      imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length());
      isImageFound = true;
    }

    // 원하는 사진 형식 설정
    if (isImageFound) {
      try {
        ImageIO.write(img.getSystemImage(), imageType,
            new File("Slide_" + slideIndex + "_Shape_" + j + "." + imageType));
      } catch (IO예외 ex) {
        // 예외
      }
    }
    isImageFound = false;
  }
}

Java의 슬라이드 배경에서 이미지 추출

또 다른 가능한 시나리오는 슬라이드 배경으로만 사용되는 이미지를 추출하는 것입니다. 다음 단계는 Java에서 슬라이드 배경 이미지를 추출하는 방법을 보여줍니다.

다음 코드 샘플은 Java에서 PPT의 슬라이드 배경에서 이미지를 추출하는 방법을 보여줍니다.

// 프레젠테이션 로드
Presentation pres = new Presentation("presentation.pptx");

com.aspose.slides.IPPImage img = null;
com.aspose.slides.IPPImage backImage = null;

String ImagePath = "BackImage_";
int slideIndex = 0;
String imageType = "";
for (int i = 0; i < pres.getSlides().size(); i++) {
  slideIndex++;

  // 슬라이드 액세스
  ISlide sl = pres.getSlides().get_Item(i);

  // 배경이 그림으로 채워져 있는지 확인
  if (sl.getBackground().getFillFormat().getFillType() == FillType.Picture) {
    // 배경 이미지 가져오기
    backImage = sl.getBackground().getFillFormat().getPictureFillFormat().getPicture().getImage();

    // 사진을 저장하다
    BufferedImage image = backImage.getSystemImage();
    imageType = backImage.getContentType();
    imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length());

    try {
      ImageIO.write(image, imageType,
          new File(ImagePath + "Slide_" + slideIndex + "." + imageType.toString()));
    } catch (IOException ex) {
      // 예외 메시지 읽기
    }
  } else {
    // 슬라이드 레이아웃에 이미지 배경이 있는지 확인
    if (sl.getLayoutSlide().getBackground().getFillFormat().getFillType() == FillType.Picture) {

      // 배경 이미지 가져오기
      backImage = sl.getLayoutSlide().getBackground().getFillFormat().getPictureFillFormat().getPicture()
          .getImage();

      // 이미지를 저장
      BufferedImage image = backImage.getSystemImage();
      imageType = backImage.getContentType();
      imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length());

      try {
        ImageIO.write(image, imageType,
            new File(ImagePath + "LayoutSlide_" + slideIndex + "." + imageType.toString()));
      } catch (IOException ex) {
        // 예외 메시지 읽기
      }
    }
  }
}

무료 라이선스 받기

Aspose는 평가 제한 없이 Java용 Aspose.Slides를 사용할 수 있는 무료 임시 라이선스를 제공합니다. 당신은 자신을 위해 하나를 얻을 수 있습니다.

결론

이 기사에서는 Java의 PowerPoint PPT/PPTX에서 이미지를 추출하는 방법을 시연했습니다. 또한 모양 또는 슬라이드 배경에서 별도로 이미지를 추출하는 방법을 다루었습니다. 또한 문서를 방문하여 Java용 Aspose.Slides에 대해 자세히 알아볼 수 있습니다. 또한 포럼에 질문을 게시할 수 있습니다.

또한보십시오