Class ImageUtils


  • public class ImageUtils
    extends Object
    • Constructor Detail

      • ImageUtils

        public ImageUtils()
    • Method Detail

      • encodeAsPng

        public static byte[] encodeAsPng​(BufferedImage image)
        Encodes a given image as PNG.
        Parameters:
        image - The image to encode.
        Returns:
        The PNG bytes representation of the image.
      • imageFromFile

        public static BufferedImage imageFromFile​(String path)
                                           throws com.applitools.eyes.EyesException
        Creates a BufferedImage from an image file specified by path.
        Parameters:
        path - The path to the image file.
        Returns:
        A BufferedImage instance.
        Throws:
        com.applitools.eyes.EyesException - If there was a problem creating the BufferedImage instance.
      • imageFromResource

        public static BufferedImage imageFromResource​(String resource)
                                               throws com.applitools.eyes.EyesException
        Creates a BufferedImage from an image file specified by resource.
        Parameters:
        resource - The resource path.
        Returns:
        A BufferedImage instance.
        Throws:
        com.applitools.eyes.EyesException - If there was a problem creating the BufferedImage instance.
      • imageFromStream

        public static BufferedImage imageFromStream​(InputStream stream)
                                             throws com.applitools.eyes.EyesException
        Creates a BufferedImage from an input stream specified by stream.
        Parameters:
        stream - The input stream.
        Returns:
        A BufferedImage instance.
        Throws:
        com.applitools.eyes.EyesException - If there was a problem creating the BufferedImage instance.
      • imageFromBase64

        public static BufferedImage imageFromBase64​(String image64)
                                             throws com.applitools.eyes.EyesException
        Creates a BufferedImage instance from a base64 encoding of an image's bytes.
        Parameters:
        image64 - The base64 encoding of an image's bytes.
        Returns:
        A BufferedImage instance.
        Throws:
        com.applitools.eyes.EyesException - If there was a problem creating the BufferedImage instance.
      • base64FromImage

        public static String base64FromImage​(BufferedImage image)
        Parameters:
        image - The image from which to get its base64 representation.
        Returns:
        The base64 representation of the image (bytes encoded as PNG).
      • imageFromBytes

        public static BufferedImage imageFromBytes​(byte[] imageBytes)
                                            throws com.applitools.eyes.EyesException
        Creates a BufferedImage instance from raw image bytes.
        Parameters:
        imageBytes - The raw bytes of the image.
        Returns:
        A BufferedImage instance representing the image.
        Throws:
        com.applitools.eyes.EyesException - If there was a problem creating the BufferedImage instance.
      • getImagePart

        public static BufferedImage getImagePart​(BufferedImage image,
                                                 com.applitools.eyes.Region region)
        Get a copy of the part of the image given by region.
        Parameters:
        image - The image from which to get the part.
        region - The region which should be copied from the image.
        Returns:
        The part of the image.
      • rotateImage

        public static BufferedImage rotateImage​(BufferedImage image,
                                                double deg)
        Rotates an image by the given degrees.
        Parameters:
        image - The image to rotate.
        deg - The degrees by which to rotate the image.
        Returns:
        A rotated image.
      • copyImageWithType

        public static BufferedImage copyImageWithType​(BufferedImage src,
                                                      int updatedType)
        Creates a copy of an image with an updated image type.
        Parameters:
        src - The image to copy.
        updatedType - The type of the copied image. See BufferedImage.getType().
        Returns:
        A copy of the src of the requested type.
      • scaleImage

        public static BufferedImage scaleImage​(BufferedImage image,
                                               double scaleRatio,
                                               boolean isMobile)
        Scales an image by the given ratio
        Parameters:
        image - The image to scale.
        scaleRatio - Factor to multiply the image dimensions by
        Returns:
        If the scale ratio != 1, returns a new scaled image, otherwise, returns the original image.
      • resizeImage

        public static BufferedImage resizeImage​(BufferedImage image,
                                                int targetWidth,
                                                int targetHeight)
        Scales an image by the given ratio
        Parameters:
        image - The image to scale.
        targetWidth - The width to resize the image to
        targetHeight - The height to resize the image to
        Returns:
        If the size of image equal to target size, returns the original image, otherwise, returns a new resized image.
      • cropImage

        public static BufferedImage cropImage​(BufferedImage image,
                                              com.applitools.eyes.Region regionToCrop)
        Removes a given region from the image.
        Parameters:
        image - The image to crop.
        regionToCrop - The region to crop from the image.
        Returns:
        A new image without the cropped region.
      • saveImage

        public static void saveImage​(BufferedImage image,
                                     String filename)
        Save image to local file system
        Parameters:
        image - The image to save.
        filename - The path to save image