How to read png images form inputstream in java - java

There are 2 applications. One application act as server and sends continuously screen shot of desktop by using the following code.
Robot robot=new Robot();
OutputStream os;
BufferedImage image = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", os);
The second application is Android application acts a client application and has to read continuously the above image stream from inputstream.
Could please help me to read the png images from inputstream in the client application.
Thanks & Regards
Mini.

In client application, read the InputStream via Socket.getInputStream() method.
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
BufferedImage image = ImageIO.read(in);

Android SDK does not support the method ImageIO.read(). Even if you can compile your code, your android application will get crashed and have error about missing libraries like this:
could not find method javax.imageio.imageio.read
What I suggest is using bitmapping instead of this...

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

Screenshot capture in Window 8 OS

To capture screen shot in my java application i have write following code
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "png", new File("resources/img/screenshot.png"));
This is working successfully and capture screen shot but this is not working in windows 8 operating system. any one else who have face this type of problem and get soluction?
my application is install into the program file folder and the windows 8 not give permission to write there how i can write there now?
Do not write it there! OS manufacturers as well as Sun/Oracle have been saying for years not to write files to the application's installation directory. It is not only the wrong place to write them, but as you have discovered, does not provide write permissions for a typical Java app.
Instead put the screen-shot in user.home e.g. as seen in this answer.
you can do that without writing file in your local machine by using the following code
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] imageByte = null;
try
{
//To Get the the size of the screen.
Rectangle screenRect = new Rectangle(Toolkit
.getDefaultToolkit().getScreenSize());
//To Store the scrreen shot in buffer.
BufferedImage capture = new Robot()
.createScreenCapture(screenRect);
//To Save the image on specific location.
ImageIO.write(capture, "png",bos);
bos.flush();
imageByte=bos.toByteArray();
bos.close();
// File file = new File("resources/img/screenshot.png");
MultipartEntity mpEntity = new MultipartEntity();
// ContentBody cfBody = new FileBody(file);
ContentBody cfBody = new ByteArrayBody(imageByte,"screenshot.png");
mpEntity.addPart("screenshot", cfBody);
}
send direct the byte array in plase of the image

ImageIO not working on Ubuntu

I have the following java code, which writes an image to a byte array then back into an image:
BufferedImage bi = ImageIO.read(new File("1.png"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi,"png",baos);
byte[] img = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(img);
BufferedImage bif = ImageIO.read(bais);
On OS X, this works perfectly fine, but on Ubuntu, bif (the final image) becomes null at the end, meaning there is a problem reading from the ByteArrayInputStream. I'm using the oracle jdk in both cases and don't know what's going on.
Edit: More evidence that I'm going insane: I printed the img byte array to a file using a FileOutputStream on Ubuntu and OSX, then diffed the two files, and they are exactly the same. This means either the ByteArrayInputStream is different or (more likely) the second ImageIO.read isn't working for some reason.
Ubuntu server is usually running in headless mode which can cause problems with java image manipulation.
http://www.oracle.com/technetwork/articles/javase/headless-136834.html
Try tweaking your jvm start-up settings to allow running headless and it should fix it.

Display Image from Server in 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);
// ...

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"));

Categories