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:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java

private static BufferedImage resizeBorder(final BufferedImage border, int targetWidth, int targetHeight)
        throws IOException {
    if (targetWidth > border.getWidth() || targetHeight > border.getHeight()) {
        BufferedImage endImage = rescaleBorder(border, targetWidth, targetHeight);
        enforceBorderColors(endImage);//from  www. j av a2s  . c o  m
        return endImage;
    }

    int w = border.getWidth();
    int h = border.getHeight();
    int[] data = border.getRGB(0, 0, w, h, null, 0, w);
    int[] newData = new int[targetWidth * targetHeight];

    float widthRatio = (float) Math.max(targetWidth - 1, 1) / (float) Math.max(w - 1, 1);
    float heightRatio = (float) Math.max(targetHeight - 1, 1) / (float) Math.max(h - 1, 1);

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            if ((0xff000000 & data[y * w + x]) != 0) {
                int newX = Math.min(Math.round(x * widthRatio), targetWidth - 1);
                int newY = Math.min(Math.round(y * heightRatio), targetHeight - 1);
                newData[newY * targetWidth + newX] = data[y * w + x];
            }
        }
    }

    BufferedImage img = UIUtil.createImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
    img.setRGB(0, 0, targetWidth, targetHeight, newData, 0, targetWidth);

    return img;
}

From source file:com.htmlhifive.pitalium.core.io.FilePersisterTest.java

static int[] toRGB(BufferedImage image) {
    return image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
}

From source file:iqq.util.ImageUtil.java

/**
 * //from   ww  w.  j  a  v a2  s.co  m
 *
 * @param srcFile ?
 * @param destFile 
 * @param destWidth 
 * @param destHeight 
 */
public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) {
    if (type == Type.jdk) {
        try {
            BufferedImage srcBufferedImage = ImageIO.read(srcFile);
            int srcWidth = srcBufferedImage.getWidth();
            int srcHeight = srcBufferedImage.getHeight();
            int width = destWidth;
            int height = destHeight;
            if (srcHeight >= srcWidth) {
                width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth));
            } else {
                height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight));
            }
            BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = destBufferedImage.createGraphics();
            graphics2D.setBackground(BACKGROUND_COLOR);
            graphics2D.clearRect(0, 0, destWidth, destHeight);
            graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
                    (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null);
            graphics2D.dispose();

            FileOutputStream out = new FileOutputStream(destFile);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(destBufferedImage);
            param.setQuality((float) DEST_QUALITY / 100, false);
            encoder.setJPEGEncodeParam(param);
            encoder.encode(destBufferedImage);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        IMOperation operation = new IMOperation();
        operation.thumbnail(destWidth, destHeight);
        operation.gravity("center");
        operation.background(toHexEncoding(BACKGROUND_COLOR));
        operation.extent(destWidth, destHeight);
        operation.quality((double) DEST_QUALITY);
        operation.addImage(srcFile.getPath());
        operation.addImage(destFile.getPath());
        if (type == Type.graphicsMagick) {
            ConvertCmd convertCmd = new ConvertCmd(true);
            if (graphicsMagickPath != null) {
                convertCmd.setSearchPath(graphicsMagickPath);
            }
            try {
                convertCmd.run(operation);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IM4JavaException e) {
                e.printStackTrace();
            }
        } else {
            ConvertCmd convertCmd = new ConvertCmd(false);
            if (imageMagickPath != null) {
                convertCmd.setSearchPath(imageMagickPath);
            }
            try {
                convertCmd.run(operation);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IM4JavaException e) {
                e.printStackTrace();
            }
        }
    }
}

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

public static byte[] resizeImageScaleCrop(InputStream data, int img_width, int img_height, boolean isPNG) {
    BufferedImage originalImage;
    try {/*from  w  w  w .j a  va2s . c  o  m*/
        originalImage = ImageIO.read(data);
        Dimension origDimentsion = new Dimension(originalImage.getWidth(), originalImage.getHeight());
        Dimension fitDimentsion = new Dimension(img_width, img_height);

        int flag = testThresholdMinWidthHeightImage(origDimentsion, fitDimentsion);
        // size Anh dang: MinW < W < 720, MinH < H < 720.
        if (flag == -1) {
            data.reset();
            ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
            byte[] read = new byte[2048];
            int i = 0;
            while ((i = data.read(read)) > 0) {
                byteArray.write(read, 0, i);
            }
            data.close();
            return byteArray.toByteArray();
        } else if (flag == 0) {
            // size Anh dang: MinW < W < 720 < H || MinH < H < 720 < W
            double ratioWidth = (origDimentsion.width * 1.0) / fitDimentsion.width;
            double ratioHeight = (origDimentsion.height * 1.0) / fitDimentsion.height;
            if (ratioWidth < ratioHeight) {
                // fit width, crop height image.
                int yH = 0;
                if (origDimentsion.height > fitDimentsion.height) {
                    yH = (origDimentsion.height - fitDimentsion.height) / 2;
                }
                return cropBufferedImage(isPNG, originalImage, 0, yH, origDimentsion.width,
                        fitDimentsion.height);
            } else {
                // fit height, crop width image.
                int xW = 0;
                if (origDimentsion.width > fitDimentsion.width) {
                    xW = (origDimentsion.width - fitDimentsion.width) / 2;
                }
                return cropBufferedImage(isPNG, originalImage, xW, 0, fitDimentsion.width,
                        origDimentsion.height);
            }
        } else {
            // size Anh dang: 720 < W,H.
            // Scale Image.
            double ratioWidth = (origDimentsion.width * 1.0) / fitDimentsion.width;
            double ratioHeight = (origDimentsion.height * 1.0) / fitDimentsion.height;
            if (ratioWidth < ratioHeight) {
                // fit width, crop height image.
                int new_width = fitDimentsion.width;
                int new_height = (int) (origDimentsion.height / ratioWidth);
                int yH = 0;
                if (new_height > fitDimentsion.height) {
                    yH = (new_height - fitDimentsion.height) / 2;
                }

                byte[] scaleByteImage = scaleBufferedImage(isPNG, originalImage, 0, 0, new_width, new_height);
                if (scaleByteImage != null && scaleByteImage.length > 0) {
                    InputStream isImg = new ByteArrayInputStream(scaleByteImage);
                    BufferedImage scaleBufferedImage = ImageIO.read(isImg);

                    // Crop width image.
                    return cropBufferedImage(isPNG, scaleBufferedImage, 0, yH, fitDimentsion.width,
                            fitDimentsion.height);
                }
            } else {
                // fit height, crop width image.
                int new_width = (int) (origDimentsion.width / ratioHeight);
                int new_height = fitDimentsion.height;
                int xW = 0;
                if (new_width > fitDimentsion.width) {
                    xW = (new_width - fitDimentsion.width) / 2;
                }

                byte[] scaleByteImage = scaleBufferedImage(isPNG, originalImage, 0, 0, new_width, new_height);
                if (scaleByteImage != null && scaleByteImage.length > 0) {
                    InputStream isImg = new ByteArrayInputStream(scaleByteImage);
                    BufferedImage scaleBufferedImage = ImageIO.read(isImg);

                    // Crop height image.
                    return cropBufferedImage(isPNG, scaleBufferedImage, xW, 0, fitDimentsion.width,
                            fitDimentsion.height);
                }
            }
        }
    } catch (Exception ex) {
    }
    return null;
}

From source file:net.shopxx.util.ImageUtil.java

/**
 * //ww w .ja v  a2s. c o m
 * @param srcFile ?
 * @param destFile 
 * @param destWidth 
 * @param destHeight 
 */
public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) {
    Assert.notNull(srcFile);
    Assert.notNull(destFile);
    Assert.state(destWidth > 0);
    Assert.state(destHeight > 0);
    if (type == Type.jdk) {
        try {
            BufferedImage srcBufferedImage = ImageIO.read(srcFile);
            int srcWidth = srcBufferedImage.getWidth();
            int srcHeight = srcBufferedImage.getHeight();
            int width = destWidth;
            int height = destHeight;
            if (srcHeight >= srcWidth) {
                width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth));
            } else {
                height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight));
            }
            BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = destBufferedImage.createGraphics();
            graphics2D.setBackground(BACKGROUND_COLOR);
            graphics2D.clearRect(0, 0, destWidth, destHeight);
            graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
                    (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null);
            graphics2D.dispose();

            FileOutputStream out = new FileOutputStream(destFile);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(destBufferedImage);
            param.setQuality((float) DEST_QUALITY / 100, false);
            encoder.setJPEGEncodeParam(param);
            encoder.encode(destBufferedImage);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        IMOperation operation = new IMOperation();
        operation.thumbnail(destWidth, destHeight);
        operation.gravity("center");
        operation.background(toHexEncoding(BACKGROUND_COLOR));
        operation.extent(destWidth, destHeight);
        operation.quality((double) DEST_QUALITY);
        operation.addImage(srcFile.getPath());
        operation.addImage(destFile.getPath());
        if (type == Type.graphicsMagick) {
            ConvertCmd convertCmd = new ConvertCmd(true);
            if (graphicsMagickPath != null) {
                convertCmd.setSearchPath(graphicsMagickPath);
            }
            try {
                convertCmd.run(operation);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IM4JavaException e) {
                e.printStackTrace();
            }
        } else {
            ConvertCmd convertCmd = new ConvertCmd(false);
            if (imageMagickPath != null) {
                convertCmd.setSearchPath(imageMagickPath);
            }
            try {
                convertCmd.run(operation);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IM4JavaException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:editeurpanovisu.ReadWriteImage.java

public static void writeTiff(Image imgImage, String strNomFich, boolean bSharpen, float sharpenLevel)
        throws ImageReadException, IOException {
    File file = new File(strNomFich);
    BufferedImage imageRGBSharpen = null;
    BufferedImage imageRGB = SwingFXUtils.fromFXImage(imgImage, null);

    Graphics2D graphics = imageRGB.createGraphics();
    graphics.drawImage(imageRGB, 0, 0, null);
    if (bSharpen) {
        imageRGBSharpen = new BufferedImage(imageRGB.getWidth(), imageRGB.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Kernel kernel = new Kernel(3, 3, sharpenMatrix);
        ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        cop.filter(imageRGB, imageRGBSharpen);
    }/*from  ww w . j a  v a 2s  . c om*/

    final ImageFormat format = ImageFormats.TIFF;
    final Map<String, Object> params = new HashMap<>();
    params.put(ImagingConstants.PARAM_KEY_COMPRESSION,
            new Integer(TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED));

    if (bSharpen) {
        try {
            Imaging.writeImage(imageRGBSharpen, file, format, params);
        } catch (ImageWriteException ex) {
            Logger.getLogger(ReadWriteImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        try {
            Imaging.writeImage(imageRGB, file, format, params);
        } catch (ImageWriteException ex) {
            Logger.getLogger(ReadWriteImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

From source file:com.alvermont.terraj.stargen.ui.UIUtils.java

/**
 * Get a JLabel object to display with star details
 * /*w w  w .  j ava2 s  .c o  m*/
 * @throws java.io.IOException If there is an error building the list
 * @return A <code>JLabel</code> representing the star
 */
public static JLabel getSunLabel() throws IOException {
    BufferedImage bi = UIUtils.getImage("Sun");

    bi = UIUtils.scaleImage(bi, 30, 120);

    ImageIcon icon = new ImageIcon(bi);

    JLabel label = new JLabel(icon);
    label.setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
    label.setMinimumSize(new Dimension(bi.getWidth(), bi.getHeight()));

    label.setToolTipText("The Star");

    return label;
}

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

private static void enforceBorderColors(BufferedImage inputImage) {
    Graphics2D g = inputImage.createGraphics();
    g.setBackground(new Color(0, 0, 0, 0));
    g.clearRect(1, 1, inputImage.getWidth() - 2, inputImage.getHeight() - 2);
    g.dispose();//from   ww w.j av a 2s.  c o m
    int w = inputImage.getWidth();
    int h = inputImage.getHeight();
    int[] rgb = new int[w * h];

    inputImage.getRGB(0, 0, w, h, rgb, 0, w);

    for (int i = 0; i < rgb.length; i++) {
        if ((0xff000000 & rgb[i]) != 0) {
            rgb[i] = 0xff000000;
        }
    }
    inputImage.setRGB(0, 0, w, h, rgb, 0, w);
}

From source file:Snippet156.java

static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
                data.setPixel(x, y, pixel);
            }/*from   ww  w . ja  v a 2  s.  c  om*/
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

From source file:org.eclipse.swt.snippets.Snippet156.java

static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int rgb = bufferedImage.getRGB(x, y);
                int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
                data.setPixel(x, y, pixel);
                if (colorModel.hasAlpha()) {
                    data.setAlpha(x, y, (rgb >> 24) & 0xFF);
                }//from w ww .  ja  v a 2 s  .  com
            }
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}