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:GraphicsUtil.java

public static void drawImage(Graphics g, BufferedImage img, ImageObserver observer) {
    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), observer);
}

From source file:Main.java

/**
 * Snapshots the specified {@link BufferedImage} and stores a copy of
 * its pixels into a JavaFX {@link Image} object, creating a new
 * object if needed.//from   w ww  .  j a  v  a2s  .  c  om
 * The returned {@code Image} will be a static snapshot of the state
 * of the pixels in the {@code BufferedImage} at the time the method
 * completes.  Further changes to the {@code BufferedImage} will not
 * be reflected in the {@code Image}.
 * <p>
 * The optional JavaFX {@link WritableImage} parameter may be reused
 * to store the copy of the pixels.
 * A new {@code Image} will be created if the supplied object is null,
 * is too small or of a type which the image pixels cannot be easily
 * converted into.
 * 
 * @param bimg the {@code BufferedImage} object to be converted
 * @param wimg an optional {@code WritableImage} object that can be
 *        used to store the returned pixel data
 * @return an {@code Image} object representing a snapshot of the
 *         current pixels in the {@code BufferedImage}.
 * @since JavaFX 2.2
 */
public static WritableImage toFXImage(BufferedImage bimg, WritableImage wimg) {
    int bw = bimg.getWidth();
    int bh = bimg.getHeight();
    switch (bimg.getType()) {
    case BufferedImage.TYPE_INT_ARGB:
    case BufferedImage.TYPE_INT_ARGB_PRE:
        break;
    default:
        BufferedImage converted = new BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB_PRE);
        Graphics2D g2d = converted.createGraphics();
        g2d.drawImage(bimg, 0, 0, null);
        g2d.dispose();
        bimg = converted;
        break;
    }
    // assert(bimg.getType == TYPE_INT_ARGB[_PRE]);
    if (wimg != null) {
        int iw = (int) wimg.getWidth();
        int ih = (int) wimg.getHeight();
        if (iw < bw || ih < bh) {
            wimg = null;
        } else if (bw < iw || bh < ih) {
            int empty[] = new int[iw];
            PixelWriter pw = wimg.getPixelWriter();
            PixelFormat<IntBuffer> pf = PixelFormat.getIntArgbPreInstance();
            if (bw < iw) {
                pw.setPixels(bw, 0, iw - bw, bh, pf, empty, 0, 0);
            }
            if (bh < ih) {
                pw.setPixels(0, bh, iw, ih - bh, pf, empty, 0, 0);
            }
        }
    }
    if (wimg == null) {
        wimg = new WritableImage(bw, bh);
    }
    PixelWriter pw = wimg.getPixelWriter();
    IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster();
    int data[] = icr.getDataStorage();
    int offset = icr.getDataOffset(0);
    int scan = icr.getScanlineStride();
    PixelFormat<IntBuffer> pf = (bimg.isAlphaPremultiplied() ? PixelFormat.getIntArgbPreInstance()
            : PixelFormat.getIntArgbInstance());
    pw.setPixels(0, 0, bw, bh, pf, data, offset, scan);
    return wimg;
}

From source file:ImageUtil.java

public static ByteArrayOutputStream fit(ByteArrayInputStream bais, int width, int height) throws IOException {
    BufferedImage src = ImageIO.read(bais);
    int newWidth;
    int newHeight;

    Float scale;/*from   ww  w  .j  a v  a  2 s.c  o m*/
    if (src.getWidth() > src.getHeight()) {
        scale = Float.valueOf(width) / Float.valueOf(src.getWidth());
    } else {
        scale = Float.valueOf(height) / Float.valueOf(src.getHeight());
    }

    newWidth = Float.valueOf(src.getWidth() * scale).intValue();
    newHeight = Float.valueOf(src.getHeight() * scale).intValue();

    // System.out.println("--- " + src.getWidth() + " - " + width);
    // System.out.println("--- " + src.getHeight() + " - " + height);
    // System.out.println("--- " + scale + " -- " + Float.valueOf(src.getWidth() * scale).intValue() + " -- " + Float.valueOf(src.getHeight() * scale).intValue());

    BufferedImage temp = scale(src, newWidth, newHeight);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(temp, "JPG", baos);

    return baos;
}

From source file:Main.java

/**
 * Pads the given {@link BufferedImage} on all sides by the given padding amount.
 *
 * @param source  The source image.//from  w w  w .  j  a va  2 s  .  c  om
 * @param padding The amount to pad on all sides, in pixels.
 * @return A new, padded image, or the source image if no padding is performed.
 */
public static BufferedImage paddedImage(BufferedImage source, int padding) {
    if (padding == 0) {
        return source;
    }

    BufferedImage newImage = newArgbBufferedImage(source.getWidth() + padding * 2,
            source.getHeight() + padding * 2);
    Graphics2D g = (Graphics2D) newImage.getGraphics();
    g.drawImage(source, padding, padding, null);
    return newImage;
}

From source file:Main.java

/**
 * Quickly crops an image from its source.
 * @param src The source image.//from  w  w  w .j a  v  a  2  s.c  om
 * @param x The x coordinate to start at.
 * @param y The y coordinate to start at.
 * @param width The width to clip.
 * @param height The height to clip.
 * @return
 */
public static BufferedImage imgUtilFastCrop(BufferedImage src, int x, int y, int width, int height) {
    if (src == null)
        return null;
    if (x == 0 && y == 0 && width == src.getWidth() && height == src.getHeight())
        return imgUtilFastCopy(src);
    else {
        BufferedImage b = new BufferedImage(width, height, src.getType());
        Graphics g = b.getGraphics();
        g.drawImage(src, -x, -y, null);
        g.dispose();
        return b;
    }
}

From source file:filtros.histograma.Histograma.java

public static void GetData(BufferedImage image) {
    //Obtem a largura e a altura da imagem original
    int width = image.getWidth();
    int height = image.getHeight();

    //Inicializa o array
    data = new int[256];

    //Aplica o filtro de escala de cinza na imagem
    BufferedImage temp = GrayScale.Apply(image);

    //Loop que percorre todos os pixels da imagem
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            //Obtem o valor RGB do pixel
            int pixel = temp.getRGB(i, j);
            int r = (int) ((pixel & 0x00FF0000) >>> 16); //Recupera o valor de Red
            int g = (int) ((pixel & 0x0000FF00) >>> 8); //Recupera o valor de Green
            int b = (int) (pixel & 0x000000FF); //Recupera o valor de Blue

            //Incrementa o valor do array na posio referente ao tom RGB
            data[r] += 1;//from   www .  jav  a2  s.c  om
            data[g] += 1;
            data[b] += 1;
        }
    }
}

From source file:UploadImage.java

public static BufferedImage shrink(BufferedImage image, int n) {

    int w = image.getWidth() / n;
    int h = image.getHeight() / n;

    BufferedImage shrunkImage = new BufferedImage(w, h, image.getType());

    for (int y = 0; y < h; ++y)
        for (int x = 0; x < w; ++x)
            shrunkImage.setRGB(x, y, image.getRGB(x * n, y * n));

    return shrunkImage;
}

From source file:Main.java

/**
 * This method reads the input image from the input from
 * start pixel height (y1) until it reads the first next row
 * where all pixel are white by height and return that value
 * @param Img - input image that will be read
 * @param y1 - input start height pixel of image
 * @return - returns the value of height when conditions are true
 *//*ww  w .j  a  va2  s .c o  m*/
private static int trimLockdown(BufferedImage Img, int y1) {

    for (int j = y1 + 1; j < Img.getHeight(); j++) {
        int counterWhite = 0;
        for (int i = 0; i < Img.getWidth(); i++) {
            if (Img.getRGB(i, j) == -1) {
                counterWhite++;
            }
        }
        if (counterWhite == Img.getWidth()) {
            //this is a chek for dots over the letters i and j
            //so they wont be missread as dots
            if (j > (Img.getHeight() / 2)) {
                return j;
            }
        }
        if (j == Img.getHeight() - 1) {
            return j + 1;
        }
    }
    return 0;
}

From source file:com.fun.util.TesseractUtil.java

/**
 * /*from  w w w . j ava2 s  .  c  om*/
 *
 * @param imageFile
 * @param times
 * @param targetFile
 * @throws IOException
 */
private static void scaled(File imageFile, int times, File targetFile) throws IOException {
    BufferedImage image = ImageIO.read(imageFile);
    int targetWidth = image.getWidth() * times;
    int targetHeight = image.getHeight() * times;
    int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage tmp = new BufferedImage(targetWidth, targetHeight, type);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.drawImage(image, 0, 0, targetWidth, targetHeight, null);
    g2.dispose();
    ImageIO.write(tmp, "png", targetFile);
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 8-bit colour using the default 256-colour
 * palette. No transparency./*w  ww .  ja v a  2 s  . c o  m*/
 * 
 * @param src
 *            the source image to convert
 * @return a copy of the source image with an 8-bit colour depth
 */
public static BufferedImage convert8(BufferedImage src) {
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);
    return dest;
}