Java Image Load loadImage(InputStream s)

Here you can find the source of loadImage(InputStream s)

Description

load Image

License

Open Source License

Declaration

public static BufferedImage loadImage(InputStream s) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * MIT License//ww w .ja  v  a  2s  .c om
 *
 * Copyright (c) 2016 Ashur Rafiev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *******************************************************************************/

import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JPanel;

public class Main {
    private static final MediaTracker imageAssist = new MediaTracker(new JPanel());

    public static BufferedImage loadImage(InputStream s) throws IOException {
        Image img = Toolkit.getDefaultToolkit().createImage(loadBytes(s));
        try {
            imageAssist.addImage(img, 1);
            imageAssist.waitForID(1);
            imageAssist.removeImage(img);
        } catch (InterruptedException e) {
            throw new IOException("Interrupted while loading image");
        }
        BufferedImage bufImg = new BufferedImage(img.getWidth(null), img.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);
        bufImg.getGraphics().drawImage(img, 0, 0, null);
        return bufImg;
    }

    public static byte[] loadBytes(InputStream s) throws IOException {
        DataInputStream in = new DataInputStream(s);
        byte bytes[] = new byte[in.available()];
        in.readFully(bytes);
        in.close();
        return bytes;
    }
}

Related

  1. getImageFromFile(String fileName)
  2. getImageFromSource(JPanel source, Dimension dim)
  3. getLocal(String imagePath)
  4. loadImage(File original)
  5. loadImage(final String src)
  6. loadImage(Object parent, String pathToImage)
  7. loadImage(String filename)
  8. loadImage(String filePath, int imageSize)
  9. loadImage(URL url)