Create gif from Servlett outputstream in Java - 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

Related

Converting Buffered Image to MultipartFile

Using Spring MVC & JSP
My scenario-
User uploads image file (gif,jpg,png) and if the file doesnt match dimensions then file needs to be scaled and disaply on jsp as preview.
I have MultipartFile(which is uploaded file), I convert that into BufferedImage then I resize the BufferedImage using Graphics2D. I want to convert this buffered image into multipart file again to show it on jsp.
How Can i convert buffered image into MultipartFile?
Thanks
I doubt this statement is correct: I want to convert this buffered image into multipart file again to show it on jsp
To my knowledge you need to either:
Write the converted image into a disk and display it using html <img..> tag, or
Create a spring mvc handler method that writes the image into the response body directly, something like this
.
#RequestMapping(..)
public void convertedImg(HttpServletResponse resp) {
// set response Content-Type..
OutputStream os = resp.getOutputStream();
//.. write your converted image into os
}
You can probably use ImageIO to write your BufferedImage to some file format.
Like:
BufferedImage image; // your image
OutputStream stream; // your output
try {
ImageIO.write(image, "png", stream);
}
finally {
stream.flush();
}
Where stream can be a FileOutputStream to the server's file system, the OutputStream of a database blob, or the servlet response's OutputStream, depending on where/if you want to store it.

Not able to delete image file after image conversion from PNG to TIF

I am creating .PNG file using BufferedImage with some test. Now after creating image I am trying to convert .PNG image to .TIF, which is working fine. Now once I create TIF image, I want to delete PNG image. But because of some reason, I am not able to do this. There is no any exception for this.
Here is my code
File pngFile = null;
FileOutputStream fOut = null;
try {
pngFile = new File("C:\\Test.PNG");
fOut = new FileOutputStream ("C:\\Test.TIF");
RenderedOp src = JAI.create("fileload", "C:\\Test.PNG");
TIFFImageEncoder encoder = new TIFFImageEncoder (fOut, null);
encoder.encode (src);
}catch(Exception e) {
}finally {
fOut.close();
System.out.println(pngFile.delete());
}
Well there's definitely no exception since your catch block is empty.
Something may be still holding a handle to the file, not allowing it to be deleted.
I would examine JAI.create, RenderedOp and the TiffEncoder.
Instead of providing the file path as string you can provide input stream and in finally first close the input stream and then delete the file. This may work.
I was facing same problem sometime before. The best way to do it in this is to first dispose the resources using the image object you have create, like below-
var image = Image.FromFile(pngTarget); // here pngTarget is my PNG file's name along with complete path.
// your code to convert png to tiff
.
.
.
at the end of the method you can write below -
image.Dispose(); // the image object I have created above
File.Delete(pngTarget); // delete the file
Also, don't forget to flush/close the memory stream, if using any.
Thanks.

error in sending image from android to java app serially -javax.imageio.IIOException: Bogus Huffman table definition

I need to send image from android app to java app. Basically, I need a byte array from the image to send to rf module which transmits.Another rf module receives and sends the byte array to java app which must make the image .
Android code:
FileInputStream fis = new FileInputStream(myFile);
byte[] b=new byte[(int)myFile.length()];
fis.read(b);server.send(b);
Java code:
FileOutputStream fwrite = new FileOutputStream(new File("my_xml"),true);
fwrite.write(bb);//bb is a byte from rf using input stream as soon as a byte comes it is read to file. This is necessary for some other reasons
fwrite.flush();
fwrite.close();
After getting full file:
FileInputStream fir=new FileInputStream("my_xml");
final BufferedImage bufferedImage = ImageIO.read(fir);
ImageIO.write(bufferedImage, "bmp", new File("image.bmp"));
fir.close();
I am getting error javax.imageio.IIOException: Bogus Huffman table definition
The rf is working fine because text file is being sent perfectly.Please help.Even without ImageIo code is not giving image even after changing extension to jpeg
The error means that the image file cant be read because the format is wrong.That is some bytes are missing or wrong or out of proper position and therefore file cant be decoded. My rf transfer does not have protocols like tcp/ip therefore some bytes are lost due to error in communication channel and hence the error.
You don't need to use ImageIO just to copy a file. Just read and write the bytes.
Your code has other problems:
You are assuming that read(byte[]) fills the buffer. It doesn't. Check the Javadoc.
You are also assuming that the file length fits into an int. If it does, fine. If it doesn't, you are hosed.
You appear to be opening and closing the FileOutputStream on every byte received. This could not be more inefficient. Open it once, write everything, close it.
flush() before close() is redundant.
You are storing the image in a file called 'my_xml'. This is only going to cause confusion, if it hasn't already.
You don't even need the file. Just load the image directly from the input stream.

How to read png images form inputstream in 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...

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);
// ...

Categories