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; } } }
/** 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 {
_format = getFormatByName(name); if (_format == null) throw new IOException("Unknown image format: "+name); _height = ii.getIconHeight(); _ctype = getContentType(_format);
/** 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.
/** 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.
/** 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) {
try { String url = attachment.getUrl(); return url != null ? getImage(url) : new AImage(contentType.substring(6), attachment.getData()); } catch (Exception e) {
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; } } };
/** 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.
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; } }