Example usage for java.awt Image getWidth

List of usage examples for java.awt Image getWidth

Introduction

In this page you can find the example usage for java.awt Image getWidth.

Prototype

public abstract int getWidth(ImageObserver observer);

Source Link

Document

Determines the width of the image.

Usage

From source file:Main.java

/**
 * This method returns a buffered image with the contents of an image
 * This snippet was taken from: http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html
 * @param image//from ww  w.  ja v  a2 s  . c o  m
 * @return The buffered image
 */
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see e661 Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:net.rptools.lib.image.ThumbnailManager.java

private Image createThumbnail(File file) throws IOException {
    // Gather info
    File thumbnailFile = getThumbnailFile(file);
    if (thumbnailFile.exists()) {
        return ImageUtil.getImage(thumbnailFile);
    }/*from w w w. ja  v a2  s.  com*/

    Image image = ImageUtil.getImage(file);
    Dimension imgSize = new Dimension(image.getWidth(null), image.getHeight(null));

    // Test if we Should we bother making a thumbnail ?
    // Jamz: New size 100k (was 30k) and put in check so we're not creating thumbnails LARGER than the original...
    if (file.length() < 102400
            || (imgSize.width <= thumbnailSize.width && imgSize.height <= thumbnailSize.height)) {
        return image;
    }
    // Transform the image
    SwingUtil.constrainTo(imgSize, Math.min(image.getWidth(null), thumbnailSize.width),
            Math.min(image.getHeight(null), thumbnailSize.height));
    BufferedImage thumbnailImage = new BufferedImage(imgSize.width, imgSize.height,
            ImageUtil.pickBestTransparency(image));

    Graphics2D g = thumbnailImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawImage(image, 0, 0, imgSize.width, imgSize.height, null);
    g.dispose();

    // Use png to preserve transparency
    FileUtils.writeByteArrayToFile(thumbnailFile, ImageUtil.imageToBytes(thumbnailImage, "png"));

    return thumbnailImage;
}

From source file:org.kitodo.imagemanagementmodule.ImageManagementModuleIT.java

@Test
public void testChangeDpi() throws IOException, InfoException {
    assert new File(_00000001_TIF).exists();
    ImageManagementInterface module = new ImageManagementModule();
    Image image = module.changeDpi(new File(_00000001_TIF).toURI(), 300);
    assertThat(72 * image.getWidth(null) / new Info(_00000001_TIF, true).getImageWidth(), is(equalTo(300)));

}

From source file:org.kitodo.imagemanagement.ImageManagementIT.java

@Test
public void testChangeDpi() throws IOException, InfoException {
    assert new File(_00000001_TIF).exists();
    ImageManagementInterface module = new ImageManagement();
    Image image = module.changeDpi(new File(_00000001_TIF).toURI(), 300);
    assertEquals(300, 72 * image.getWidth(null) / new Info(_00000001_TIF, true).getImageWidth());

}

From source file:org.kitodo.imagemanagementmodule.ImageManagementModuleIT.java

@Test
public void testGetSizedWebImage() throws IOException {
    assert new File(_00000001_TIF).exists();
    ImageManagementInterface module = new ImageManagementModule();
    Image scaledWebImage = module.getSizedWebImage(new File(_00000001_TIF).toURI(), 150);
    assertThat(scaledWebImage.getWidth(null), is(equalTo(150)));
}

From source file:org.kitodo.imagemanagementmodule.ImageManagementModuleIT.java

@Test
public void testGetScaledWebImage() throws IOException, InfoException {
    assert new File(_00000001_TIF).exists();
    ImageManagementInterface module = new ImageManagementModule();
    Image scaledWebImage = module.getScaledWebImage(new File(_00000001_TIF).toURI(), 0.3);
    assertThat(scaledWebImage.getWidth(null),
            is((int) Math.round(0.3 * new Info(_00000001_TIF, true).getImageWidth())));
}

From source file:net.sf.maltcms.chromaui.project.spi.nodes.DescriptorNode.java

@Override
public Image getIcon(int type) {
    Image descrImage = DescriptorFactory.getImage(getBean());
    int w = descrImage.getWidth(null);
    int h = descrImage.getHeight(null);
    if (getBean() instanceof IColorizableDescriptor) {
        IColorizableDescriptor colorDescr = (IColorizableDescriptor) getBean();
        Color c = colorDescr.getColor();
        if (c != null) {
            BufferedImage bi = new BufferedImage(w / 10, h / 10, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = bi.createGraphics();

            g2.setColor(colorDescr.getColor());
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
            g2.fillRect(0, 0, bi.getWidth(), bi.getHeight());
            descrImage = ImageUtilities.mergeImages(bi, descrImage, w - bi.getWidth(), h - bi.getHeight());
        }//from www.j  a va2 s .  c o m
    }
    return descrImage;
}

From source file:org.kitodo.imagemanagement.ImageManagementIT.java

@Test
public void testGetSizedWebImage() throws IOException {
    assert new File(_00000001_TIF).exists();
    ImageManagementInterface module = new ImageManagement();
    Image scaledWebImage = module.getSizedWebImage(new File(_00000001_TIF).toURI(), 150);
    assertEquals(150, scaledWebImage.getWidth(null));
}

From source file:ScreenCapture.java

public void setImage(Image image) {
    this.image = image;
    setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this)));
    revalidate();//from   w  w w  . j  a  va 2  s.c o  m
    startPoint.x = endPoint.x;
    startPoint.y = endPoint.y;
    repaint();
}

From source file:ImageProcessingTest.java

/**
 * Open a file and load the image.//w w  w  .  j  a va  2 s . c o m
 */
public void openFile() {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));
    String[] extensions = ImageIO.getReaderFileSuffixes();
    chooser.setFileFilter(new FileNameExtensionFilter("Image files", extensions));
    int r = chooser.showOpenDialog(this);
    if (r != JFileChooser.APPROVE_OPTION)
        return;

    try {
        Image img = ImageIO.read(chooser.getSelectedFile());
        image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
        image.getGraphics().drawImage(img, 0, 0, null);
    } catch (IOException e) {
        JOptionPane.showMessageDialog(this, e);
    }
    repaint();
}