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:org.jamwiki.utils.ImageUtil.java

/**
 * Resize an image, using a maximum dimension value.  Image dimensions will
 * be constrained so that the proportions are the same, but neither the width
 * or height exceeds the value specified.
 *//* w w  w.  j  a  va  2s .c  om*/
private static BufferedImage resizeImage(WikiImage wikiImage, int maxDimension) throws Exception {
    File imageFile = new File(Environment.getValue(Environment.PROP_FILE_DIR_FULL_PATH), wikiImage.getUrl());
    BufferedImage original = ImageUtil.loadImage(imageFile);
    maxDimension = calculateImageIncrement(maxDimension);
    int increment = Environment.getIntValue(Environment.PROP_IMAGE_RESIZE_INCREMENT);
    if (increment <= 0 || (maxDimension > original.getWidth() && maxDimension > original.getHeight())) {
        // let the browser scale the image
        return original;
    }
    String newUrl = wikiImage.getUrl();
    int pos = newUrl.lastIndexOf('.');
    if (pos > -1) {
        newUrl = newUrl.substring(0, pos) + "-" + maxDimension + "px" + newUrl.substring(pos);
    } else {
        newUrl += "-" + maxDimension + "px";
    }
    wikiImage.setUrl(newUrl);
    File newImageFile = new File(Environment.getValue(Environment.PROP_FILE_DIR_FULL_PATH), newUrl);
    if (newImageFile.exists()) {
        return ImageUtil.loadImage(newImageFile);
    }
    int width = -1;
    int height = -1;
    if (original.getWidth() >= original.getHeight()) {
        width = maxDimension;
    } else {
        height = maxDimension;
    }
    Image resized = null;
    try {
        resized = original.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
    } catch (Throwable t) {
        logger.severe(
                "Unable to resize image.  This problem sometimes occurs due to dependencies between Java and X on UNIX systems.  Consider enabling an X server or setting the java.awt.headless parameter to true for your JVM.",
                t);
        resized = original;
    }
    BufferedImage bufferedImage = null;
    if (resized instanceof BufferedImage) {
        bufferedImage = (BufferedImage) resized;
    } else {
        bufferedImage = ImageUtil.imageToBufferedImage(resized);
    }
    ImageUtil.saveImage(bufferedImage, newImageFile);
    return bufferedImage;
}

From source file:com.t3.image.ImageUtil.java

/**
 * Flip the image and return a new image
 * @param direction 0-nothing, 1-horizontal, 2-vertical, 3-both
 * @return/*from w  w  w.ja  va 2 s  .c o m*/
 */
public static BufferedImage flip(BufferedImage image, int direction) {
    BufferedImage workImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getTransparency());

    boolean flipHorizontal = (direction & 1) == 1;
    boolean flipVertical = (direction & 2) == 2;

    int workW = image.getWidth() * (flipHorizontal ? -1 : 1);
    int workH = image.getHeight() * (flipVertical ? -1 : 1);
    int workX = flipHorizontal ? image.getWidth() : 0;
    int workY = flipVertical ? image.getHeight() : 0;

    Graphics2D wig = workImage.createGraphics();
    wig.drawImage(image, workX, workY, workW, workH, null);
    wig.dispose();

    return workImage;
}

From source file:Main.java

/**
 * Snapshots the specified JavaFX {@link Image} object and stores a
 * copy of its pixels into a {@link BufferedImage} object, creating
 * a new object if needed.//from   w  w w .j a  v a 2  s.c  om
 * The method will only convert a JavaFX {@code Image} that is readable
 * as per the conditions on the
 * {@link Image#getPixelReader() Image.getPixelReader()}
 * method.
 * If the {@code Image} is not readable, as determined by its
 * {@code getPixelReader()} method, then this method will return null.
 * If the {@code Image} is a writable, or other dynamic image, then
 * the {@code BufferedImage} will only be set to the current state of
 * the pixels in the image as determined by its {@link PixelReader}.
 * Further changes to the pixels of the {@code Image} will not be
 * reflected in the returned {@code BufferedImage}.
 * <p>
 * The optional {@code BufferedImage} parameter may be reused to store
 * the copy of the pixels.
 * A new {@code BufferedImage} 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 img the JavaFX {@code Image} to be converted
 * @param bimg an optional {@code BufferedImage} object that may be
 *        used to store the returned pixel data
 * @return a {@code BufferedImage} containing a snapshot of the JavaFX
 *         {@code Image}, or null if the {@code Image} is not readable.
 * @since JavaFX 2.2
 */
public static BufferedImage fromFXImage(Image img, BufferedImage bimg) {
    PixelReader pr = img.getPixelReader();
    if (pr == null) {
        return null;
    }
    int iw = (int) img.getWidth();
    int ih = (int) img.getHeight();
    int prefBimgType = getBestBufferedImageType(pr.getPixelFormat(), bimg);
    if (bimg != null) {
        int bw = bimg.getWidth();
        int bh = bimg.getHeight();
        if (bw < iw || bh < ih || bimg.getType() != prefBimgType) {
            bimg = null;
        } else if (iw < bw || ih < bh) {
            Graphics2D g2d = bimg.createGraphics();
            g2d.setComposite(AlphaComposite.Clear);
            g2d.fillRect(0, 0, bw, bh);
            g2d.dispose();
        }
    }
    if (bimg == null) {
        bimg = new BufferedImage(iw, ih, prefBimgType);
    }
    IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster();
    int offset = icr.getDataOffset(0);
    int scan = icr.getScanlineStride();
    int data[] = icr.getDataStorage();
    WritablePixelFormat<IntBuffer> pf = getAssociatedPixelFormat(bimg);
    pr.getPixels(0, 0, iw, ih, pf, data, offset, scan);
    return bimg;
}

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

/**
 * Build a list of label objects from a list of planets 
 *
 * @param planets The list of planets to build labels for
 * @throws java.io.IOException If there is an error building the labels
 * @return A list of <code>JLabel</code> objects in the same order as the
 * input list//from w w  w .  j a v  a2s.  co m
 */
public static List<JLabel> buildImages(List<Planet> planets) throws IOException {
    List<JLabel> labels = new ArrayList<JLabel>();

    List<BufferedImage> images = getPlanetImages(planets);

    Planet p = planets.get(0);

    for (BufferedImage bi : images) {
        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("<html>" + UIUtils.getInfo(p) + "</html>");

        labels.add(label);

        log.debug("Added icon for planet " + p.getNumber() + " size " + bi.getWidth() + "," + bi.getHeight());

        p = p.getNextPlanet();
    }

    return labels;
}

From source file:ImageProcessing.ImageProcessing.java

public static void copyImage(BufferedImage source_image, BufferedImage target_image) {
    //Copies pixel data from source image to target image.
    //The size will not be copied/adjusted, so keep in mind the size of both images.
    for (int i = 0; i < target_image.getHeight(); i++) {
        for (int j = 0; j < target_image.getWidth(); j++) {
            int rgb = source_image.getRGB(j, i);
            target_image.setRGB(j, i, rgb);
        }/*from w  w  w  .j  a va  2s  . co m*/
    }
}

From source file:cn.z.Ocr5.java

public static int getBlackCount(BufferedImage img) {
    final int width = img.getWidth();
    final int height = img.getHeight();
    int count = 0;
    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;/*from w  w  w  .j av a2s .c  o m*/
            }
        }
    }
    return count;
}

From source file:GraphicsUtil.java

public static void drawImage(Graphics g, BufferedImage img, Rectangle2D bounds, ImageObserver observer) {
    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(img, // what to draw
            (int) bounds.getMinX(), // dest left
            (int) bounds.getMinY(), // dest top
            (int) bounds.getMaxX(), // dest right
            (int) bounds.getMaxY(), // dest bottom
            0, // src left
            0, // src top
            img.getWidth(), // src right
            img.getHeight(), // src bottom
            observer // to notify of image updates
    );/*w w  w . ja  v a  2  s  .  co  m*/
}

From source file:cn.z.Ocr5.java

public static boolean isNotEight(BufferedImage img) {
    final int width = img.getWidth();
    final int height = img.getHeight();
    int minCount = width;
    for (int y = height / 2 - 2; y < height / 2 + 2; ++y) {
        int count = 0;
        for (int x = 0; x < width / 2 + 2; ++x) {
            if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;// w w  w  . j a v a 2s .c o m
            }
        }
        minCount = Math.min(count, minCount);
    }
    return minCount < 2;
}

From source file:cn.z.Ocr5.java

public static boolean isNotThree(BufferedImage img) {
    final int width = img.getWidth();
    final int height = img.getHeight();
    int minCount = width;
    for (int y = height / 2 - 3; y < height / 2 + 3; ++y) {
        int count = 0;
        for (int x = 0; x < width / 2 + 1; ++x) {
            if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;/*from www  . j av  a 2 s  .com*/
            }
        }
        minCount = Math.min(count, minCount);
    }
    return minCount > 0;
}

From source file:cn.z.Ocr5.java

public static boolean isNotFive(BufferedImage img) {
    final int width = img.getWidth();
    final int height = img.getHeight();
    int minCount = width;
    for (int y = 0; y < height / 3; ++y) {
        int count = 0;
        for (int x = width * 2 / 3; x < width; ++x) {
            if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;/*from   ww w . ja va 2s.com*/
            }
        }
        minCount = Math.min(count, minCount);
    }
    return minCount > 0;
}