Example usage for java.awt.image BufferedImage getWidth

List of usage examples for java.awt.image BufferedImage getWidth

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getWidth.

Prototype

public int getWidth() 

Source Link

Document

Returns the width of the BufferedImage .

Usage

From source file:com.afis.jx.ckfinder.connector.utils.ImageUtils.java

/**
 * Uploads image and if the image size is larger than maximum allowed it resizes the image.
 *
 * @param stream input stream.//from ww w .j  a  v a 2 s  .  c  o m
 * @param file file name
 * @param fileName name of file
 * @param conf connector configuration
 * @throws IOException when error occurs.
 */
public static void createTmpThumb(final InputStream stream, final File file, final String fileName,
        final IConfiguration conf) throws IOException {

    BufferedInputStream bufferedIS = new BufferedInputStream(stream);
    bufferedIS.mark(Integer.MAX_VALUE);
    BufferedImage image = ImageIO.read(bufferedIS);
    if (image == null) {
        throw new IOException("Wrong file");
    }
    Dimension dimension = createThumbDimension(image, conf.getImgWidth(), conf.getImgHeight());
    if (dimension.width == 0 || dimension.height == 0
            || (image.getHeight() == dimension.height && image.getWidth() == dimension.width)) {
        bufferedIS.reset();
        writeUntouchedImage(bufferedIS, file);
    } else {
        resizeImage(image, dimension.width, dimension.height, conf.getImgQuality(), file);
    }
    stream.close();
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Can be used to verify impact of different encoding algorithms. Compares two BufferedImages and outputs differences between those.
 *
 * @param image// w  w w .j a  v a  2s .  c  om
 * @param image2
 * @return
 */
public static int[] calculateBufferedImageDifferencesPxByPx(BufferedImage image, BufferedImage image2) {
    int width = image.getWidth();
    int height = image.getHeight();

    int width2 = image2.getWidth();
    int height2 = image2.getHeight();

    WritableRaster raster1 = image.getRaster();
    WritableRaster raster2 = image2.getRaster();

    if (width != width2 || height != height2)
        throw new IllegalArgumentException(
                "Please insert two identical images that were treated with different image algorithms");

    int[] diff = new int[width * height];

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster1.getNumBands(); band++) {
                int p1 = raster1.getSample(col, row, band);
                int p2 = raster2.getSample(col, row, band);

                diff[((row * width) + col)] = p2 - p1;
            }
        }
    }

    return diff;
}

From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java

public static void updateImage(JLabel imageContainer, File imageFile, Format format) {
    if (imageFile == null || !imageFile.exists()) {
        return;/*w  w  w  .  j  a  va  2  s. c  o m*/
    }
    BufferedImage img = null;
    try {
        img = ImageIO.read(imageFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (img == null) {
        return;
    }
    int imageWidth = img.getWidth();
    int imageHeight = img.getHeight();
    int imageViewWidth = (int) imageContainer.getPreferredSize().getWidth();
    int imageViewHeight = (int) imageContainer.getPreferredSize().getHeight();
    double factor = getScaleFactorToFit(new Dimension(imageWidth, imageHeight),
            new Dimension(imageViewWidth, imageViewHeight));
    imageWidth = (int) (factor * imageWidth);
    imageHeight = (int) (factor * imageHeight);
    if (imageWidth <= 0 || imageHeight <= 0 || imageViewWidth <= 0 || imageViewHeight <= 0) {
        return;
    }
    BufferedImage tmp = UIUtil.createImage(imageViewWidth, imageViewHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    int x = (imageViewWidth - imageWidth) / 2;
    int y = (imageViewHeight - imageHeight) / 2;
    if (format == Format.PNG || format == Format.XML) {
        g2.drawImage(img, x, y, imageWidth, imageHeight, null);
    } else {
        g2.drawImage(img, x, y, imageWidth, imageHeight, Color.WHITE, null);
    }
    g2.dispose();
    imageContainer.setIcon(new ImageIcon(tmp));
}

From source file:com.github.zhanhb.ckfinder.connector.utils.ImageUtils.java

/**
 * create thumb file.//w  ww . j av a 2 s . co m
 *
 * @param originFile origin image file.
 * @param file file to save thumb
 * @param thumbnail connector thumbnail properties
 * @return true if success
 * @throws IOException when IO Exception occurs.
 */
public static boolean createThumb(Path originFile, Path file, ThumbnailProperties thumbnail)
        throws IOException {
    log.debug("createThumb");
    BufferedImage image;
    try (InputStream is = Files.newInputStream(originFile)) {
        image = ImageIO.read(is);
    }
    if (image != null) {
        Dimension dimension = createThumbDimension(image, thumbnail.getMaxWidth(), thumbnail.getMaxHeight());
        FileUtils.createPath(file, true);
        if (image.getHeight() <= dimension.height && image.getWidth() <= dimension.width) {
            writeUntouchedImage(originFile, file);
        } else {
            resizeImage(image, dimension.width, dimension.height, thumbnail.getQuality(), file);
        }
        return true;
    } else {
        log.error("Wrong image file");
    }
    return false;
}

From source file:net.cloudkit.relaxation.VerifyImage.java

public static byte[][] imgToByteArray(BufferedImage src) {
    int w = src.getWidth();
    int h = src.getHeight();
    byte[][] result = new byte[w][h];

    for (int x = 0; x < w; ++x) {
        for (int y = 0; y < h; ++y) {
            int rgb = getRed(src.getRGB(x, y));
            result[x][y] = (byte) (rgb == 255 ? 0 : 1);
        }//from ww w.ja  v  a 2  s .  co  m
    }

    return result;
}

From source file:com.silverpeas.gallery.ImageHelper.java

private static void getDimension(final BufferedImage image, final PhotoDetail photo) {
    if (image == null) {
        photo.setSizeL(0);/*from  www  .j av  a  2s. c o  m*/
        photo.setSizeH(0);
    } else {
        photo.setSizeL(image.getWidth());
        photo.setSizeH(image.getHeight());
    }
}

From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java

private static BufferedImage generateBordersImage(BufferedImage source, int trimmedWidth, int trimmedHeight)
        throws IOException {
    BufferedImage finalBorder = UIUtil.createImage(trimmedWidth + 2, trimmedHeight + 2,
            BufferedImage.TYPE_INT_ARGB);
    int cutW = source.getWidth() - 2;
    int cutH = source.getHeight() - 2;

    // left border
    BufferedImage leftBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB);
    leftBorder.setRGB(0, 0, 1, cutH, source.getRGB(0, 1, 1, cutH, null, 0, 1), 0, 1);
    verifyBorderImage(leftBorder);/* w ww .j  a  va2 s  .  c o  m*/
    leftBorder = resizeBorder(leftBorder, 1, trimmedHeight);
    finalBorder.setRGB(0, 1, 1, trimmedHeight, leftBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1);

    // right border
    BufferedImage rightBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB);
    rightBorder.setRGB(0, 0, 1, cutH, source.getRGB(cutW + 1, 1, 1, cutH, null, 0, 1), 0, 1);
    verifyBorderImage(rightBorder);
    rightBorder = resizeBorder(rightBorder, 1, trimmedHeight);
    finalBorder.setRGB(trimmedWidth + 1, 1, 1, trimmedHeight,
            rightBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1);

    // top border
    BufferedImage topBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB);
    topBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, 0, cutW, 1, null, 0, cutW), 0, cutW);
    verifyBorderImage(topBorder);
    topBorder = resizeBorder(topBorder, trimmedWidth, 1);
    finalBorder.setRGB(1, 0, trimmedWidth, 1, topBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0,
            trimmedWidth);

    // bottom border
    BufferedImage bottomBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB);
    bottomBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, cutH + 1, cutW, 1, null, 0, cutW), 0, cutW);
    verifyBorderImage(bottomBorder);
    bottomBorder = resizeBorder(bottomBorder, trimmedWidth, 1);
    finalBorder.setRGB(1, trimmedHeight + 1, trimmedWidth, 1,
            bottomBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0, trimmedWidth);

    return finalBorder;
}

From source file:common.utils.ImageUtils.java

/**
  * resize input image to new dinesions(only smaller) and save it to file
  * @param image input image for scaling
  * @param scale_factor factor for scaling image
 * @param rez writes resulting image to a file
 * @throws IOException//from   w w  w . ja v a2s  .c o m
  */
public static void saveScaledImageWidth(BufferedImage image, double scale_factor, OutputStream rez)
        throws IOException {
    //long time_resize = System.currentTimeMillis();
    if (scale_factor == 1) {
        writeImage(image, 1, rez);
    } else {
        int width = (int) (scale_factor * image.getWidth());
        int height = (int) (scale_factor * image.getHeight());
        //BufferedImage scaled_bi = new BufferedImage( image.getWidth(), image.getHeight(), image.getType() );
        int type = image.getType();
        BufferedImage scaled_bi;
        if (type == BufferedImage.TYPE_CUSTOM) {
            scaled_bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        } else {
            scaled_bi = new BufferedImage(width, height, type);
        }

        Graphics2D g2 = scaled_bi.createGraphics();

        //g2.drawImage(image, at, null);
        g2.drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        writeImage(scaled_bi, 1, rez);

        g2.dispose();
    }
    //time_resize = System.currentTimeMillis() - time_resize;
    //System.out.print("time_resize=" + (time_resize) + "; ");
}

From source file:com.aurel.track.item.PrintItemDetailsBL.java

/**
 * The big difference relative to other FlatHistoryBean of RENDER_TYPE=ACTUAL_VALUES is that 
 * some of HistoryEntries of FlatHistoryBean have oldValue set which means special handling in jsp:
 * either rendering a image or create a link for attachment
 * @param attachmentBeans/*  ww w.  j  a va 2s .  c o  m*/
 * @param locale
 * @return
 */
private static List<FlatHistoryBean> loadAttachmentFlatHistoryBeans(List<TAttachmentBean> attachmentBeans,
        Map<Integer, TWorkItemBean> workItemBeansMap, Locale locale, boolean withChildren) {
    List<FlatHistoryBean> flatValueList = new ArrayList<FlatHistoryBean>();
    Integer stretchPercent = null;
    Integer maxWidth = null;
    Integer maxHeight = null;
    Properties properties = loadConfigFromFile();
    stretchPercent = getConfigByName(properties, "stretchPercent", 100);
    maxWidth = getConfigByName(properties, "maxWidth", 500);
    maxHeight = getConfigByName(properties, "maxHeight", 500);
    if (attachmentBeans != null) {
        for (TAttachmentBean attachmentBean : attachmentBeans) {
            FlatHistoryBean flatHistoryBean = new FlatHistoryBean();
            List<HistoryEntry> historyEntries = new ArrayList<HistoryEntry>();
            flatHistoryBean.setHistoryEntries(historyEntries);
            flatHistoryBean.setChangedByName(attachmentBean.getChangedByName());
            flatHistoryBean.setPersonID(attachmentBean.getChangedBy());
            flatHistoryBean.setLastEdit(attachmentBean.getLastEdit());
            flatHistoryBean.setType(HistoryBean.HISTORY_TYPE.ATTACHMENT);
            flatHistoryBean.setIconName("attachment.png");
            HistoryLoaderBL.addWorkItemToFlatHistoryBean(flatHistoryBean, workItemBeansMap,
                    attachmentBean.getWorkItem(), FlatHistoryBean.RENDER_TYPE.ACTUAL_VALUES);
            flatHistoryBean.setWorkItemID(attachmentBean.getWorkItem());

            HistoryEntry historyEntry = new HistoryEntry();
            /*render the attachmnet download: 
            historyEntry.fieldLabel -> Name
            historyEntry.newValue -> the fileName
            historyEntry.oldValue -> attachmentID: the value is used in the URL                   
            */
            historyEntry.setFieldLabel(
                    LocalizeUtil.getLocalizedTextFromApplicationResources("common.lbl.name", locale));
            historyEntry.setNewValue(attachmentBean.getFileName());
            historyEntry.setOldValue(attachmentBean.getObjectID().toString());

            historyEntries.add(historyEntry);
            if (!withChildren) {
                historyEntry = new HistoryEntry();
                historyEntry.setFieldLabel(
                        LocalizeUtil.getLocalizedTextFromApplicationResources("common.lbl.size", locale));
                historyEntry.setNewValue(TAttachmentBean.getFileSizeString(attachmentBean.getSize()));
                historyEntries.add(historyEntry);

                String description = attachmentBean.getDescription();
                if (description != null && !"".equals(description)) {
                    historyEntry = new HistoryEntry();
                    historyEntry.setFieldLabel(LocalizeUtil
                            .getLocalizedTextFromApplicationResources("common.lbl.description", locale));
                    historyEntry.setNewValue(description);
                    historyEntries.add(historyEntry);
                }
            }
            boolean isImage = AttachBL.isImage(attachmentBean);
            if (isImage) {
                /*render the image: 
                   historyEntry.fieldLabel -> null
                   historyEntry.newValue -> image width (in pixels)
                   historyEntry.oldValue -> attachmentID: used in the URL                
                */
                TAttachmentBean attachmentBeanPopulated = AttachBL.loadAttachment(attachmentBean.getObjectID(),
                        attachmentBean.getWorkItem(), true);
                String fileNameOnDisk = attachmentBeanPopulated.getFullFileNameOnDisk();
                File f = new File(fileNameOnDisk);
                BufferedImage image;
                Integer originalWidth = null;
                Integer originalHeight = null;
                try {
                    image = ImageIO.read(f);
                    originalWidth = image.getWidth();
                    originalHeight = image.getHeight();
                } catch (IOException e) {
                    LOGGER.warn("Reading the image failed with " + e.getMessage());
                    LOGGER.debug(ExceptionUtils.getStackTrace(e));
                }

                if (fileNameOnDisk != null && !"".equals(fileNameOnDisk)) {
                    historyEntry = new HistoryEntry();
                    if (originalWidth != null && originalHeight != null) {
                        Integer calculatedWidth = Integer
                                .valueOf(originalWidth.intValue() * stretchPercent / 100);
                        Integer calculatedHeight = Integer
                                .valueOf(originalHeight.intValue() * stretchPercent / 100);
                        if (calculatedWidth.intValue() > maxWidth.intValue()
                                && calculatedHeight.intValue() > maxHeight.intValue()) {
                            double widthScale = calculatedWidth.doubleValue() / maxWidth.doubleValue();
                            double heightScale = calculatedHeight.doubleValue() / maxHeight.doubleValue();
                            if (widthScale > heightScale) {
                                calculatedWidth = maxWidth;
                            } else {
                                calculatedWidth = Integer.valueOf((int) (calculatedWidth.doubleValue()
                                        * maxHeight.doubleValue() / calculatedHeight.doubleValue()));
                            }
                        } else {
                            if (calculatedWidth.intValue() > maxWidth.intValue()) {
                                calculatedWidth = maxWidth;
                            } else {
                                if (calculatedHeight.intValue() > maxHeight.intValue()) {
                                    calculatedWidth = Integer.valueOf((int) (calculatedWidth.doubleValue()
                                            * maxHeight.doubleValue() / calculatedHeight.doubleValue()));
                                }
                            }
                        }
                        historyEntry.setNewValue(calculatedWidth.toString());
                    }
                    historyEntry.setOldValue(attachmentBean.getObjectID().toString());
                    historyEntries.add(historyEntry);
                }
            }
            flatValueList.add(flatHistoryBean);
        }
    }
    return flatValueList;
}

From source file:com.enonic.vertical.adminweb.handlers.xmlbuilders.ContentEnhancedImageXMLBuilder.java

private static String resolveFilenameForScaledImage(String originalImageName, BufferedImage image,
        String fileType) {/*  w w w .j  av  a  2 s.  c o  m*/
    StringBuffer name = new StringBuffer();
    name.append(originalImageName).append("_").append(image.getWidth()).append("x").append(image.getHeight());
    name.append(".").append(fileType);
    return name.toString();
}