Search in sources :

Example 1 with JpegEncoder

use of com.servoy.j2db.util.gui.JpegEncoder in project servoy-client by Servoy.

the class AbstractScriptButton method getThumbnailJPGImage.

static byte[] getThumbnailJPGImage(Component component, int width, int height, Icon icon) {
    Image image = null;
    if (icon instanceof MyImageIcon) {
        ImageIcon myIcon = ((MyImageIcon) icon).getScaledIcon(width, height);
        image = myIcon.getImage();
    } else if (icon instanceof ImageIcon) {
        image = ((ImageIcon) icon).getImage();
    }
    if (image != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JpegEncoder encoder = new JpegEncoder(component, image, 100, baos);
        encoder.compress();
        return baos.toByteArray();
    }
    return null;
}
Also used : ImageIcon(javax.swing.ImageIcon) MyImageIcon(com.servoy.j2db.util.gui.MyImageIcon) JpegEncoder(com.servoy.j2db.util.gui.JpegEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(java.awt.Image) MyImageIcon(com.servoy.j2db.util.gui.MyImageIcon)

Example 2 with JpegEncoder

use of com.servoy.j2db.util.gui.JpegEncoder in project servoy-client by Servoy.

the class WebBaseLabel method getThumbnailJPGImage.

public static byte[] getThumbnailJPGImage(int width, int height, MediaResource icon, String text_url, int iconId, boolean keepAspectRatio, IApplication application) {
    Image sourceImage = null;
    byte[] sourceRawData = null;
    if (icon != null) {
        sourceRawData = icon.getRawData();
    } else if (text_url != null) {
        int index = text_url.indexOf(MediaURLStreamHandler.MEDIA_URL_DEF);
        // If it is a media URL.
        if (index != -1) {
            String name = text_url.substring(index + MediaURLStreamHandler.MEDIA_URL_DEF.length());
            try {
                Media media = application.getFlattenedSolution().getMedia(name);
                if (media != null)
                    sourceRawData = media.getMediaData();
                else if (name.startsWith(MediaURLStreamHandler.MEDIA_URL_BLOBLOADER))
                    sourceRawData = MediaURLStreamHandler.getBlobLoaderMedia(application, text_url);
            } catch (Exception ex) {
                // $NON-NLS-1$
                Debug.error("Error loading media for URL: " + text_url, ex);
            }
        } else // If it is a regular, non-media, URL.
        {
            try {
                URL url = new URL(text_url);
                ImageIcon iicon = new ImageIcon(url);
                iicon = ImageLoader.resizeImageIcon(iicon, width, height, keepAspectRatio);
                sourceImage = iicon.getImage();
            } catch (Exception ex) {
                // $NON-NLS-1$
                Debug.error("Error loading icon from URL: " + text_url, ex);
            }
        }
    } else if (iconId > 0) {
        Media media = application.getFlattenedSolution().getMedia(iconId);
        if (media != null)
            sourceRawData = media.getMediaData();
    }
    if ((sourceImage == null) && (sourceRawData != null)) {
        // don't get the directly scaled buffered image, it is not precise.
        sourceImage = ImageLoader.getBufferedImage(sourceRawData, -1, -1, true);
        ImageIcon iicon = new ImageIcon(sourceImage);
        iicon = ImageLoader.resizeImageIcon(iicon, width, height, keepAspectRatio);
        sourceImage = iicon.getImage();
    }
    byte[] jpegedRawData = null;
    if (sourceImage != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JpegEncoder encoder = new JpegEncoder(sourceImage, 100, baos);
        encoder.compress();
        jpegedRawData = baos.toByteArray();
    }
    return jpegedRawData;
}
Also used : ImageIcon(javax.swing.ImageIcon) JpegEncoder(com.servoy.j2db.util.gui.JpegEncoder) Media(com.servoy.j2db.persistence.Media) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(java.awt.Image) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

JpegEncoder (com.servoy.j2db.util.gui.JpegEncoder)2 Image (java.awt.Image)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ImageIcon (javax.swing.ImageIcon)2 Media (com.servoy.j2db.persistence.Media)1 MyImageIcon (com.servoy.j2db.util.gui.MyImageIcon)1 IOException (java.io.IOException)1 URL (java.net.URL)1