Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Image saving size reduction  RSS feed

 
ramnna jain
Ranch Hand
Posts: 129
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application I allow to save an image file of type jpeg.
But when I save it, I found the file size reduced means the loss of image quality.
Example: (suppose in my local drive the file size is 1.4mb. after uploading to the server the file saved on the server shows 644kb).

I want to save the image in its original file size.

Here is the code I use to save the image file.

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPEG does alter an image through a read/write cycle. If you don't need to perform any image processing, why don't you save the contents of the InputStream directly to a file? That will result in an identical copy of the original file.
 
ramnna jain
Ranch Hand
Posts: 129
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following code... but the saving file is erroneous... the file with the given
name is created but shows size 0 bytes.

 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change ">0" into "!= -1", ">-1" or ">=0". Especially through network connections, having a return value of 0 is allowed when reading a number of bytes. Perhaps the data simply isn't available yet. All read methods return -1 to indicate there is no more data, and there will be no more data.
 
ramnna jain
Ranch Hand
Posts: 129
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the desired results. Thanks for your support.
 
dani sm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

The image which is written used above code is not able to use further. I am using the image to read the characters by OCR. Since the quality of the image was low, i used the above code. But it is getting the below exception.

javax.imageio.IIOException: Not a JPEG file: starts with 0x89 0x50

Kindly suggest any idea to resolve this exception.
Thanks in advance.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Files starting with 0x89 0x50 are PNG files not JPEG files. What file type are you using?
 
dani sm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using JPEG files taken from android camera in my Project. While writing image with the below code, image file is saving with reduced size.

String str = "C:\\";
InputStream in = request.getPart("saveimages").getInputStream();

BufferedImage image = null;
image = ImageIO.read(in);
ImageIO.write(image, "jpg", new File(str));

After saving the image, we are calling tesseract OCR by passing this image.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception message clearly states that the code that is throwing the exception expects a JPG file but is getting something else (that something else happens to be a PNG). Lots of known file types start their files start with a magic number ie a number that identifies the type of type of file, for PNG it is 89 50 4e 47, for JPG it is ff d8 ff e0.

Looking at your code what I think may be wrong is the file being passed into the write method as the destination (the third parameter) should be a file name and not a directory.
 
dani sm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks or your valuable reply!

Yes you are right. the image i passed is not a jpeg file.

As per Mr. ramnna jain's code, the image is created with the same size.


String str = "E:\\"+ imagename;

OutputStream outst=new FileOutputStream(new File(str));
byte buf[] = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
outst.write(buf, 0, len);
}
in.close();
outst.flush();
outst.close();

Is it possible to create a valid JPEG file using above code.?
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not try it and see.
 
dani sm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I tried. It writes the image with original size and also with .jpg extension. But while doing image processing with tesseract OCR it gets the below exception as i said before.

javax.imageio.IIOException: Not a JPEG file: starts with 0x89 0x50
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your question is not about reducing the size of an image, or saving an image to a file using ImageIO, but about an exception in a 3rd party library?
 
dani sm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah.
This exception arises when i am trying to save the image with original size. If i save the image with reduced size using ImageIO, i can use the third party tool without exception.

All i expected is to get an image with original size and also readable by the third party tool.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dani sm wrote:Yes. I tried. It writes the image with original size and also with .jpg extension. But while doing image processing with tesseract OCR it gets the below exception as i said before.

javax.imageio.IIOException: Not a JPEG file: starts with 0x89 0x50

I thought we had already established that you weren't using a JPG file. Just changing the extension to .jpg doesn't turn a PNG file into a JPG format.
The code you showed, whilst incomplete, looks like code that will copy any file of any format from one place to another. It doesn't transform the file in any way whereas the ImageIO method can transform a file's format.

This exception arises when i am trying to save the image with original size. If i save the image with reduced size using ImageIO

The problem is almost certainly because the original file is a PNG and not a JPG. When you use ImageIO in the way you showed it will output the file's format as JPG hence the file output by ImageIO works but the original doesn't.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ramnna jain wrote:In my application I allow to save an image file of type jpeg.
But when I save it, I found the file size reduced means the loss of image quality.

Always use png instead of jpg. Jpg is lossy compression and png compression is lossless. No significant difference in file size.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:
Always use png instead of jpg. Jpg is lossy compression and png compression is lossless. No significant difference in file size.

Not necessarily true. The final file size of a JPEG can be a lot smaller than a PNG file depending on the image and the amount of data loss you are prepared to accept.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!