Retrieve an image from the web in java - java

I am trying to read an image that resides somewhere on the web from my Java program. So far I have successfully loaded an image by using the following code.
URL url = new URL("http://www.google.com/images/nav_logo4.png");
Image img = Toolkit.getDefaultToolkit().getImage(url);
What I want to know is why this code (which is the first i tried) does not work:
BufferedImage img = ImageIO.read(new File("http://www.google.com/images/nav_logo4.png"));
This would have the benefit of giving me a BufferedImage. Also, how can I make the above code block until the image is loaded? I know I can use an ImageObserver, but is there a simpler way?
When I try the second option, I get this exception:
javax.imageio.IIOException: Can't read input file!

A File cannot refer to a URL.
Although I haven't tried it, there appears to be a ImageIO.read(URL) method, which can take an URL as the input as an URL object.
I would presume it would be called as follows:
ImageIO.read(new URL("http://url/to/my/image.png"));

File objects cant read from URLs

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());
}

java.lang.illegalArgumentException: Image==null?

im planning to write a java program that read the image and saves it. seems like i have struck with this issue. please help. here is my code.
public class test{
public static void main(String[] args){
try{
URL testurl=new URL("www.google.co.in");
img=ImageIO.read(testurl.openStream());
ImageIO.write(img,"png",new File("c:\\out.jpg"));
}
catch(IOException e){}
}
i also tried this with a localhost url but it didnt work. please help me. also i wish to save the image in the same name that is found in the webpage. any suggestion on how can i do that ?
URL imageUrl = new URL("http://host.com/image.jpg");
BufferedImage image = ImageIO.read(imageUrl);
File outputFile = new File("myImage.jpg");
ImageIO.write(image, "jpg", outputFile);
I'm assuming the URL you used is just a placeholder, considering it's not an image.
You cannot automatically create an image out of an HTML file. The only way this is possible is if you render the HTML first, or if you start with an image file
The problem is the URL you have specified. It must be able to be read as an image. As Java Docs say:
If no registered ImageReader claims to be able to read the resulting stream, null is returned.
So, if you specify a link like the following:
URL testurl = new URL("http://www.dotahut.com/img/icons/spells/442.png");
it will work.

Updated Image not getting reflected

I am trying to crop image.There is an ajax call made to the server end where cropping takes place and original file gets replaced with cropped image.
Now when i get back the control, i still see the old image even though cropped image exists at the same location.
Changes reflect only after page refresh which i don't want users to do it.Code for crop image is as follows
BufferedImage originalImgage = ImageIO.read(new File(filePath+"\\"+subFolder+"\\"+fileName));
ImageIO.write(originalImgage,extention,new File(filePath+"\\"+subFolder+"\\"+dateStamp+"_"+fileName));//save original image
BufferedImage SubImgage = originalImgage.getSubimage(xAxis,yAxis,width,height);
File outputfile = new File(filePath+"\\"+subFolder+"\\"+fileName);
ImageIO.write(SubImgage,extention,outputfile);
pls help
Thanks
Try to replace the image src parameter with the new path returned from ajax in ajax success call back.
Well after struggling a lot finally found the problem.The image is cached by the browser. So whenever u do any operations on image on the same file location , the latest copy is not download.Instead old image from cache is still referred.
Hence the solution was to make browser download latest copy.This was simple.
Append dummy parameter to file path forcing browser to download
rand = new Date().getTime();
var image_Path = filePath+"?crop="+rand

Tesseract read an image online?

Im about to start working with tesseract, tess4j to be exact, and im going through the api docs. I havent come across any way to read from a webpage.
Basically the program opens a webpage that is just an image. I want tess4j to read the image from the page and convert it to words. If tess4j cant do this is there any other java ocrs that would be helpful, preferably without downloading the image?
Appreciate the help.
Try this:
String imageURL = "<Remote URL of image>";
String result = "";
URL url = new URL(imageURL);
BufferedImage img = ImageIO.read(url);
Tesseract instance = new Tesseract();
instance.setDatapath("<your tessdata path>");
result = instance.doOCR(img);
Reading and downloading are synonymous. If you are looking into reading an image from a web-page without opening it in a web-page, I would suggest looking into the "curl" command and it's equivalent in Java. After getting the image with the aforementioned command, it can then be parsed with Tesseract.

How to load an image in Java using toolkit and new URL

I need to use a JPEG image in my Java applet.
In my applet class, I define the image name and create an object to ImageBuffer class.
String iname= "image1.jpg";
b = new ImageBuffer(iname,this);
In the ImageBuffer class, I call
Image image = null;
image = Toolkit.getDefaultToolkit().getImage(new URL(applet.getCodeBase(),fileName));
While this does not flag an error and image is not null anymore, it does not initialize image correctly. The height and width are -1. The url of the path however appears to be correct : /C:/Users/..../image1.jpg
How do I correctly load the image? It is in the bin file of my Eclipse project currently.
The height and width are -1.
Use a MediaTracker to monitor the asynchronous loading progress of the image. Alternately use ImageIO to load the image prior to the next code line.
If the image is placed in the correct location, then this would fix it
image = new ImageIcon(Toolkit.getDefaultToolkit().getImage(new URL(applet.getCodeBase(),fileName))).getImage();
If the code returns a NullPointerException it means the image is not placed in the correct directory.

Categories

Resources