开发者

Java Saving Original JPEG Without Loss

As can be seen below I have 1st image an original JPEG image .Second one was taken to buffer image and than save using http://www.lac.inpe.br/JIPCookbook/6040-howto-compressimages.jsp with 1.0 quality . Still image became smaller in size and a really small destortion. Is it possible to save image to its quality as it is ? Pleas not that saving image as it is was just a sample test. After adding text I save it with highest quality which looses information too.

开发者_Python百科

Java Saving Original JPEG Without Loss

Java Saving Original JPEG Without Loss


Do not redraw the image and save it. Just copy the raw bytes instead!


I suspect your current code is something like this:

BufferedImage image = ImageIO.read(new File("my.jpg");
ImageIO.write(image, "jpg", new File("copy.jpg"));

Every time you repeat this the image will change a little (as you saw you always loose some quality). If you only want to copy the JPEG/file without changing anything you can do something like this (from this page http://www.exampledepot.com/egs/java.io/CopyFile.html):

void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}


JPEG, even with highest quality settings, is always lossy, even if the original image data came from a JPEG.

There are some operations like rotation/mirroring/crop that can be done lossless on a JPEG (using tools like jpegtran), but these are rare exceptions.

Anyway, it seems you have access to the original JPG image and you don't change it, so I don't understand why you compress it again.

If you really have to store such images lossless, best choice would be using the lossless mode of JPEG2000, this gives a smaller filesize than other alternatives like PNG for image data that has been compressed using JPG (although it is still much larger than the original JPG). For example, for the first of your example pictures:

 hAw2d.jpg ->   268,678 bytes (Original)
 hAw2d.jp2 -> 1,021,007 bytes (JPEG 2000, lossless)
 hAw2d.png -> 1,213,392 bytes (PNG)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜