Why use ImageIO can't get BufferedImage from URL - java

imageURL: https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBB77GLfY75FNWL&w=720&h=2048&url=http%3A%2F%2Fwww.facebook.com%2Fads%2Fimage%2F%3Fd%3DAQI0duFYFcmydWNutbwmSk2DfOmHcDrhPfsMJTUoEObbWkVzYUtrHgCuN_LFrWcPRzJi6jPgbn80oFs0Kj_WrdROjdnJkjbnS5-UJv9l9cJyhKCWS-lr-MXlc263Ul3Txe-VFqXfRrA6BOjt4DF-Sww2&ext=best
URL url = new URL(imageURL);
BufferedImage image = ImageIO.read(url);
or
URL url = new URL(imageURL);
BufferedImage image = ImageIO.read(url.openStream());
the result image is null? why?

ImageIO.read(URL) does support reading images from URL like you describe, however, it does support only a limited set of image formats. Built-in formats are JPEG, PNG, GIF, BMP and WBMP. There are plugins for many other formats, like TIFF, JPEG 2000, etc.
The problem is that the linked image is not in any of the built-in formats, it's in WEBP format, a new image format created by Google, and which does not have very widespread use yet. The reason it displays fine in your browser (and mine :-) ), is most likely that you are using Chrome, and Chrome has built-in support for WEBP.
There's at least one WEBP ImageIO plugin available. If you build and install this plugin, your code above should work and read the image just fine. There should be no need to invoke ImageIO.scanForPlugins() if the plugin is on class path when you launch your application.

Make sure the URL your provided is linking to a valid image file format such as jpg, png, bmp and so on.
Your current URL is linking to a .php file which is obviously not an image.
For example:
public static void main(String[] args)
{
Image image = null;
try {
URL url = new URL("https://s-media-cache-ak0.pinimg.com/236x/ac/bb/d4/acbbd49b22b8c556979418f6618a35fd.jpg");
image = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.setSize(236, 306);
frame.add(new JLabel(new ImageIcon(image)));
frame.setVisible(true);
}

When I open the link in Firefox, it attempts to download a file called "safe_image.php". It works in Google Chrome, so there's something weird going on in the headers or something for that URL. Is there any way you can host the image elsewhere?
Edit: Your image appears to be in the WebP format. ImageIO does not support this format natively, so you will need a library like webp-imageio.
Install that library and see if your code works. ImageIO should automatically find the plugin when you run ImageIO.scanForPlugins().

Related

Write animated-gif stored in BufferedImage to java.io.File Object

I am reading a gif image from internet url.
// URL of a sample animated gif, needs to be wrapped in try-catch block
URL imageUrl = new Url("http://4.bp.blogspot.com/-CTUfMbxRZWg/URi_3Sp-vKI/AAAAAAAAAa4/a2n_9dUd2Hg/s1600/Kei_Run.gif");
// reads the image from url and stores in BufferedImage object.
BufferedImage bImage = ImageIO.read(imageUrl);
// creates a new `java.io.File` object with image name
File imageFile = new File("download.gif");
// ImageIO writes BufferedImage into File Object
ImageIO.write(bImage, "gif", imageFile);
The code executes successfully. But, the saved image is not animated as the source image is.
I have looked at many of the stack-overflow questions/answers, but i am not able to get through this. Most of them do it by BufferedImage frame by frame which alters frame-rate. I don't want changes to the source image. I want to download it as it is with same size, same resolution and same frame-rate.
Please keep in mind that i want to avoid using streams and unofficial-libraries as much as i can(if it can't be done without them, i will use them).
If there is an alternative to ImageIO or the way i read image from url and it gets the thing done, please point me in that direction.
There is no need to decode the image and then re-encode it.
Just read the bytes of the image, and write the bytes, as is, to the file:
try (InputStream in = imageUrl.openStream()) {
Files.copy(in, new File("download.gif").toPath());
}

How to download captcha image in JAVA?I am able to get image url in HttpURLConnection object

I have written a JAVA program to get captcha image from a secure site. The aim is to download that captcha image and display the user in own customised window.
For that I am able to successfully get the captcha image in HttpURLConnection object (e.g. "http://enquiry.indianrail.gov.in/ntes/CaptchaServlet?action=getNewCaptchaImg&t=1430205579014" ). However I am struggling to get a way to save this image in local disk. How to parse this url and save captcha image?
This is how you can read the image from URL
BufferedImage img = null;
try {
URL url = new URL(getCodeBase(), "examples/strawberry.jpg");
img = ImageIO.read(url);
} catch (IOException e) {
}
getCodeBase() will return the URL you can also directly pass it , Now after having BufferedImage instance you can save it , display it whatever you want to do !
More on Reading/Loading an Image
You should have the image as a stream, so you just need to convert your inpustream image to file.
See an example here: http://www.mkyong.com/java/how-to-convert-inputstream-to-file-in-java/

Unable to load image from the same package in Java?

In my java package, I have a file called 'prog.ico'. I'm trying to load this file, via the following code:
java.net.URL url = this.getClass().getResource("prog.ico");
java.awt.Image image = ImageIO.read( url );
System.out.println("image: " + image);
This gives the output:
image: null
What am I doing wrong? The .ico file exists in the same package as the class from which I'm running this code.
It seems that the .ico image format is not supported. See this question and it's answer to get around this.
To prevent link rot: This solution recommends using Image4J to process .ico files.
I've written a plugin for ImageIO that adds support for .ICO (MS Windows Icon) and .CUR (MS Windows Cursor) formats.
You can get it from GitHub here: https://github.com/haraldk/TwelveMonkeys/tree/master/imageio/imageio-ico
After you have it installed the plugin, you should be able to read your icon using the code in your original post.
I thing you must go over FileInputStream to wrap the file
File file = new File("prog.ico");
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis); //reading the image file

Load tif images in Vaadin

I could not load a tif image with Vaadin 7 rc1
Here are the some of the alternatoves I have tried:
String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
Image image = new Image("Image", new FileResource(new File(basePath + "myPath/image.tif")));
Embedded image = new Embedded("Image", new ThemeResource("/pathToResource/imgae.jtif"));
In both cases the image was where the path pointed. Moreover if I had a jpg or png image in the same directory it was shown without a problem.
Does Vaadin 7 rc1 not support tif images?
The real question here is that if your browser supports TIFF images. Take a look at this wikipedia article.
For cross-browser compatibility, I suggest you to convert your images to PNG or JPG.

Java read different type of image formats jpg,tif,gif,png

I am trying to read some image files jpg, tif, gif, png and need to save files and create icons.
And i am getting UnsupportedTypeException.
ImageIO.read(file);
If i use following line, as earlier discuss in form.
BufferedImage img = JPEGCodec.createJPEGDecoder(inputStream).decodeAsBufferedImage();
I get JPEGCodec cannot found symbol.
I am using netbean 7.0.1. I have also added jai-imageio.jar.
By default, ImageIO can only read JPG, GIF and PNG file formats, if I remember right. To add new formats like TIFF, you need to add a plugin, which is a jar file, to your classpath, and to add an ImageIO.scanForPlugins() to you code before you try to read a file.
Example of plugin:
http://ij-plugins.sourceforge.net/plugins/imageio/
Try "ImageIO plugins" in Google.
JAI-ImageIO does include plugins for file formats like TIFF, so in principal what you are trying to do should work. However, to install JAI-ImageIO it's not enough to add it to your classpath. See the complete installation instructions here: http://java.sun.com/products/java-media/jai/INSTALL-jai_imageio_1_0_01.html
Fr detail we can see the like
http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/
Image img = null;
ImageInputStream iis = new FileImageInputStream(file);
try {
for (Iterator<ImageReader> i = ImageIO.getImageReaders(iis);
img == null && i.hasNext(); ) {
ImageReader r = i.next();
try {
r.setInput(iis);
img = r.read(0);
} catch (IOException e) {}
}
} finally {
iis.close();
}
return img;
Java advance image io also solve the problem, but its hard to maintain to install on all plateform.

Categories

Resources