Example usage for javax.imageio ImageIO read

List of usage examples for javax.imageio ImageIO read

Introduction

In this page you can find the example usage for javax.imageio ImageIO read.

Prototype

public static BufferedImage read(ImageInputStream stream) throws IOException 

Source Link

Document

Returns a BufferedImage as the result of decoding a supplied ImageInputStream with an ImageReader chosen automatically from among those currently registered.

Usage

From source file:com.buddycloud.mediaserver.business.util.ImageUtils.java

public static BufferedImage createImagePreview(File image, int width, int height) throws IOException {
    final BufferedImage img = ImageIO.read(image);
    final BufferedImage thumbnail = Scalr.resize(img, Method.ULTRA_QUALITY, width, height);
    img.flush();//from www .  j a v a  2  s  .com

    return thumbnail;
}

From source file:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

public static BufferedImage fileToImage(File imageFile) {
    BufferedImage image;/* w w  w  .j av  a2  s.  c om*/
    try {
        image = ImageIO.read(imageFile);
    } catch (IOException e) {
        throw new RuntimeException("Failed to read image file: " + imageFile, e);
    }
    return image;
}

From source file:costumetrade.common.util.PdfUtils.java

public static PDDocument createImagePdf(File file) {

    PDPageContentStream contentStream = null;
    try {/*from   ww w .  ja va  2 s .co  m*/
        BufferedImage bimg = ImageIO.read(file);
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(new PDRectangle(bimg.getWidth(), bimg.getHeight()));
        document.addPage(page);
        PDImageXObject image = PDImageXObject.createFromFileByExtension(file, document);
        contentStream = new PDPageContentStream(document, page);
        contentStream.drawImage(image, 0, 0);
        return document;
    } catch (IOException e) {
        throw new RuntimeException("?PDF", e);
    } finally {
        IOUtils.closeQuietly(contentStream);
    }

}

From source file:org.mule.module.facebook.FacebookConnectorGenericUnitTest.java

@Before
public void setup() throws IOException {
    connector = new FacebookConnector();
    connector.setAccessToken("ACCESS_TOKEN");
    client = mock(Client.class);
    connector.setClient(client);/*  w  ww.jav a 2  s.c o m*/
    resource = mock(WebResource.class);
    final BufferedImage image = ImageIO.read(new ClassPathResource("image.jpg").getInputStream());

    when(client.resource((URI) anyObject())).thenReturn(resource);
    when(resource.queryParam(anyString(), anyString())).thenReturn(resource);
    when(resource.queryParam("type", "user")).thenReturn(resource);
    when(resource.queryParam("type", "event")).thenReturn(resource);
    when(resource.queryParam("type", "checkin")).thenReturn(resource);
    when(resource.queryParam("type", "post")).thenReturn(resource);
    when(resource.queryParam("type", "page")).thenReturn(resource);
    when(resource.queryParam("type", "group")).thenReturn(resource);
    when(resource.get(String.class)).thenReturn(jsonResponse);
    when(resource.get(BufferedImage.class)).thenReturn(image);
    when(resource.post(String.class, eq(anyObject()))).thenReturn(jsonResponse);

}

From source file:org.opennms.poller.remote.MetadataUtils.java

public static ImageComponent getImageFromURL(final URL url) {
    try {// ww  w.  j a v  a 2  s .  c  o m
        return new ImageComponent(ImageIO.read(url));
    } catch (final Exception e) {
        LOG.warn("Unable to ready image: {}", url, e);
        return null;
    }
}

From source file:de.uniwue.dmir.heatmap.util.beans.BufferedImageFactoryBean.java

@Override
public BufferedImage getObject() throws Exception {
    InputStream inputStream = this.resource.getInputStream();
    return ImageIO.read(inputStream);
}

From source file:jadx.core.utils.android.Res9patchStreamDecoder.java

public void decode(InputStream in, OutputStream out) throws JadxException {
    try {/*from   ww w .  j  ava 2 s  . c o  m*/
        byte[] data = IOUtils.toByteArray(in);

        BufferedImage im = ImageIO.read(new ByteArrayInputStream(data));
        int w = im.getWidth(), h = im.getHeight();

        BufferedImage im2 = new BufferedImage(w + 2, h + 2, BufferedImage.TYPE_INT_ARGB);
        im2.createGraphics().drawImage(im, 1, 1, w, h, null);

        NinePatch np = getNinePatch(data);
        drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight);
        drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom);

        int[] xDivs = np.xDivs;
        for (int i = 0; i < xDivs.length; i += 2) {
            drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]);
        }

        int[] yDivs = np.yDivs;
        for (int i = 0; i < yDivs.length; i += 2) {
            drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]);
        }

        ImageIO.write(im2, "png", out);
    } catch (IOException | NullPointerException ex) {
        throw new JadxException(ex.toString());
    }
}

From source file:ImageUtil.java

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

    Float scale;//from   w w  w .j a va2 s .  co m
    if (src.getWidth() > src.getHeight()) {
        scale = Float.valueOf(height) / Float.valueOf(src.getHeight());
        if (src.getWidth() * scale < width) {
            scale = Float.valueOf(width) / Float.valueOf(src.getWidth());
        }
    } else {
        scale = Float.valueOf(width) / Float.valueOf(src.getWidth());
        if (src.getHeight() * scale < height) {
            scale = Float.valueOf(height) / Float.valueOf(src.getHeight());
        }
    }
    //        
    //        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, Float.valueOf(src.getWidth() * scale).intValue(),
            Float.valueOf(src.getHeight() * scale).intValue());

    temp = crop(temp, width, height);

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

    return baos;
}

From source file:com.igormaznitsa.mindmap.swing.panel.utils.MiscIcons.java

@Nullable
private static Image loadImage(@Nonnull final String name) {
    if ("empty".equals(name)) {
        final BufferedImage result = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
        return result;
    }//from w w w  .jav a  2s .  c o m
    final InputStream in = MiscIcons.class
            .getResourceAsStream("/com/igormaznitsa/mindmap/swing/miscicons/" + name + ".png");
    if (in == null) {
        return null;
    }
    try {
        return ImageIO.read(in);
    } catch (IOException ex) {
        LOGGER.error("IO exception for icon '" + name + '\'');
        return null;
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.igormaznitsa.mindmap.swing.panel.utils.Icons.java

private Icons(final String name) {
    final InputStream in = ScalableIcon.class.getClassLoader()
            .getResourceAsStream("com/igormaznitsa/mindmap/swing/panel/icons/" + name); //NOI18N
    try {//from  ww w  . jav  a  2 s  .co m
        this.icon = new ImageIcon(ImageIO.read(in));
    } catch (IOException ex) {
        throw new Error("Can't load icon " + name, ex); //NOI18N
    } finally {
        IOUtils.closeQuietly(in);
    }
}