Example usage for javax.swing ImageIcon getIconWidth

List of usage examples for javax.swing ImageIcon getIconWidth

Introduction

In this page you can find the example usage for javax.swing ImageIcon getIconWidth.

Prototype

public int getIconWidth() 

Source Link

Document

Gets the width of the icon.

Usage

From source file:com.reydentx.core.common.PhotoUtils.java

public static byte[] waterMarkJPG(String baseImagePath, String waterMartPath) {
    try {//ww  w  .j a  va  2 s .  c  o m
        File origFile = new File(baseImagePath);
        ImageIcon icon = new ImageIcon(origFile.getPath());
        BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.drawImage(icon.getImage(), 0, 0, null);

        ImageIcon png = new ImageIcon(waterMartPath);
        graphics.drawImage(png.getImage(), 0, 0, null);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpg", baos);
        baos.flush();
        byte[] imageInByte = baos.toByteArray();

        return imageInByte;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:br.com.diegosilva.jsfcomponents.util.Utils.java

public static byte[] resizeImage(byte[] imageData, String contentType, int width) {
    ImageIcon icon = new ImageIcon(imageData);

    double ratio = (double) width / icon.getIconWidth();
    int resizedHeight = (int) (icon.getIconHeight() * ratio);

    int imageType = "image/png".equals(contentType) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
    BufferedImage bImg = new BufferedImage(width, resizedHeight, imageType);
    Graphics2D g2d = bImg.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.drawImage(icon.getImage(), 0, 0, width, resizedHeight, null);
    g2d.dispose();//ww  w.j  av  a 2s.  c om

    String formatName = "";
    if ("image/png".equals(contentType))
        formatName = "png";
    else if ("image/jpeg".equals(contentType) || "image/jpg".equals(contentType))
        formatName = "jpeg";

    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    try {
        ImageIO.write(bImg, formatName, baos);
        return baos.toByteArray();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:edu.ku.brc.ui.IconManager.java

/**
 * Creates a Black and White image from the color
 * @param img the image to be converted//www .ja v  a 2  s. c om
 * @return new B&W image
 */
public static ImageIcon createBWImage(final ImageIcon img) {
    BufferedImage bi = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.createGraphics();
    g.drawImage(img.getImage(), 0, 0, null);
    ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    colorConvert.filter(bi, bi);
    ImageIcon icon = new ImageIcon(bi);
    g.dispose();
    return icon;
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * @param name//from w w w  .  j a  v a 2 s .c om
 * @param imgIcon
 * @return
 */
public static String uuencodeImage(final String name, final ImageIcon imgIcon) {
    try {
        BufferedImage tmp = new BufferedImage(imgIcon.getIconWidth(), imgIcon.getIconWidth(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = tmp.createGraphics();
        //g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(imgIcon.getImage(), 0, 0, imgIcon.getIconWidth(), imgIcon.getIconWidth(), null);
        g2.dispose();

        ByteArrayOutputStream output = new ByteArrayOutputStream(8192);
        ImageIO.write(tmp, "PNG", output);
        byte[] outputBytes = output.toByteArray();
        output.close();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        UUEncoder uuencode = new UUEncoder(name);
        uuencode.encode(new ByteArrayInputStream(outputBytes), bos);
        return bos.toString();

    } catch (Exception ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(GraphicsUtils.class, ex);
        ex.printStackTrace();
    }
    return "";
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * Gets a scaled icon and if it doesn't exist it creates one and scales it
 * @param icon image to be scaled//from ww  w. j a  v a  2  s. co m
 * @param iconSize the icon size (Std)
 * @param scaledIconSize the new scaled size in pixels
 * @return the scaled icon
 */
public static BufferedImage getBufferedImage(final ImageIcon icon) {
    Image imgMemory = icon.getImage();

    //make sure all pixels in the image were loaded
    imgMemory = new ImageIcon(imgMemory).getImage();

    int w = icon.getIconWidth();
    int h = icon.getIconHeight();
    BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    Graphics2D graphics2D = bufferedImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(imgMemory, 0, 0, w, h, 0, 0, w, h, null);
    graphics2D.dispose();

    return bufferedImage;
}

From source file:edu.ku.brc.ui.GraphicsUtils.java

/**
 * Gets a scaled icon and if it doesn't exist it creates one and scales it
 * @param icon image to be scaled/*w  w  w  .jav  a  2 s .  co  m*/
 * @param iconSize the icon size (Std)
 * @param scaledIconSize the new scaled size in pixels
 * @return the scaled icon
 */
public static Image getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight,
        final boolean maintainRatio) {
    if (icon != null) {
        int dstWidth = newMaxWidth;
        int dstHeight = newMaxHeight;

        int srcWidth = icon.getIconWidth();
        int srcHeight = icon.getIconHeight();

        if ((dstWidth < 0) || (dstHeight < 0)) { //image is nonstd, revert to original size
            dstWidth = icon.getIconWidth();
            dstHeight = icon.getIconHeight();
        }

        if (maintainRatio) {
            double longSideForSource = Math.max(srcWidth, srcHeight);
            double longSideForDest = Math.max(dstWidth, dstHeight);

            double multiplier = longSideForDest / longSideForSource;
            dstWidth = (int) (srcWidth * multiplier);
            dstHeight = (int) (srcHeight * multiplier);
        }

        Image imgMemory = icon.getImage();

        //make sure all pixels in the image were loaded
        imgMemory = new ImageIcon(imgMemory).getImage();

        BufferedImage thumbImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB);

        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(imgMemory, 0, 0, dstWidth, dstHeight, 0, 0, srcWidth, srcHeight, null);
        graphics2D.dispose();
        return thumbImage;

    }
    return null;
}

From source file:net.cbtltd.server.UploadFileService.java

private static byte[] getImageBlob(String fn, int fullsizepixels) {
    try {/*from   w  w  w  .  j av a2  s .  c  o  m*/
        ImageIcon image = new ImageIcon(fn);

        ImageIcon logoImage = new ImageIcon(
                image.getImage().getScaledInstance(fullsizepixels, -1, Image.SCALE_SMOOTH));
        BufferedImage bufferedImage = new BufferedImage(logoImage.getIconWidth(), logoImage.getIconHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.drawImage(logoImage.getImage(), 0, 0, null);
        fn = fn.substring(0, fn.lastIndexOf('.')) + ".Blob.jpg";
        File file = new File(fn);
        file.delete();
        ImageIO.write(bufferedImage, FULLSIZE_JPEG, file);

        InputStream is = new FileInputStream(file);
        long length = file.length();
        if (length > Integer.MAX_VALUE) {
            throw new IOException("File is too large for logo - maximum size = " + Integer.MAX_VALUE);
        }
        byte[] bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file " + file.getName());
        }
        is.close();

        return bytes;
    } catch (IOException x) {
        throw new RuntimeException("Error creating BLOB image " + x.getMessage());
    }
}

From source file:net.cbtltd.server.UploadFileService.java

private static boolean getImage(String fn, int fullsizepixels, int thumbnailpixels) {

    ImageIcon image = new ImageIcon(fn);
    //      if(image.getIconHeight() > 0 && image.getIconWidth() > 0) {
    if (image.getImageLoadStatus() == MediaTracker.COMPLETE) {
        ImageIcon fullsizeImage = new ImageIcon(
                image.getImage().getScaledInstance(-1, fullsizepixels, Image.SCALE_SMOOTH));
        LOG.debug("\n UploadFileService setImage image= " + image + " width=" + fullsizeImage.getIconWidth()
                + "  height=" + fullsizeImage.getIconHeight());
        BufferedImage fullsizeBufferedImage = new BufferedImage(fullsizeImage.getIconWidth(),
                fullsizeImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics fullsizeGraphics = fullsizeBufferedImage.getGraphics();
        fullsizeGraphics.drawImage(fullsizeImage.getImage(), 0, 0, null);
        File fullsizeFile = new File(fn.substring(0, fn.lastIndexOf('.')) + ".jpg");
        fullsizeFile.delete();// ww  w.j av a2  s.c  o  m

        try {
            ImageIO.write(fullsizeBufferedImage, FULLSIZE_JPEG, fullsizeFile);
        } catch (IOException x) {
            throw new RuntimeException("Error saving full sized image " + x.getMessage());
        }

        ImageIcon thumbnailImage = new ImageIcon(
                image.getImage().getScaledInstance(-1, thumbnailpixels, Image.SCALE_SMOOTH));
        File thumbnailFile = new File(fn.substring(0, fn.lastIndexOf('.')) + "Thumb.jpg");
        thumbnailFile.delete();
        BufferedImage thumbnailBufferedImage = new BufferedImage(thumbnailImage.getIconWidth(),
                thumbnailImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics thumbnailGraphics = thumbnailBufferedImage.getGraphics();
        thumbnailGraphics.drawImage(thumbnailImage.getImage(), 0, 0, null);
        try {
            ImageIO.write(thumbnailBufferedImage, FULLSIZE_JPEG, thumbnailFile);
        } catch (IOException x) {
            throw new RuntimeException("Error saving thumbnail image " + x.getMessage());
        }
        return true;
    } else {
        LOG.error("\n UploadFileService setImage image= " + image + " width=" + image.getIconWidth()
                + "  height=" + image.getIconHeight());
        return false;
    }
}

From source file:net.cbtltd.server.UploadFileService.java

private static boolean getImage(String fn, BufferedImage image, Map<String, byte[]> images) {
    int fullsizepixels = Text.FULLSIZE_PIXELS_VALUE;
    int thumbnailpixels = Text.THUMBNAIL_PIXELS_VALUE;
    //      ByteBuffer byteArray = new ByteBuffer();
    ByteArrayOutputStream bOutputReg = new ByteArrayOutputStream();
    ByteArrayOutputStream bOutputThumb = new ByteArrayOutputStream();
    ImageIcon imageIcon = new ImageIcon(image);
    //      if(image.getIconHeight() > 0 && image.getIconWidth() > 0) {
    if (imageIcon.getImageLoadStatus() == MediaTracker.COMPLETE) {
        ImageIcon fullsizeImage = new ImageIcon(
                imageIcon.getImage().getScaledInstance(-1, fullsizepixels, Image.SCALE_SMOOTH));
        LOG.debug("\n UploadFileService setImage image= " + imageIcon + " width=" + fullsizeImage.getIconWidth()
                + "  height=" + fullsizeImage.getIconHeight());
        BufferedImage fullsizeBufferedImage = new BufferedImage(fullsizeImage.getIconWidth(),
                fullsizeImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics fullsizeGraphics = fullsizeBufferedImage.getGraphics();
        fullsizeGraphics.drawImage(fullsizeImage.getImage(), 0, 0, null);
        String fullsizeFile = fn.substring(0, fn.lastIndexOf('.')) + ".jpg";

        try {//from w  ww . j av a  2  s.c o m
            ImageIO.write(fullsizeBufferedImage, FULLSIZE_JPEG, bOutputReg);
            bOutputReg.flush();
            images.put(fullsizeFile, bOutputReg.toByteArray());
            bOutputReg.close();
        } catch (IOException x) {
            throw new RuntimeException("Error saving full sized image " + x.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage() + " Error saving full sized image: " + fullsizeFile);
        }

        ImageIcon thumbnailImage = new ImageIcon(
                imageIcon.getImage().getScaledInstance(-1, thumbnailpixels, Image.SCALE_SMOOTH));
        String thumbnailFile = fn.substring(0, fn.lastIndexOf('.')) + "Thumb.jpg";

        BufferedImage thumbnailBufferedImage = new BufferedImage(thumbnailImage.getIconWidth(),
                thumbnailImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics thumbnailGraphics = thumbnailBufferedImage.getGraphics();
        thumbnailGraphics.drawImage(thumbnailImage.getImage(), 0, 0, null);
        try {
            ImageIO.write(thumbnailBufferedImage, FULLSIZE_JPEG, bOutputThumb);
            bOutputThumb.flush();
            images.put(thumbnailFile, bOutputThumb.toByteArray());
            bOutputThumb.close();
        } catch (IOException x) {
            throw new RuntimeException("Error saving thumbnail image " + x.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage() + " Error saving thumbnail image: " + thumbnailFile);
        }

        return true;
    } else {
        LOG.error("\n UploadFileService setImage image= " + imageIcon + " width=" + imageIcon.getIconWidth()
                + "  height=" + imageIcon.getIconHeight());
        return false;
    }
}

From source file:net.cbtltd.server.UploadFileService.java

private static boolean getCompressedImage(String fn, BufferedImage image, Map<String, byte[]> images) {
    int fullsizepixels = Text.FULLSIZE_PIXELS_VALUE;
    int thumbnailpixels = Text.THUMBNAIL_PIXELS_VALUE;
    //      ByteBuffer byteArray = new ByteBuffer();
    ByteArrayOutputStream bOutputReg = new ByteArrayOutputStream();
    ByteArrayOutputStream bOutputThumb = new ByteArrayOutputStream();
    ImageIcon imageIcon = new ImageIcon(image);
    try {/*  w ww .  jav  a2 s  .com*/
        GZIPOutputStream zipStreamReg = new GZIPOutputStream(bOutputReg);
        GZIPOutputStream zipStreamThumb = new GZIPOutputStream(bOutputThumb);

        if (imageIcon.getImageLoadStatus() == MediaTracker.COMPLETE) {
            ImageIcon fullsizeImage = new ImageIcon(
                    imageIcon.getImage().getScaledInstance(-1, fullsizepixels, Image.SCALE_SMOOTH));
            LOG.debug("\n UploadFileService setImage image= " + imageIcon + " width="
                    + fullsizeImage.getIconWidth() + "  height=" + fullsizeImage.getIconHeight());
            BufferedImage fullsizeBufferedImage = new BufferedImage(fullsizeImage.getIconWidth(),
                    fullsizeImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics fullsizeGraphics = fullsizeBufferedImage.getGraphics();
            fullsizeGraphics.drawImage(fullsizeImage.getImage(), 0, 0, null);
            String fullsizeFile = fn.substring(0, fn.lastIndexOf('.')) + ".jpg";

            try {
                ImageIO.write(fullsizeBufferedImage, FULLSIZE_JPEG, zipStreamReg);
                zipStreamReg.close();
                bOutputReg.close();
                images.put(fullsizeFile, bOutputReg.toByteArray());
            } catch (IOException x) {
                throw new RuntimeException("Error saving full sized image " + x.getMessage());
            }

            ImageIcon thumbnailImage = new ImageIcon(
                    imageIcon.getImage().getScaledInstance(-1, thumbnailpixels, Image.SCALE_SMOOTH));
            String thumbnailFile = fn.substring(0, fn.lastIndexOf('.')) + "Thumb.jpg";

            BufferedImage thumbnailBufferedImage = new BufferedImage(thumbnailImage.getIconWidth(),
                    thumbnailImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics thumbnailGraphics = thumbnailBufferedImage.getGraphics();
            thumbnailGraphics.drawImage(thumbnailImage.getImage(), 0, 0, null);
            try {
                ImageIO.write(thumbnailBufferedImage, FULLSIZE_JPEG, zipStreamThumb);
                zipStreamThumb.close();
                bOutputThumb.close();
                images.put(thumbnailFile, bOutputThumb.toByteArray());
            } catch (IOException x) {
                throw new RuntimeException("Error saving thumbnail image " + x.getMessage());
            }

            return true;
        } else {
            LOG.error("\n UploadFileService setImage image= " + imageIcon + " width=" + imageIcon.getIconWidth()
                    + "  height=" + imageIcon.getIconHeight());
            return false;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;
}