Enum IF

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

    public enum IF
    extends java.lang.Enum<IF>
    IF (Image Format) - Documentation.

    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 Description
      BMP
      Used to indicate a picture using the common ommon '.bmp' image format.
      GIF
      Used to indicate a picture using the common ommon '.gif' image format.
      JPG
      Used to indicate a picture using the common '.jpg' image format.
      PNG
      Used to indicate a picture using the common '.png' image format.
    • Enum Constant Detail

      • JPG

        public static final IF JPG
        Used to indicate a picture using the common '.jpg' image format. According to a Yahoo! Search link:

        The JPEG file extension is used interchangeably with JPG. JPEG stands for Joint Photographic Experts Group who created the standard. JPG files have 2 sub-formats, JPG/ Exif (often used in digital cameras and photographic equipment), and JPG/ JFIF (often used on the World Wide Web).

        What is JPG? What Opens a JPG? Exact Link:

        http://whatis.techtarget.com/fileformat/JPG-JPEG-bitmap
      • GIF

        public static final IF GIF
        Used to indicate a picture using the common ommon '.gif' image format. Short for "Graphics Interchange Format".
      • BMP

        public static final IF BMP
        Used to indicate a picture using the common ommon '.bmp' image format. Abbreviation of the word 'Bit Map'
      • PNG

        public static final IF PNG
        Used to indicate a picture using the common '.png' image format. PNG stands for Portable Network Graphics. It is an open source file extension for raster graphics files.
    • Field Detail

      • extension

        public final java.lang.String extension
        This is the actual file-name extension saved as a String.
        Code:
        Exact Field Declaration Expression:
        1
        public final    String  extension;
        
      • B64_INIT_STRING

        public static final java.util.regex.Pattern B64_INIT_STRING
        This will parse a 'Base64' String into two groups using Java's RegEx Tools.
        Matcher m = B64_INIT_STRING.matcher(base64String); if (m.find())


        1. m.group(1) => Image Encoding Type (gif, jpg, etc...)

        2. m.gropu(2) => Base 64 String
        Code:
        Exact Field Declaration Expression:
        1
        2
        3
        4
        public static final Pattern B64_INIT_STRING = Pattern.compile(
                "^\\s*data:\\s*image\\/\\s*([A-Za-z]{3,4})\\s*;\\s*base64\\s*,(.*)$",
                Pattern.CASE_INSENSITIVE
            );
        
    • 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
      • 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:
        1
         return extension;
        
      • getGuess

        public static IF getGuess​(java.lang.String urlStr)
        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 'urlStr' is a pointer to a non-image-file, 'null' will be returned.
        Parameters:
        urlStr - Is the url of the 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:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
         if (urlStr == null)                 return null;
        
         int pos = urlStr.lastIndexOf(".");
        
         if (pos == -1)                      return null;
         if (pos == urlStr.length() - 1)     return null;
        
         String s = urlStr.substring(pos + 1).toLowerCase().trim();
        
         for (int i=0; i < arr.length; i++)
             if (arr[i].extension.equals(s))                 return arr[i];
             else if (arr[i].alternateExtension != null)
                 if (arr[i].alternateExtension.equals(s))    return arr[i];
        
         return null;
        
      • 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:
        1
        2
        3
        4
        5
        6
         extension = extension.toLowerCase();
         for (int i=0; i < arr.length; i++)
             if (arr[i].extension.equals(extension))                 return arr[i];
             else if (arr[i].alternateExtension != null)
                 if (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:
        1
         String f = url.getFile(); if (f != null) return getGuess(f); else return 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:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
         // sourceData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAADwCAYAAAA+VemSAAAgAEl...==';
         Matcher m = B64_INIT_STRING.matcher(base64EncodedImageWithFormat);
        
         // System.out.println("first 50:\t" + base64EncodedImageWithFormat.substring(0, 80));
        
         if (! m.find()) return null;
         
         String  imageFormatStr      = m.group(1);
         String  base64EncodedImage  = m.group(2);
         IF      imageFormat         = (imageFormatStr != null) ? IF.get(imageFormatStr) : null;
        
         // System.out.println("imageFormatStr:\t" + '[' + imageFormatStr + ']');
         // System.out.println("imageFormat:\t" + '[' + imageFormat + ']');
        
         if (imageFormat == null)            return null;
        
         BufferedImage   bi      = decodeBase64ToImage(base64EncodedImage, imageFormat);
         // BufferedImage   bi   = decodeBase64ToImage_V2(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() .

        COPIED FROM java.util.Base64:

        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:
        1
        2
        3
        4
        5
        6
        7
        8
        9
         try
         {
             ByteArrayInputStream    bis     = new ByteArrayInputStream(Base64.getDecoder().decode(base64EncodedImage));
             BufferedImage           image   = ImageIO.read(bis);
        
             bis.close();
             return image;
         }
         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() .

        COPIED FROM java.util.Base64:

        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:
        1
        2
        3
        4
        5
        6
        7
        8
        9
         try
         {
             ByteArrayInputStream    bis     = new ByteArrayInputStream(Base64.getUrlDecoder().decode(base64EncodedImage));
             BufferedImage           image   = ImageIO.read(bis);
        
             bis.close();
             return image;
         } 
         catch (IOException e) { return null; }