Enum IF

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<IF>

    public enum IF
    extends java.lang.Enum<IF>
    An enumeration of the primary image-types available on the internet.

    This is just an enumerated-type used to ensure proper parameter-requests when downloading images. The type provides a simple means for storing words such as 'jpg,' 'png,' 'gif,' etc... when attempting to download images.
    See Also:
    ImageScrape, ImageScraper


    • Enum Constant Summary

      Enum Constants 
      Enum Constant
      BMP
      GIF
      JPG
      PNG
    • Field Summary

       
      Base64-Encoded Image String Regular-Expression Matcher
      Modifier and Type Field
      static Pattern B64_INIT_STRING
       
      File-Name or URL Extension Convenience Fields
      Modifier and Type Field
      String alternateExtension
      String extension
    • Method Summary

       
      Convert String to Enum Constant
      Modifier and Type Method
      static IF valueOf​(String name)
       
      List all Enum Constants
      Modifier and Type Method
      static IF[] values()
       
      Retrieve Image-Format from a URL or File-Name
      Modifier and Type Method
      static IF get​(String extension)
      static IF getGuess​(String uriStr)
      static IF getGuess​(URL url)
      static IF guessOrThrow​(String uriStr)
       
      Decoding Base64-Encoded Images (Images as String's)
      Modifier and Type Method
      static Ret2<java.awt.image.BufferedImage,
           â€‹IF>
      decodeBase64ToImage​(String base64EncodedImageWithFormat)
      static java.awt.image.BufferedImage decodeBase64ToImage​(String base64EncodedImage, IF imageFormat)
      static java.awt.image.BufferedImage decodeBase64ToImage_V2​(String base64EncodedImage, IF imageFormat)
       
      Methods: class java.lang.Object
      Modifier and Type Method
      String toString()
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • values

        public static IF[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (IF c : IF.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static IF valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • getGuess

        public static IF getGuess​(java.lang.String uriStr)
        This will extract the file-extension from an image URL. Not all images on the internet have URL's that end with the actual image-file-type. In that case, or in the case that the 'uriStr' is a pointer to a non-image-file, 'null' will be returned.
        Parameters:
        uriStr - Is the uri or File-Name of an image.
        Returns:
        If extension has a file-extension that is listed in the IF[] Array - that file-extension will be returned, otherwise 'null' will be returned.
        Code:
        Exact Method Body:
         if (uriStr == null) return null;
        
         int pos = uriStr.lastIndexOf(".");
        
         if (pos == -1) return null;
         if (pos == uriStr.length() - 1) return null;
        
         String s = uriStr.substring(pos + 1).toLowerCase().trim();
        
        
         // The following array is a private & static array defined above
         // NOTE: private static final IF[] arr = { JPG, GIF, BMP, PNG };
        
         for (int i=0; i < arr.length; i++)
        
             if (arr[i].extension.equals(s)) return arr[i];
        
             else if (   (arr[i].alternateExtension != null)
                     &&  (arr[i].alternateExtension.equals(s)))
        
                 return arr[i];
        
         return null;
        
      • guessOrThrow

        public static IF guessOrThrow​(java.lang.String uriStr)
        Invokes getGuess(String), and returns the results - unless the returned result would be null, in which case a UnrecognizedImageExtException is thrown instead.
        Parameters:
        uriStr - Is the uri or File-Name of the image.
        Returns:
        The Image-Format of this Image, based on it's File-Name
        Throws:
        UnrecognizedImageExtException - If the Image-Type cannot be determined (does not match any) based on its File-Name Extension. ('.jpg', '.png', '.gif' etc...)
        Code:
        Exact Method Body:
         IF ret = getGuess(uriStr);
         if (ret != null) return ret;
        
         throw new UnrecognizedImageExtException(
             "The URI or File-Name\n" +
             "[" + uriStr + "]\n" +
             "doesn't have a File-Extension that matches any of the recognized Image-Types " +
             "('.jpg', '.png', '.gif' etc...)"
         );
        
      • get

        public static IF get​(java.lang.String extension)
        Converts a String image-extension to an instance this enumerated type.
        Parameters:
        extension - A valid image-format extension
        Returns:
        An instance of this enumeration, if applicable, or 'null' otherwise.
        Code:
        Exact Method Body:
         extension = extension.toLowerCase().trim();
        
         // The following array is a private & static array defined above
         // NOTE: private static final IF[] arr = { JPG, GIF, BMP, PNG };
        
         for (int i=0; i < arr.length; i++)
        
             if (arr[i].extension.equals(extension)) return arr[i];
        
             else if (   (arr[i].alternateExtension != null)
                     &&  (arr[i].alternateExtension.equals(extension)))
        
                 return arr[i];
        
         return null;
        
      • getGuess

        public static IF getGuess​(java.net.URL url)
        This will retrieve the image name from a java.net.URL object.
        Parameters:
        url - The url of the image.
        Returns:
        If this URL has a file-extension that is listed in the IF[] Array, that file-extension will be returned, otherwise 'null' will be returned.
        Code:
        Exact Method Body:
         String f = url.getFile();
        
         return (f != null) ? getGuess(f) : null;
        
      • decodeBase64ToImage

        public static Ret2<java.awt.image.BufferedImage,​IFdecodeBase64ToImage​
                    (java.lang.String base64EncodedImageWithFormat)
        
        This will retrieve a Buffered Image from a String retrieved from a string that follows this format below. This is the format usually found inside HTML Image Tags.

        Specifically: <IMG SRC="data:image/{png or gif or jpg etc};base64,...">

        The ellipsis (...) above represents the actual Base-64 encoded String. Many web-sites return HTML image tags with the actual picture/image encoded into a String and saved inside the 'SRC' attribute. This method will decode that image-as-a-String into a java.awt.image.BufferedImage
        Parameters:
        base64EncodedImageWithFormat - The best way to obtain this String is to use the command [String encoded = imageTag.AV("src"); ], and pass this variable 'encoded' to this parameter. It is important to note that variable 'imageTag' must be a public class TagNode, and that TagNode must:

        • Have public final String tok equal to 'img'

        • The <IMG> represented must have a SRC="..." which contains a Base-64 encoded image.
        Returns:
        A decoded image that can be saved to file, and an instance of IF that identifies what type of image was specified.

        • Ret2.a (BufferedImage):} The Converted Image

        • Ret2.b (IF):} The Image Type
        Code:
        Exact Method Body:
         // sourceData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAADwCAYAAAA+VemSA...==';
         final Matcher m = B64_INIT_STRING.matcher(base64EncodedImageWithFormat);
        
         if (! m.find()) return null;
         
         final String  imageFormatStr      = m.group(1);
         final String  base64EncodedImage  = m.group(2);
        
         final IF imageFormat = (imageFormatStr != null)
             ? IF.get(imageFormatStr)
             : null;
        
         if (imageFormat == null) return null;
        
         final BufferedImage bi =
             decodeBase64ToImage(base64EncodedImage, imageFormat);
        
         if (bi == null) return null;
        
         return new Ret2<BufferedImage, IF>(bi, imageFormat);
        
      • decodeBase64ToImage

        public static java.awt.image.BufferedImage decodeBase64ToImage​
                    (java.lang.String base64EncodedImage,
                     IF imageFormat)
        
        This will decode a Base-64 String into an image. Here, the decoder used is the one obtained from a call to: java.util.Base64.getDecoder() .

        Text copied from class: java.util.Base64, JDK 1.8
        Basic: Uses "The Base64 Alphabet" as specified in Table 1 of RFC 4648 and RFC 2045 for encoding and decoding operation. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.
        Returns:
        A decoded image that can be saved to file.
        Code:
        Exact Method Body:
         try
             (ByteArrayInputStream bis = new ByteArrayInputStream
                 (Base64.getDecoder().decode(base64EncodedImage)))
        
             { return ImageIO.read(bis); }
        
         catch (IOException e)
             { return null; }
        
      • decodeBase64ToImage_V2

        public static java.awt.image.BufferedImage decodeBase64ToImage_V2​
                    (java.lang.String base64EncodedImage,
                     IF imageFormat)
        
        This will decode a base-64 String into an image. Here, the decoder used is the one obtained from a call to: java.util.Base64.getURLDecoder() .

        Text copied from class: java.util.Base64, JDK 1.8
        URL and Filename safe: Uses the "URL and Filename safe Base64 Alphabet" as specified in Table 2 of RFC 4648 for encoding and decoding. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.
        Returns:
        A decoded image that can be saved to file.
        Code:
        Exact Method Body:
         try
             (ByteArrayInputStream bis = new ByteArrayInputStream
                 (Base64.getUrlDecoder().decode(base64EncodedImage)))
        
             { return ImageIO.read(bis); }
        
         catch (IOException e)
             { return null; }
        
      • toString

        public java.lang.String toString()
        Convert an instance of this enumerated-type to a String.
        Overrides:
        toString in class java.lang.Enum<IF>
        Returns:
        The image-format extension as a String.
        Code:
        Exact Method Body:
         return extension;