Package Torello.HTML.Tools.Images
Enum IF
- java.lang.Object
-
- java.lang.Enum<IF>
-
- Torello.HTML.Tools.Images.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
Hi-Lited Source-Code:- View Here: Torello/HTML/Tools/Images/IF.java
- Open New Browser-Tab: Torello/HTML/Tools/Images/IF.java
File Size: 14,705 Bytes Line Count: 374 '\n' Characters Found
-
-
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-NameModifier 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()
-
-
-
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
-
BMP
-
PNG
-
-
Field Detail
-
extension
public final java.lang.String extension
This is the actual file-name extension saved as aString
.
-
alternateExtension
public final java.lang.String alternateExtension
This field is always just null, except for the case of the'JPG'
Enumeration Constant. For that Image-Format this simply evaluates to theString 'jpeg'
.
-
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.
Example:
import java.util.regex.Matcher; ... Matcher m = IF.B64_INIT_STRING.matcher(base64String); if (m.find()) { ... }
-
m.group(1) =>
Image Encoding Type-String
("gif", "jpg",
etc..)
m.gropu(2) =>
Base 64 Encoded Image-String
-
-
-
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 namejava.lang.NullPointerException
- if the argument is null
-
getGuess
public static IF getGuess(java.lang.String uriStr)
This will extract the file-extension from an imageURL
. Not all images on the internet haveURL'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 theuri
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)
InvokesgetGuess(String)
, and returns the results - unless the returned result would be null, in which case aUnrecognizedImageExtException
is thrown instead.- Parameters:
uriStr
- Is theuri
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 aString
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 ajava.net.URL
object.- Parameters:
url
- Theurl
of the image.- Returns:
- If this
URL
has a file-extension that is listed in theIF[]
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,IF> decodeBase64ToImage (java.lang.String base64EncodedImageWithFormat)
This will retrieve a Buffered Image from aString
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 actualBase-64
encodedString
. Many web-sites return HTML image tags with the actual picture/image encoded into aString
and saved inside the'SRC'
attribute. This method will decode that image-as-a-String
into ajava.awt.image.BufferedImage
- Parameters:
base64EncodedImageWithFormat
- The best way to obtain thisString
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 apublic class TagNode
, and thatTagNode
must:- Have
public final String tok
equal to'img'
- The
<IMG>
represented must have aSRC="..."
which contains aBase-64
encoded image.
- Have
- 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...=='; Matcher m = B64_INIT_STRING.matcher(base64EncodedImageWithFormat); if (! m.find()) return null; String imageFormatStr = m.group(1); String base64EncodedImage = m.group(2); IF imageFormat = (imageFormatStr != null) ? IF.get(imageFormatStr) : null; if (imageFormat == null) return null; 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 aBase-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
-
-