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.simiacryptus.mindseye.test.data.CIFAR10.java

private static LabeledObject<BufferedImage> toImage(final byte[] b) {
    @Nonnull
    final BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            final int red = 0xFF & b[1 + 1024 * 0 + x + y * 32];
            final int blue = 0xFF & b[1 + 1024 * 1 + x + y * 32];
            final int green = 0xFF & b[1 + 1024 * 2 + x + y * 32];
            final int c = (red << 16) + (blue << 8) + green;
            img.setRGB(x, y, c);/*from   w  ww .j av a 2s .  c  o m*/
        }
    }
    return new LabeledObject<>(img, Arrays.toString(new byte[] { b[0] }));
}

From source file:Main.java

/**
 * Changes the opacity of a given image.
 * @param src The source image.//  w ww  . ja v a  2  s .  com
 * @param opacity The opacity to change to.
 * @return
 */
public static BufferedImage imgUtilAdjustImageTransparency(BufferedImage src, float opacity) {
    if (src == null)
        return null;
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity);
    BufferedImage cpy = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) cpy.getGraphics();
    g.setComposite(ac);
    g.drawImage(src, 0, 0, null);
    g.dispose();
    return cpy;
}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void getImageThumbnail(BufferedInputStream stream_file, String save, String type, int w, int h) {

    try {/*from   w w w  .java 2 s  . c  o  m*/
        File file = new File(save);
        BufferedImage bi = ImageIO.read(stream_file);

        int width = bi.getWidth();
        int height = bi.getHeight();
        if (w < width) {
            width = w;
        }
        if (h < height) {
            height = h;
        }

        BufferedImage bufferIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Image atemp = bi.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);

        Graphics2D g2 = bufferIm.createGraphics();
        g2.drawImage(atemp, 0, 0, width, height, null);
        ImageIO.write(bufferIm, type, file);
    } catch (Exception e) {
        log.error(e);
    }

}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void getImageThumbnail(BufferedInputStream stream_file, String save, String type, int w) {

    try {/*from   w w  w  .ja v  a 2s .c o  m*/
        File file = new File(save);
        BufferedImage bi = ImageIO.read(stream_file);

        int width = bi.getWidth();
        int height = bi.getHeight();

        double ratio = (double) height / width;
        height = (int) (w * ratio);
        if (w < width) {
            width = w;
        }

        BufferedImage bufferIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Image atemp = bi.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);

        Graphics2D g2 = bufferIm.createGraphics();
        g2.drawImage(atemp, 0, 0, width, height, null);
        ImageIO.write(bufferIm, type, file);
    } catch (Exception e) {
        log.error(e);
    }

}

From source file:Main.java

private static void applyShadow(BufferedImage image, int shadowSize, Color shadowColor, float shadowOpacity) {
    int dstWidth = image.getWidth();
    int dstHeight = image.getHeight();

    int left = (shadowSize - 1) >> 1;
    int right = shadowSize - left;
    int xStart = left;
    int xStop = dstWidth - right;
    int yStart = left;
    int yStop = dstHeight - right;

    int shadowRgb = shadowColor.getRGB() & 0x00FFFFFF;

    int[] aHistory = new int[shadowSize];
    int historyIdx = 0;

    int aSum;/*from  w  w  w .ja  va  2  s  . c o  m*/

    int[] dataBuffer = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    int lastPixelOffset = right * dstWidth;
    float sumDivider = shadowOpacity / shadowSize;

    // horizontal pass

    for (int y = 0, bufferOffset = 0; y < dstHeight; y++, bufferOffset = y * dstWidth) {
        aSum = 0;
        historyIdx = 0;
        for (int x = 0; x < shadowSize; x++, bufferOffset++) {
            int a = dataBuffer[bufferOffset] >>> 24;
            aHistory[x] = a;
            aSum += a;
        }

        bufferOffset -= right;

        for (int x = xStart; x < xStop; x++, bufferOffset++) {
            int a = (int) (aSum * sumDivider);
            dataBuffer[bufferOffset] = a << 24 | shadowRgb;

            // substract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];

            // get the lastest pixel
            a = dataBuffer[bufferOffset + right] >>> 24;
            aHistory[historyIdx] = a;
            aSum += a;

            if (++historyIdx >= shadowSize) {
                historyIdx -= shadowSize;
            }
        }
    }

    // vertical pass
    for (int x = 0, bufferOffset = 0; x < dstWidth; x++, bufferOffset = x) {
        aSum = 0;
        historyIdx = 0;
        for (int y = 0; y < shadowSize; y++, bufferOffset += dstWidth) {
            int a = dataBuffer[bufferOffset] >>> 24;
            aHistory[y] = a;
            aSum += a;
        }

        bufferOffset -= lastPixelOffset;

        for (int y = yStart; y < yStop; y++, bufferOffset += dstWidth) {
            int a = (int) (aSum * sumDivider);
            dataBuffer[bufferOffset] = a << 24 | shadowRgb;

            // substract the oldest pixel from the sum
            aSum -= aHistory[historyIdx];

            // get the lastest pixel
            a = dataBuffer[bufferOffset + lastPixelOffset] >>> 24;
            aHistory[historyIdx] = a;
            aSum += a;

            if (++historyIdx >= shadowSize) {
                historyIdx -= shadowSize;
            }
        }
    }
}

From source file:com.partagames.imageresizetool.SimpleImageResizeTool.java

/**
 * Scales an image to the desired dimensions.
 *
 * @param img  Original image/*w ww. j  a v a2 s. c  o m*/
 * @param newW Target width
 * @param newH Target height
 * @return Scaled image
 */
public static BufferedImage scale(BufferedImage img, int newW, int newH) {
    int w = img.getWidth();
    int h = img.getHeight();
    final BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
    final Graphics2D g = dimg.createGraphics();

    // use provided rendering hint, default is bilinear 
    switch (scalingHint) {
    case "n":
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        break;
    case "b":
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        break;
    }

    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
    g.dispose();
    return dimg;
}

From source file:Main.java

public static String getImageDimension(String imgLocation) throws Exception {
    BufferedImage image = null;
    try {/*w ww  .j a v  a  2s.c o  m*/
        image = ImageIO.read(new URL(imgLocation));
    } catch (Exception e) {
        // If an error occurs, we consider the image as null. - FB
    }
    return (image == null ? "-1,-1" : image.getWidth() + "," + image.getHeight());
}

From source file:imageprocessingproject.ImageHistogram.java

public static BufferedImage normalizeImage(BufferedImage image) {
    int height = image.getHeight();
    int width = image.getWidth();

    int r, g, b, minr = 255, ming = 255, minb = 255, maxr = 0, maxg = 0, maxb = 0;
    Color c;//from  w  ww  .  j  av a2  s . c  om

    BufferedImage tempImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            if (minr > c.getRed()) {
                minr = c.getRed();
            }
            if (ming > c.getGreen()) {
                ming = c.getGreen();
            }
            if (minb > c.getBlue()) {
                minb = c.getBlue();
            }
            if (maxr < c.getRed()) {
                maxr = c.getRed();
            }
            if (maxg < c.getGreen()) {
                maxg = c.getGreen();
            }
            if (maxb < c.getBlue()) {
                maxb = c.getBlue();
            }
        }
    }

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            r = (int) ((c.getRed() - minr) * 255 / (maxr - minr));
            g = (int) ((c.getGreen() - ming) * 255 / (maxg - ming));
            b = (int) ((c.getBlue() - minb) * 255 / (maxb - minb));
            tempImage.setRGB(i, j, new Color(r, g, b, c.getAlpha()).getRGB());
        }
    }

    return tempImage;
}

From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java

public static BufferedImage convertRGBAToIndexed(BufferedImage src) {
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    Graphics g = dest.getGraphics();
    g.setColor(new Color(231, 20, 189));
    g.fillRect(0, 0, dest.getWidth(), dest.getHeight()); // fill with a
    // hideous color
    // and make it
    // transparent
    dest = makeTransparent(dest, 0, 0);// w  w w .ja v a2s  .  c om
    dest.createGraphics().drawImage(src, 0, 0, null);
    return dest;
}

From source file:com.openkm.util.ImageUtils.java

/**
 * pointWithinRange/*from  ww w . j  ava  2s .c o  m*/
 */
public static boolean pointWithinRange(Point p, BufferedImage img) {
    return !(p.x < 0 || p.y < 0 || p.x >= img.getWidth() || p.y >= img.getHeight());
}