All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sksamuel.scrimage.nio.ImageReaders Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
package com.sksamuel.scrimage.nio;

import com.sksamuel.scrimage.ImageParseException;
import com.sksamuel.scrimage.ImmutableImage;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * Utilities for reading of an Image to an array of bytes in a specified format.
 */
public class ImageReaders {

   private static final List defaultReaders = detectReaders();

   private static List detectReaders() {
      return detectReaders(Thread.currentThread().getContextClassLoader());
   }

   private static List detectReaders(ClassLoader classloader) {
      return StreamSupport.stream(ServiceLoader.load(ImageReader.class, classloader).spliterator(), false).collect(Collectors.toList());
   }

   public static ImmutableImage read(ImageSource source, Rectangle rectangle) throws IOException {
      return read(source, rectangle, defaultReaders);
   }

   public static ImmutableImage read(ImageSource source, Rectangle rectangle, ClassLoader classloader) throws IOException {
      return read(source, rectangle, classloader == null ? defaultReaders : detectReaders(classloader));
   }

   public static ImmutableImage read(ImageSource source, Rectangle rectangle, List readers) throws IOException {
      List errors = new ArrayList<>();
      for (ImageReader reader : readers) {
         try {
            ImmutableImage image = reader.read(source.read(), rectangle);
            if (image == null) {
               errors.add(new IOException(reader + " failed"));
            } else {
               return image;
            }
         } catch (Exception e) {
            errors.add(new IOException(reader.toString() + " failed due to " + e.getMessage(), e));
         }
      }
      if (errors.isEmpty())
         throw new ImageParseException();
      else
         throw new ImageParseException(errors);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy