/** * Retrieve a compressed image from an image. * @param image - the image. * @return A compressed image from an image. * @throws IOException If we were unable to compress the image. */ public static CompressedImage fromPng(RenderedImage image) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); ImageIO.write(image, "png", output); return new CompressedImage("image/png", output.toByteArray()); }
/** * Uncompress and return the stored image. * @return The image. * @throws IOException If the image data could not be decoded. */ public BufferedImage getImage() throws IOException { return ImageIO.read(new ByteArrayInputStream(getData())); }
/** * Convert the compressed image to encoded text. * @return The encoded text. */ public String toEncodedText() { if (encoded == null) { final ByteBuf buffer = Unpooled.wrappedBuffer(getDataCopy()); encoded = "data:" + getMime() + ";base64," + Base64.encode(buffer).toString(Charsets.UTF_8); } return encoded; } }
@Override public String getMime() { initialize(); return super.getMime(); }
@Override protected byte[] getData() { initialize(); return super.getData(); }
/** * Retrieve a compressed image from a byte array of a PNG file. * @param data - the file as a byte array. * @return The compressed image. */ public static CompressedImage fromPng(byte[] data) { return new CompressedImage("image/png", data); }
/** * Retrieve the compressed PNG file that is being displayed as a favicon. * @return The favicon, or NULL if no favicon will be displayed. */ public CompressedImage getFavicon() { String favicon = (String) FAVICON.get(handle); return (favicon != null) ? CompressedImage.fromEncodedText(favicon) : null; }
/** * Set the compressed PNG file that is being displayed. * @param image - the new compressed image or NULL if no favicon should be displayed. */ public void setFavicon(CompressedImage image) { FAVICON.set(handle, (image != null) ? image.toEncodedText() : null); }
/** * Retrieve a copy of the underlying data array. * @return The underlying compressed image. */ public byte[] getDataCopy() { return getData().clone(); }
/** * Retrieve a compressed image from an input stream. * @param input - the PNG as an input stream. * @return The compressed image. * @throws IOException If we cannot read the input stream. */ public static CompressedImage fromPng(InputStream input) throws IOException { return new CompressedImage("image/png", ByteStreams.toByteArray(input)); }