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.smash.revolance.ui.model.helper.ImageHelper.java

/**
 * diff = (img2.rgb(x,y) != img1.rgb(x,y)) ? img1.rgb() : 0
 *
 * @param img1//from   w  w w .  j  av  a 2  s .  co  m
 * @param img2
 * @return
 * @throws IOException
 */
public static BufferedImage diffImg(BufferedImage img1, BufferedImage img2) throws IOException {
    int w = img1.getWidth();
    int h = img1.getHeight();
    int t = img1.getType();

    BufferedImage diff = new BufferedImage(w, h, t);

    if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) {
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                int refIntensity = img1.getRGB(x, y);
                int imgIntensity = img2.getRGB(x, y);
                if (refIntensity != imgIntensity) {
                    diff.setRGB(x, y, imgIntensity);
                } else {
                    diff.setRGB(x, y, 0);
                }
            }
        }
    }

    return diff;
}

From source file:es.logongas.util.seguridad.CodigoVerificacionSeguro.java

/**
 * Esta funcin se aplica pq para imagenes muy grandes no reconoce el cdigo
 * QR/*  w  w  w  . j  ava  2 s.c o  m*/
 *
 * @param originalImage La imagen original
 * @param f El factor de escalado
 * @return La iamgen reescalada.
 */
private static BufferedImage resizeImage(BufferedImage originalImage, double f) {
    int ancho = (int) (((double) originalImage.getWidth()) * f);
    int alto = (int) (((double) originalImage.getHeight()) * f);
    int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
    BufferedImage resizedImage = new BufferedImage(ancho, alto, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, ancho, alto, null);
    g.dispose();

    return resizedImage;
}

From source file:ImageClip.java

/**
 * Clips the input image to the specified shape
 * //from   ww w  .j  a  v a2  s . com
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return The smallest image containing those pixels that fall
 *         inside the clip shape
 */
public static BufferedImage clip(BufferedImage image, float... clipVerts) {
    assert clipVerts.length >= 6;
    assert clipVerts.length % 2 == 0;

    int[] xp = new int[clipVerts.length / 2];
    int[] yp = new int[xp.length];

    int minX = image.getWidth(), minY = image.getHeight(), maxX = 0, maxY = 0;

    for (int j = 0; j < xp.length; j++) {
        xp[j] = Math.round(clipVerts[2 * j] * image.getWidth());
        yp[j] = Math.round(clipVerts[2 * j + 1] * image.getHeight());

        minX = Math.min(minX, xp[j]);
        minY = Math.min(minY, yp[j]);
        maxX = Math.max(maxX, xp[j]);
        maxY = Math.max(maxY, yp[j]);
    }

    for (int i = 0; i < xp.length; i++) {
        xp[i] -= minX;
        yp[i] -= minY;
    }

    Polygon clip = new Polygon(xp, yp, xp.length);
    BufferedImage out = new BufferedImage(maxX - minX, maxY - minY, image.getType());
    Graphics g = out.getGraphics();
    g.setClip(clip);

    g.drawImage(image, -minX, -minY, null);
    g.dispose();

    return out;
}

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

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

    GifDecoder dec = new GifDecoder();
    AnimatedGifEncoder enc = new AnimatedGifEncoder();
    dec.read(stream_file);//w  w w. j  av a  2  s  .  c  o m

    int cnt = dec.getFrameCount();

    int delay = 0;
    int width = 0;
    int height = 0;

    try {
        enc.start(save);
        enc.setRepeat(0);

        for (int i = 0; i < cnt; i++) {
            BufferedImage frame = dec.getFrame(i);
            delay = dec.getDelay(i);

            width = frame.getWidth();
            height = frame.getHeight();

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

            BufferedImage destimg = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g = destimg.createGraphics();

            g.drawImage(frame, 0, 0, width, height, null);
            enc.setDelay(delay);

            enc.addFrame(destimg);
        }

        enc.finish();
    } catch (Exception ex) {
        log.error(ex);
    }
}

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

public static void getGifImageThumbnail(BufferedInputStream stream_file, String save, String type, int w,
        int h) {/*from w  ww  .  j av a  2s.c o  m*/

    GifDecoder dec = new GifDecoder();
    AnimatedGifEncoder enc = new AnimatedGifEncoder();
    dec.read(stream_file);

    int cnt = dec.getFrameCount();

    int delay = 0;
    int width = 0;
    int height = 0;

    try {
        enc.start(save);
        enc.setRepeat(0);

        for (int i = 0; i < cnt; i++) {
            BufferedImage frame = dec.getFrame(i);
            delay = dec.getDelay(i);

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

            BufferedImage destimg = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g = destimg.createGraphics();

            g.drawImage(frame, 0, 0, width, height, null);
            enc.setDelay(delay);

            enc.addFrame(destimg);
        }

        enc.finish();
    } catch (Exception ex) {
        log.error(ex);
    }
}

From source file:com.tractionsoftware.reshoot.Reshoot.java

private static boolean isRetina(RemoteWebDriver driver, BufferedImage img) {
    Dimension size = driver.manage().window().getSize();
    return (size.width == img.getWidth() / 2);
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public final static BufferedImage getSubimage(BufferedImage image, int x, int y, int width, int height) {
    if (width > image.getWidth() - x)
        width = image.getWidth() - x;//from   ww w .  j  ava2 s .  c om
    if (height > image.getHeight() - y)
        height = image.getHeight() - y;
    return image.getSubimage(x, y, width, height);
}

From source file:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java

private static BufferedImage toThumbnailSize(BufferedImage image) {
    final int w = image.getWidth();
    final int h = image.getHeight();
    final int max = Math.max(w, h);
    if (max <= 256) {
        return image;
    }/*  w w w.  j av a 2s .c  o  m*/
    final double s = max / 256d;
    final BufferedImage result = new BufferedImage(Math.max((int) Math.min(w / s, 256), 1),
            Math.max((int) Math.min(h / s, 256), 1), BufferedImage.TYPE_INT_BGR);
    final Graphics2D graphics = result.createGraphics();
    try {
        graphics.drawImage(image.getScaledInstance(result.getWidth(), result.getHeight(), Image.SCALE_SMOOTH),
                0, 0, null);
    } finally {
        graphics.dispose();
    }
    return result;
}

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

public static BufferedImage removeAlphaChannel(BufferedImage src) {
    //if (src.getColorModel().hasAlpha()) {
    BufferedImage image = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.setComposite(AlphaComposite.Src);
    g.drawImage(src, 0, 0, null);/*  www  . ja v a 2 s.co  m*/
    g.dispose();
    return image;
    //}
    //return src;
    /*
     * BufferedImage rgbImage = new BufferedImage(src.getWidth(),
     * src.getHeight(), BufferedImage.TYPE_3BYTE_BGR); for (int x = 0; x <
     * src.getWidth(); ++x) { for (int y = 0; y < src.getHeight(); ++y) {
     * rgbImage.setRGB(x, y, src.getRGB(x, y) & 0xFFFFFF); } } return
     * rgbImage;
     */
}

From source file:org.jamwiki.utils.ImageUtil.java

/**
 *
 *///from   w  ww  . j  a v a  2  s  . c o m
private static void setScaledDimensions(BufferedImage bufferedImage, WikiImage wikiImage, int maxDimension) {
    int width = bufferedImage.getWidth();
    int height = bufferedImage.getHeight();
    if (width >= height) {
        height = (int) Math.floor(((double) maxDimension / (double) width) * (double) height);
        width = maxDimension;
    } else {
        width = (int) Math.floor(((double) maxDimension / (double) height) * (double) width);
        height = maxDimension;
    }
    wikiImage.setWidth(width);
    wikiImage.setHeight(height);
}