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.
Related
I'm developing some kind of android mail app and I get each mail attachments as an ArrayList of urls from a rest api and I want to use them in some kind of attachment section. I need to check the urls and pass image links to a preview adapter using glide api and show other urls (other file formats, like .pdf, .docx or ...) in another section as a download link.
Is there any way to know if the url is link to a image file or not before downloading it?
I know there are seemingly similar threads that are answered already but this is different in two ways.
First I want to to know if the url is link to image or not before downloading it.
Second I don't want to use static extension check. Because there are like tons of different extensions like .jpg, .png,... and they may change and I don't want to update my app with each change.
There is a way you can do it but I'm not sure its the best approach.
Code:
new Thread(new Runnable() { // if already doing the checking on network thread then no need to add this thread
#Override
public void run() {
try {
URLConnection connection = new URL("image url here").openConnection();
String contentType = connection.getHeaderField("Content-Type");
boolean image = contentType.startsWith("image/"); //true if image
Log.i("IS IMAGE", "" + image);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Hope this helps!
You can provide additional fields,which can help you identify file format, in your rest API.
You can checkout response content-type. Checkout this answer:
https://stackoverflow.com/a/5802223
If you have the URI you could:
use this for the full path
and substring after the last "."
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.
I'm using the following code to use an image in my local drive as a tooltip but it does'nt show the picture :
String path = "C:/images/A.png";
labelIMG.setToolTipText("<html><img src='"+path+"'></html>");
any work around for this, thanks in advance!
The tooltip render is expecting a URL, not a file reference...
Try something like this instead.
String path = "C:/images/A.png";
File file = new File(path);
labelIMG.setToolTipText("<html><img src='"+file.toURI().toURL()+"'></html>");
You, could, of course, just use a simple String, but off the top of my head it might look something like file:///c:/images/A.png (I'm not a Windows box at the moment, so I can't check, sorry)
I have a testing server which runs headless. One test I want is to check that an image served off a particular URL matches some reference image.
Is there an API in Sikuli which can directly accept an image as a stream and compare it with some other image taken from the local resource file? Unfortunately, there is no complete tutorial on Sikuli's Java API, all I've found is tutorials that assume that there is a display available.
I'll be glad to see any examples or at least links to the needed parts of Sikuli javadocs. Also, suggestions for other approaches are welcome.
To use Sikuli you need
A base image on which the other image will be searched.
The image which will be searched within the other image.
If image 1 is your local resource image, you can create a org.sikuli.Finder instance with the path to the image and the Region of this image which will be searched.
Example (java level):
finder = new Finder("path/to/image", new Region(0, 0, <imgwidth>, <imgheight>));
If image 1 is your stream, you have to make a BufferedImage out of it somehow (I do not know the best way to do this).
Then you can make a org.sikuli.ScreenImage from this BufferedImage with the help of an java.awt.Rectangle and an org.sikuli.Region.
finder = new Finder(new ScreenImage(new Rectangle(0,0,<imgwidth>,<imgheight>), bufferedImage), new Region(0,0,<imgwidth>,<imgheight>))
After you created the finder from image 1, you can search image 2 within this image.
Again, you have two possibilities.
If the second image is your local resource image, you can create an org.sikuli.Pattern object with the file location:
pattern = new Pattern("path/to/image.png");
Else, if this is your stream, you have to make a BufferedImage out of the stream somehow. You can then create a pattern from this image:
pattern = new Pattnern(bufferedImage);
As a last step, you can now run the finder to search for the pattern:
finder.find(pattern);
You can check if the finder found anything with:
finder.hasNext();
And you should be able to iterate all findings with:
for (Match m : finder):
//do something with the match
I hope I could help you although your question is already some weeks old.
Below code helps for asserting images
//take screenshots
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
//copy it some location
FileUtils.copyFile(scrFile, new File("C:\\screenshot.png"));
Finder f = new Finder("C:\\screenshot.png");
System.out.println("abc");
f.find("C:\\chrome3.png", 0.95);
while(f.hasNext()){
System.out.println("found");
Match m= f.next();
f.destroy();
}
}
catch (IOException e)
{
e.printStackTrace();
}
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