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:de.romankreisel.faktotum.beans.BundesbruderBean.java

private Byte[] storeImageToByteArray(Image image) throws IOException {
    if (!(image instanceof RenderedImage)) {
        BufferedImage newImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        newImage.getGraphics().drawImage(image, 0, 0, null);
        image = newImage;// ww  w  . j ava  2 s . co  m
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write((RenderedImage) image, "jpg", byteArrayOutputStream);
    byteArrayOutputStream.flush();
    byte[] imageInByte = byteArrayOutputStream.toByteArray();
    byteArrayOutputStream.close();
    return ArrayUtils.toObject(imageInByte);
}

From source file:org.jtalks.jcommune.service.nontransactional.ImageConverterTest.java

@Test(dataProvider = "parameterResizeImage")
public void testResizeImage(int maxWidth, int maxHeight, int imageType, String format) throws IOException {
    int expectedWidth = 4;
    int expectedHeight = 4;
    imageConverter = new ImageConverter(format, imageType, maxWidth, maxHeight);
    BufferedImage originalImage = ImageIO
            .read(new MockMultipartFile("test_image", "test_image", "image/png", originalImageByteArray)
                    .getInputStream());//ww  w  .  j  av a  2  s. c  om
    Image modifiedImage = imageConverter.resizeImage(originalImage, imageType);
    assertEquals(modifiedImage.getWidth(null), expectedWidth);
    assertEquals(modifiedImage.getHeight(null), expectedHeight);
}

From source file:BufferedImageMouseDrag.java

DisplayCanvas() {
    setBackground(Color.white);/*from ww w . j  a  v a  2s.  c o  m*/
    setSize(450, 400);
    addMouseMotionListener(new MouseMotionHandler());

    Image image = getToolkit().getImage("largeJava2sLogo.gif");

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 1);
    try {
        mt.waitForAll();
    } catch (Exception e) {
        System.out.println("Exception while loading image.");
    }

    if (image.getWidth(this) == -1) {
        System.out.println("no gif file");
        System.exit(0);
    }

    bi = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bi.createGraphics();
    big.drawImage(image, 0, 0, this);
}

From source file:com.krawler.esp.handlers.genericFileUpload.java

public final void imgResizeCompany(String imagePath, int Width, int Height, String fName, boolean ori)
        throws IOException {
    try {/*from   w  w w  . j a va2s  . co m*/
        // Get a path to the image to resize.
        // ImageIcon is a kluge to make sure the image is fully
        // loaded before we proceed.
        Image sourceImage = new ImageIcon(Toolkit.getDefaultToolkit().getImage(imagePath)).getImage();
        int imageWidth = sourceImage.getWidth(null);
        int imageHeight = sourceImage.getHeight(null);
        if (ori) {
            Width = imageWidth;
            Height = imageHeight;
        } else {
            Width = imageWidth < Width ? imageWidth : Width;
            Height = imageHeight < Height ? imageHeight : Height;
            float imageRatio = ((float) imageWidth / (float) imageHeight);
            float framemageratio = ((float) Width / (float) Height);
            if (imageRatio > framemageratio) {
                float value = Width / imageRatio;
                Height = (int) value;

            } else {
                float value = Height * imageRatio;
                Width = (int) value;
            }
        }
        BufferedImage resizedImage = this.scaleCompanyImage(sourceImage, Width, Height);
        ImageIO.write(resizedImage, "PNG", new File(fName + ".png"));
        sourceImage.flush();
    } catch (Exception e) {
        this.ErrorMsg = "Problem occured while uploading logo";
        logger.warn(e.getMessage(), e);
    }
}

From source file:org.forumj.web.servlet.post.SetAvatar.java

private ImageSize getImageSize(Image image) {
    return new ImageSize(image.getHeight(null), image.getWidth(null));
}

From source file:IDlook.java

private void createThumbnail() {
    int maxDim = 350;
    try {//  w  w w .j  av a  2 s.c  om
        Image inImage = icon.getImage();

        double scale = (double) maxDim / (double) inImage.getHeight(null);
        if (inImage.getWidth(null) > inImage.getHeight(null)) {
            scale = (double) maxDim / (double) inImage.getWidth(null);
        }

        int scaledW = (int) (scale * inImage.getWidth(null));
        int scaledH = (int) (scale * inImage.getHeight(null));

        BufferedImage outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);

        AffineTransform tx = new AffineTransform();

        if (scale < 1.0d) {
            tx.scale(scale, scale);
        }

        Graphics2D g2d = outImage.createGraphics();
        g2d.drawImage(inImage, tx, null);
        g2d.dispose();

        iconThumbnail = new ImageIcon(outImage);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.pureinfo.tgirls.servlet.TestServlet.java

private BufferedImage getImg(File _temp, float _i) throws IOException {
    BufferedImage output;/*from  w w w.  j  av  a2s.c  om*/
    Image img = ImageIO.read(_temp);
    int width = img.getWidth(null);
    int height = img.getHeight(null);

    Float f = width * _i;
    width = f.intValue();
    f = height * _i;
    height = f.intValue();

    output = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = output.getGraphics();
    g.drawImage(img, 0, 0, width, height, null);
    g.dispose();
    return output;
}

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

private Image createThumbnail(File file) throws IOException {

    // Gather info
    File thumbnailFile = getThumbnailFile(file);
    if (thumbnailFile.exists()) {
        return ImageUtil.getImage(thumbnailFile);
    }/*ww  w .j a  v  a2  s .c o  m*/

    Image image = ImageUtil.getImage(file);

    // Should we bother making a thumbnail ?
    if (file.length() < 30 * 1024) {
        return image;
    }

    // Transform the image
    Dimension imgSize = new Dimension(image.getWidth(null), image.getHeight(null));
    SwingUtil.constrainTo(imgSize, thumbnailSize.width, 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();

    // Ensure path exists
    if (thumbnailFile.exists())
        thumbnailFile.delete();
    else
        thumbnailFile.getParentFile().mkdirs();

    try (OutputStream os = new FileOutputStream(thumbnailFile)) {
        IOUtils.write(ImageUtil.imageToBytes(thumbnailImage, "png"), os);
    }

    return thumbnailImage;
}

From source file:ddf.catalog.transformer.input.tika.TikaInputTransformer.java

private void createThumbnail(InputStream input, Metacard metacard) {
    try {// w  w  w.  j  a  v a  2s  . com
        Image image = ImageIO.read(new CloseShieldInputStream(input));

        if (null != image) {
            BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = bufferedImage.createGraphics();
            graphics.drawImage(image, null, null);
            graphics.dispose();

            BufferedImage thumb = Scalr.resize(bufferedImage, 200);

            try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                ImageIO.write(thumb, "jpeg", out);

                byte[] thumbBytes = out.toByteArray();
                metacard.setAttribute(new AttributeImpl(Metacard.THUMBNAIL, thumbBytes));
            }
        } else {
            LOGGER.warn("Unable to read image from input stream to create thumbnail.");
        }
    } catch (Exception e) {
        LOGGER.warn("Unable to read image from input stream to create thumbnail.", e);
    }
}

From source file:RasterDemo.java

RasterPanel() {
    setBackground(Color.white);/*  w w  w . j  av  a 2  s.c  om*/
    setSize(450, 400);

    Image image = getToolkit().getImage("largeJava2sLogo.jpg");

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 1);
    try {
        mt.waitForAll();
    } catch (Exception e) {
        System.out.println("Exception while loading image.");
    }

    if (image.getWidth(this) == -1) {
        System.out.println("No jpg file");
        System.exit(0);
    }

    bi1 = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bi1.createGraphics();
    big.drawImage(image, 0, 0, this);
    bi = bi1;
}