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:net.dv8tion.jda.utils.AvatarUtil.java

public static Avatar getAvatar(InputStream inputStream) {
    try {//from  w w  w  . j av a 2 s  . c  o  m
        //reading
        BufferedImage img = ImageIO.read(inputStream);
        return getAvatar(img);
    } catch (IOException e) {
        JDAImpl.LOG.log(e);
    }
    return null;
}

From source file:eu.novait.imageresizer.helpers.ImageConverter.java

public void process() throws IOException {
    BufferedImage inImage = ImageIO.read(this.file);
    double ratio = (double) inImage.getWidth() / (double) inImage.getHeight();
    int w = this.width;
    int h = this.height;
    if (inImage.getWidth() >= inImage.getHeight()) {
        w = this.width;
        h = (int) Math.round(w / ratio);
    } else {/*from w  w  w. ja v a2 s .  co m*/
        h = this.height;
        w = (int) Math.round(ratio * h);
    }
    int type = inImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : inImage.getType();
    BufferedImage outImage = new BufferedImage(w, h, type);
    Graphics2D g = outImage.createGraphics();
    g.drawImage(inImage, 0, 0, w, h, null);
    g.dispose();
    String ext = FilenameUtils.getExtension(this.file.getAbsolutePath());
    String t = "jpg";
    switch (ext) {
    case "png":
        t = "png";
        break;
    }
    ImageIO.write(outImage, t, this.outputfile);
}

From source file:Main.java

public MyTextArea() {
    super(20, 20);
    try {//from ww w.j  a  v  a  2 s  . co m
        image = ImageIO.read(new File("yourImage.png"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:androidassetsgenerator.AssetGenerator.java

public void scaleThumbnailator(File input, String fileName, int baseIndex, String outputDir)
        throws IOException {
    asset = ImageIO.read(input);

    String file = FilenameUtils.removeExtension(fileName);
    String fileExtension = FilenameUtils.getExtension(fileName);

    System.out.println(baseIndex);

    BufferedImage outputLdpi, outputMdpi, outputHdpi, outputXhdpi, outputXxhdpi, outputXxxhdpi;

    switch (baseIndex) {
    // Write from ldpi to ldpi -> kind of useless
    case 0:/*from   w  w  w . ja  v a  2 s .  co m*/
        writeOutput(asset, path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    // Write from mdpi to ldpi
    case 1:
        writeOutput(scale34(asset), path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    // Write from hdpi to mdpi, ldpi
    case 2:
        outputMdpi = scale23(asset);
        // Write to mdpi
        writeOutput(outputMdpi, path(fileName, outputDir, Constants.DRAWABLE_MDPI));
        // Write to ldpi
        writeOutput(scale34(outputMdpi), path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    // Write from xhdpi to hdpi, mdpi, ldpi
    case 3:
        outputHdpi = scale34(asset);
        outputMdpi = scale23(outputHdpi);
        // Write to hdpi
        writeOutput(outputHdpi, path(fileName, outputDir, Constants.DRAWABLE_HDPI));
        // Write to mdpi
        writeOutput(outputMdpi, path(fileName, outputDir, Constants.DRAWABLE_MDPI));
        // Write to ldpi
        writeOutput(scale34(outputMdpi), path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    // Write from xxhdpi to xhdpi, hdpi, mdpi, ldpi
    case 4:
        outputXhdpi = scale23(asset);
        outputHdpi = scale34(outputXhdpi);
        outputMdpi = scale23(outputHdpi);
        // Write to xhdpi
        writeOutput(outputXhdpi, path(fileName, outputDir, Constants.DRAWABLE_XHDPI));
        // Write to hdpi
        writeOutput(outputHdpi, path(fileName, outputDir, Constants.DRAWABLE_HDPI));
        // Write to mdpi
        writeOutput(outputMdpi, path(fileName, outputDir, Constants.DRAWABLE_MDPI));
        // Write to ldpi
        writeOutput(scale34(outputMdpi), path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    // Write from xxxhdpi to xxhdpi, xhdpi, hdpi, mdpi, ldpi
    case 5:
        outputXxhdpi = scale34(asset);
        outputXhdpi = scale23(outputXxhdpi);
        outputHdpi = scale34(outputXhdpi);
        outputMdpi = scale23(outputHdpi);
        // Write to xxhdpi
        writeOutput(outputXxhdpi, path(fileName, outputDir, Constants.DRAWABLE_XXHDPI));
        // Write to xhdpi
        writeOutput(outputXhdpi, path(fileName, outputDir, Constants.DRAWABLE_XHDPI));
        // Write to hdpi
        writeOutput(outputHdpi, path(fileName, outputDir, Constants.DRAWABLE_HDPI));
        // Write to mdpi
        writeOutput(outputMdpi, path(fileName, outputDir, Constants.DRAWABLE_MDPI));
        // Write to ldpi
        writeOutput(scale34(outputMdpi), path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    }

}

From source file:Main.java

public BackgroundPane() {
    try {/*from   w ww . j  av  a  2 s  .c om*/
        bg = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    Timer timer = new Timer(40, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            yOffset += yDelta;
            if (yOffset > getHeight()) {
                yOffset = 0;
            }
            repaint();
        }
    });
    timer.start();
}

From source file:DBMS.UpdateFileUpload.java

public static boolean processFile(String path, FileItemStream item, int id) {
    try {// ww  w.  ja  va2  s .c o m
        String check = item.getName();
        if (check.endsWith(".jpg") || check.endsWith(".JPG")) {
            String imstring = "images/" + Integer.toString(id);
            File f = new File(path + File.separator + imstring);
            if (!f.exists())
                f.mkdir();
            File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
            FileOutputStream fos = new FileOutputStream(savedFile);
            InputStream is = item.openStream();
            int x = 0;
            byte[] b = new byte[1024];
            while ((x = is.read(b)) != -1) {
                fos.write(b, 0, x);
            }
            fos.flush();
            fos.close();
            String dbimage = imstring + "/a.jpg";
            //dc.enterImage(dbimage);
            //im =dbimage;
            //System.out.println("Resizing!");
            //Resize rz = new Resize();
            //rz.resize(dbimage);
            BufferedImage originalImage = ImageIO.read(savedFile);
            int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
            BufferedImage resizeImageJpg = resizeImage(originalImage, type);
            ImageIO.write(resizeImageJpg, "jpg", savedFile);
            File rFile = new File(f.getAbsolutePath() + "/a.jpg");
            savedFile.renameTo(rFile);
            ProfileEditDB dc = new ProfileEditDB();
            dc.enterImage(id, dbimage);
            System.out.println("Link Entered to Database!");
            return true;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java

public static void updateImage(JLabel imageContainer, File imageFile, Format format) {
    if (imageFile == null || !imageFile.exists()) {
        return;//from   www  .j a  v a 2  s .  com
    }
    BufferedImage img = null;
    try {
        img = ImageIO.read(imageFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (img == null) {
        return;
    }
    int imageWidth = img.getWidth();
    int imageHeight = img.getHeight();
    int imageViewWidth = (int) imageContainer.getPreferredSize().getWidth();
    int imageViewHeight = (int) imageContainer.getPreferredSize().getHeight();
    double factor = getScaleFactorToFit(new Dimension(imageWidth, imageHeight),
            new Dimension(imageViewWidth, imageViewHeight));
    imageWidth = (int) (factor * imageWidth);
    imageHeight = (int) (factor * imageHeight);
    if (imageWidth <= 0 || imageHeight <= 0 || imageViewWidth <= 0 || imageViewHeight <= 0) {
        return;
    }
    BufferedImage tmp = UIUtil.createImage(imageViewWidth, imageViewHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    int x = (imageViewWidth - imageWidth) / 2;
    int y = (imageViewHeight - imageHeight) / 2;
    if (format == Format.PNG || format == Format.XML) {
        g2.drawImage(img, x, y, imageWidth, imageHeight, null);
    } else {
        g2.drawImage(img, x, y, imageWidth, imageHeight, Color.WHITE, null);
    }
    g2.dispose();
    imageContainer.setIcon(new ImageIcon(tmp));
}

From source file:javafx1.Testpics.java

private void bildSchleife() {
    File f = new File("F:/NetBeansProjekte/pictures");
    File[] fileArray = f.listFiles();
    java.util.Arrays.sort(fileArray);
    for (File file : fileArray) {
        try {/*from w  ww.j  a  v  a2 s . c  om*/
            System.out.println("file: " + file.getCanonicalPath());
            File outputfile = new File(file.getCanonicalPath() + ".jpg");
            BufferedImage bi = ImageIO.read(file);
            System.out.println(" enc" + encodeToString(bi, "jpg"));
            ImageIO.write(bi, "jpg", outputfile);
            bi = ImageIO.read(outputfile);
            System.out.println(" w" + bi.getWidth() + " h" + bi.getHeight());
            byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
            // System.out.println(new String(data));
            byte[] x = Base64.encodeBase64URLSafe(data);
            System.out.println("x" + new String(x));
            String b64 = Base64.encodeBase64String(data);
            System.out.println("64" + b64);

            //                byte[] backToBytes = Base64.decodeBase64(base64String);
            //                InputStream in = new ByteArrayInputStream(backToBytes);
            //                BufferedImage bi;
            //                bi = ImageIO.read(in);
            //byte[] xx = base64String.getBytes(StandardCharsets.UTF_8);
            // bild, wie es von http kommt
            //                RunPic rp = new RunPic(this, data);
            //                Platform.runLater(rp);
            try {
                Thread.sleep(50);
                break;
            } catch (InterruptedException ex) {
                Logger.getLogger(JavaFX1.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (IOException ex) {
            Logger.getLogger(JavaFX1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.springsecurity.plugin.util.ImageResizer.java

public BufferedImage scale(File icon, int targetWidth, int targetHeight) {
    BufferedImage ret = null;//  w  w w . ja va  2  s  .  c om
    if (icon.exists()) {
        try {
            BufferedImage img = ImageIO.read(icon);
            ret = img;
            int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
                    : BufferedImage.TYPE_INT_ARGB;
            BufferedImage scratchImage = null;
            Graphics2D g2 = null;
            int w = img.getWidth();
            int h = img.getHeight();
            int prevW = w;
            int prevH = h;
            do {
                if (w > targetWidth) {
                    w /= 2;
                    w = (w < targetWidth) ? targetWidth : w;
                }
                if (h > targetHeight) {
                    h /= 2;
                    h = (h < targetHeight) ? targetHeight : h;
                }
                if (scratchImage == null) {
                    scratchImage = new BufferedImage(w, h, type);
                    g2 = scratchImage.createGraphics();
                }

                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2.drawImage(ret, 0, 0, w, h, 0, 0, prevW, prevH, null);

                prevW = w;
                prevH = h;
                ret = scratchImage;
            } while (w != targetWidth || h != targetHeight);
            if (g2 != null) {
                g2.dispose();
            }
            if (targetWidth != ret.getWidth() || targetHeight != ret.getHeight()) {
                scratchImage = new BufferedImage(targetWidth, targetHeight, type);
                g2 = scratchImage.createGraphics();
                g2.drawImage(ret, 0, 0, null);
                g2.dispose();
                ret = scratchImage;
            }
        } catch (IOException e) {
        }
    }
    return ret;
}

From source file:com.daniel.processimage.ImageManager.java

public byte[] readImage(Part file) throws FileNotFoundException, IOException {
    InputStream is = file.getInputStream();
    BufferedImage bufferedImage = ImageIO.read(is);
    ByteArrayOutputStream byteMas = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "png", byteMas);
    byteMas.flush();/*from  w  w w . j  av  a  2 s.  c o  m*/
    byte[] imageInBytes = byteMas.toByteArray();
    byteMas.close();

    return imageInBytes;
}