/** Constructs an image with a byte array. */ public AImage(String name, byte[] data) throws IOException { init(name, data); } /** Constructs an image with an input stream.
/** Encodes an AWT image into a ZK image. * * @param name The name of the image. The extension of the name must * be the format. For example, foo.png and foo.jpeg. * @param image The AWT image to be encoded. * @exception IllegalArgumentException if name doesn't contain an * extension with supported format. */ public static Image encode(String name, RenderedImage image) throws IOException { ImageEncoder encoder = ImageEncoders.newInstance(getFormat(name)); return new AImage(name, encoder.encode(image)); } private static String getFormat(String name) {
/** Encodes an AWT image into a ZK image (in PNG, JPEG or other format). * * @param name The name of the image. The extension of the name must * be the format. Supported format: png and jpeg. * For example, foo.png and foo.jpeg. * @param image The AWT image to be encoded. * @param quality The quality to use for the image encoding. * It is a number between 0 and 1. The higher the value, * the better the output quality. * Not applicable to "png". * @param encodeAlpha Whether to encode alpha transparency. * Not applicable to "png". * @exception IllegalArgumentException if name doesn't contain an * extension with supported format. */ public static Image encode(String name, RenderedImage image, float quality, boolean encodeAlpha) throws IOException { ImageEncoder encoder = ImageEncoders.newInstance(getFormat(name)); encoder.setQuality(quality); encoder.setEncodingAlpha(encodeAlpha); return new AImage(name, encoder.encode(image)); } /** Encodes an AWT image into a ZK image.
public static AImage getImage(String url) throws Exception { return new AImage(Executions.encodeToURL(url)); }
public class ImageToZkImageCoverter implements Converter<AImage, byte[], Image> { private Log logger = LogFactory.getLog(ImageToZkImageCoverter.class); @Override public byte[] coerceToBean(AImage compAttr, Image component, BindContext ctx) { logger.debug("Converting the image"); return compAttr.getByteData(); } @Override public AImage coerceToUi(byte[] beanProp, Image component, BindContext ctx) { try { if (beanProp != null && beanProp.length > 0) { AImage im = new AImage("", beanProp); component.setContent(im); logger.trace("Return an image, length = " + beanProp.length); return im; } logger.debug("Return null => image is empty"); return null; } catch (IOException e) { logger.error("Error occured, returning null", e); return null; } } }
public Object coerceToUi(Object beanProp, org.zkoss.zul.Image imgComp, BindContext ctx) { if (beanProp == null) { return null; } else if (beanProp instanceof org.zkoss.image.Image) { return beanProp; } else if (beanProp instanceof RenderedImage) { RenderedImage awtImg = (RenderedImage) beanProp; try { return Images.encode("a.png", awtImg); } catch (java.io.IOException ex) { throw UiException.Aide.wrap(ex); } } return beanProp; }
/** Returns the encoded URL for the hover image or null if not * available. */ private String getEncodedHoverURL() { if (_auxinf != null && _auxinf.hoverimg instanceof Image) { final Image image = (Image) _auxinf.hoverimg; return Utils.getDynamicMediaURI(this, _auxinf.hoverimgver, "h/" + image.getName(), image.getFormat()); } final Desktop dt = getDesktop(); //it might not belong to any desktop return dt != null && _auxinf != null && _auxinf.hoverimg != null ? dt.getExecution().encodeURL((String) _auxinf.hoverimg) : null; }
_format = getFormatByName(name); if (_format == null) throw new IOException("Unknown image format: "+name); _height = ii.getIconHeight(); _ctype = getContentType(_format);
/** Constructs an image with an URL. */ public AImage(URL url) throws IOException { this(getName(url), url.openStream()); } private void init(String name, byte[] data) throws IOException {
try { String url = attachment.getUrl(); return url != null ? getImage(url) : new AImage(contentType.substring(6), attachment.getData()); } catch (Exception e) {
/** Sets the content directly with the rendered image. * It actually encodes the rendered image to an PNG image * ({@link org.zkoss.image.Image}) with {@link Images#encode}, * and then invoke {@link #setContent(org.zkoss.image.Image)}. * * <p>If you want more control such as different format, quality, * and naming, you can use {@link Images} directly. * * @since 3.0.7 */ public void setContent(RenderedImage image) { try { setContent(image == null ? (org.zkoss.image.Image) null : Images.encode("a.png", image)); } catch (java.io.IOException ex) { throw new UiException(ex); } }
/** Returns the encoded URL of the hover image, or null if no hover image. */ private String getEncodedHoverURL() { if (_hoverimg != null) return Utils.getDynamicMediaURI(this, _hoverimgver, "h/" + _hoverimg.getName(), _hoverimg.getFormat()); final Desktop dt = getDesktop(); //it might not belong to any desktop return dt != null && _hoversrc != null ? dt.getExecution().encodeURL(_hoversrc) : null; }
/** Constructs an image with an input stream. * * <p>Note that this method automatically closes the input stream * (since ZK 3.0.0). */ public AImage(String name, InputStream is) throws IOException { try { init(name, Files.readAll(is)); } finally { is.close(); } } /** Constructs an image with a file name.
public void onEvent(Event event) { if (Strings.isBlank(getWidth())) throw new UiException("captcha must specify width"); if (Strings.isBlank(getHeight())) throw new UiException("captcha must specify height"); try { //generate the distorted image based on the given text value byte[] bytes = getCaptchaEngine().generateCaptcha(Captcha.this); final AImage image = new AImage("captcha" + new Date().getTime(), bytes); setContent(image); } catch (java.io.IOException ex) { throw UiException.Aide.wrap(ex); } finally { _smartDrawCaptcha = false; } } };
/** Sets the content of the hover image directly with the rendered image. * The hover image is used when the mouse is moving over this component. * * <p>It actually encodes the rendered image to an PNG image * ({@link org.zkoss.image.Image}) with {@link Images#encode}, * and then invoke {@link #setHoverContent(org.zkoss.image.Image)}. * * <p>If you want more control such as different format, quality, * and naming, you can use {@link Images} directly. * @since 3.5.0 */ public void setHoverContent(RenderedImage image) { try { setHoverContent(Images.encode("hover.png", image)); } catch (java.io.IOException ex) { throw new UiException(ex); } }
/** Returns the encoded URL of the image (never null). */ private String getEncodedURL() { if (_image != null) return Utils.getDynamicMediaURI( //already encoded this, _imgver, "c/" + _image.getName(), _image.getFormat()); final Desktop dt = getDesktop(); //it might not belong to any desktop return dt != null ? dt.getExecution().encodeURL(_src != null ? _src : "~./img/spacer.gif") : ""; }
private void doSmartDraw() { if (Strings.isBlank(getType())) throw new UiException("chart must specify type (pie, bar, line, ...)"); if (_model == null) { _model = createDefaultModel(); } if (Strings.isBlank(getWidth())) throw new UiException("chart must specify width"); if (Strings.isBlank(getHeight())) throw new UiException("chart must specify height"); try { final AImage image = new AImage("chart" + new Date().getTime(), getEngine().drawChart(Chart.this)); setContent(image); } catch (java.io.IOException ex) { throw UiException.Aide.wrap(ex); } finally { _smartDrawChart = false; } }
/** Sets the content of the hover image directly with the rendered image. * The hover image is used when the mouse is moving over this component. * * <p>It actually encodes the rendered image to an PNG image * ({@link org.zkoss.image.Image}) with {@link Images#encode}, * and then invoke {@link #setHoverImageContent(org.zkoss.image.Image)}. * * <p>If you want more control such as different format, quality, * and naming, you can use {@link Images} directly. * @since 3.5.0 */ public void setHoverImageContent(RenderedImage image) { try { setHoverImageContent(Images.encode("hover.png", image)); } catch (java.io.IOException ex) { throw new UiException(ex); } }
/** Returns the encoded URL for the image ({@link #getImage} * or {@link #getImageContent}), or null if no image. * <p>Used only for component development; not by application developers. * <p>Note: this method can be invoked only if execution is not null. */ private String getEncodedImageURL() { if (_auxinf != null && _auxinf.image instanceof Image) { final Image image = (Image) _auxinf.image; return Utils.getDynamicMediaURI(this, //already encoded _auxinf.imgver, "c/" + image.getName(), image.getFormat()); } final Desktop dt = getDesktop(); //it might not belong to any desktop return dt != null && _auxinf != null && _auxinf.image != null ? dt.getExecution().encodeURL((String) _auxinf.image) : null; }
/** Sets the content directly with the rendered image. * It actually encodes the rendered image to an PNG image * ({@link org.zkoss.image.Image}) with {@link Images#encode}, * and then invoke {@link #setImageContent(org.zkoss.image.Image)}. * * <p>If you want more control such as different format, quality, * and naming, you can use {@link Images} directly. * * @since 3.0.7 */ public void setImageContent(RenderedImage image) { try { setImageContent(Images.encode("a.png", image)); } catch (java.io.IOException ex) { throw new UiException(ex); } }