Display Image from Server in Java - java

I am new in Java programming. My query is that I am having an image which is present on a server and I want to display that image inside the JFrame. I tried using the Image class but that seems to be not working.
Please Note: I don't want to use applets for this, so is there some other method by which this can be done?
Thanks & Regards,

Assuming that it's a public accessible webserver, you can use URL#openStream() to get an InputStream out of an URL.
InputStream input = new URL("http://example.com/image.png").openStream();
Then you can just create the BufferedImage with help of ImageIO#read() the usual way.
BufferedImage image = ImageIO.read(input);
// ...

Related

Create gif from Servlett outputstream in Java

I am developing an application on a server to manage some Image showing stuff. Therefor I created a servlett to put an Image from a Directory into an outputstream so I can call it from my jsp to display the Image.
BufferedImage bi;
bi = ImageIO.read(new URL(imagePath));
OutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
out.close();
this part works fine. Now I thought it would be nice to get a Stream of Images and combine them to a .gif file and to show the .gif in my .jsp too.
In this Link http://elliot.kroo.net/software/java/GifSequenceWriter/GifSequenceWriter.java
I found an example that should work(I hope so).
So I want to change this solution a little bit.I created another servlett where I do a for-loop on a hashmap and every time I want to pass the showimage the Id of the Image that I want.
for(Map.Entry<Integer, Integer> entry : mMap.entrySet())
{
response.sendRedirect(request.getContextPath() + "/showimage?
imageid="+entry.getValue());
}
After that, my showImage servlett stores the Image as .jpg in the outputstream. In the next step, within my for loop I want to get the image outputstream and use the writeSequence function.
Can anyone tell me how to get the image from the Outputstream? Or is the Outputstream sent straight back to client and this isn't even possible without copying the imageshow code in the new class?
Thank you very much

Show images coming from mongodb into java swing

i'm a newbie in Java and Mongodb. Till now all works fine but now i complete stuck. I can write and retrieve images to and from Mongodb. But how i can show them on screen. I know i can get them from the folder but i like to show the image direct from database into, in this case because i learning, into swing.
i guess i need to convert them? OR how does it works?
Database dbb = new Database(mc, dba);
DB dbc1 = dbb.getDatabase("aatestdb");
String newFileName = "test foto";
GridFS gfsPhotoa = new GridFS(dbc1, "photofile");
GridFSDBFile imageForOutput = gfsPhotoa.findOne(newFileName);
System.out.println ("show i receive data");
// proof i got image from dbimageForOutput.writeTo("/Users/xyz/Pictures/foto_offshore_site/java_app.jpg");
Typically in Swing you can use a JLabel and ImageIcon to show an image. If you can get your GridFSDBFile into an InputStream, you could try ImageIO.read(InputStream) This may or may not work. If it doesn't work, you are going to have to get an ImageReader for the specific kind of image you are dealling with

How can I get an image too big from a server?

I'm currenty developing for blackberry and just bumped into this problem as i was trying to download an image from a server. The servlet which the device communicates with is working correctly, as I have made a number of tests for it. But it gives me the
413 HTTP error ("Request entity too large").
I figure i will just get the bytes, uhm, portion by portion. How can i accomplish this?
This is the code of the servlet (the doGet() method):
try {
ImageIcon imageIcon = new ImageIcon("c:\\Users\\dcalderon\\prueba.png");
Image image = imageIcon.getImage();
PngEncoder pngEncoder = new PngEncoder(image, true);
output.write(pngEncoder.pngEncode());
} finally {
output.close();
}
Thanks. It's worth mentioning that I am developing both the client-side and the server-side.
I am not aware by server side code. You can look on this Link to get an idea how to upload file using multipart to support Big files uploading
it can also work on blackberry , With some modifications needed.
http://www.developer.nokia.com/Community/Wiki/HTTP_Post_multipart_file_upload_in_Java_ME
I'm not familiar with the PNGEncoder class you're using, but just looking at your servlet code, and the comment you made about the request size (2.2 MB), I'm guessing that part of your problem is that you're uncompressing the image, and then transmitting it across the network.
I don't think you should have any PNGEncoder or ImageIcon code in your servlet. You should just read the "c:\\Users\\dcalderon\\prueba.png" file in with a normal InputStream as bytes, and then write that to the servlet's output. I don't think it matters whether that file is a PNG image, a .mp3 file, or any other content. (although you might need to set the Content Type to image/png).
So, I would try transmitting the image compressed (as a .png just as it's stored on disk). If that still doesn't work, then go with the suggestion to use multipart transmission.

How to take single snapshots from a webcam?

I want to take a snapshot with my webcam using java and save it to a jpg file. What are the steps needed to do so? A tutorial would be greatly appreciated.
Greetings,
Burkhard
the JMF (Java Media Framework) is a good starting point. However, I did not succeed with it.
I finally found the solution here.
The important part being:
Buffer buf = frameGrabber.grabFrame();
// Convert frame to an buffered image so it can be processed and saved
Image img = (new BufferToImage((VideoFormat) buf.getFormat()).createImage(buf));
buffImg = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_RGB);
//TODO saving the buffImg
what you are looking for might be the Java Media Framework (JMF).
See the Sun Tutorial. I hope that helps.
I prefer using JMyron instead of JMF. JMyron is easy to use for accessing webcam. To save the captured image you just need to save the BufferedImage using ImageIO.write(); this blog post How To Use Webcam Using Javais usefull to start using JMyron.
Try webcam-capture project.
This code will take a snapshot from webcam (embedded, connected to USB or IP camera) and save it into JPG file:
Webcam webcam = Webcam.getDefault();
webcam.open()
BufferedImage image = webcam.getImage();
ImageIO.write(image, "JPG", new File("test.jpg"));

how to load a image from web in java

I need to load an image from a web in a simple Java stand alone aplication. Any Ideas?
You can load an image using
BufferedImage img = ImageIO.read(new URL("http://stackoverflow.com/content/img/so/logo.png"));
For methods how to display the loaded image, see the Sun "Working with images" tutorial.
URL url = new URL("http://host/theimage.jpg");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
is that enough to start you? Don't know what you want to do from there.
See the documentation at ImageIO.read(URL).
I would take a look at HTTPClient.
Find the URL to the image, and you can get an inputstream feeding you the image data, plus you'll get the content-type etc. so you can correctly handle it once you've downloaded it.
Here's some sample code. You may also need to call getResponseHeaders() on the GetMethod to identify the image type.

Categories