Java BufferedImage from URL Img2ByteByUrl(String strUrl)

Here you can find the source of Img2ByteByUrl(String strUrl)

Description

Img Byte By Url

License

Apache License

Declaration

public static byte[] Img2ByteByUrl(String strUrl) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.net.URL;

import javax.imageio.ImageIO;

public class Main {
    public static byte[] Img2ByteByUrl(String strUrl) {

        ByteArrayOutputStream baos = null;
        try {//from  w w  w. ja v a2  s .c o m
            URL u = new URL(strUrl);
            BufferedImage image = ImageIO.read(u);

            // convert BufferedImage to byte array
            baos = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", baos);
            baos.flush();

            return baos.toByteArray();
        } catch (Exception e) {
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                }
            }
        }
        return null;
    }
}

Related

  1. downloadImage(String url)
  2. downloadImage(URL url)
  3. getFileAndDownload(String urlString, String folder)
  4. httpGetImage(String url)
  5. urlToImage(String urlstring)
  6. urlToImage(URL imageUrl)
  7. urlToJpegThumbnail(String url, String filename, double maxWidth, double maxHeight)