简体   繁体   中英

Wrting Image in java in different formats gives different result

public static void main(String[] args) throws IOException {

        File original_f = new File(args[0]);
        String[] parts= args[0].split("\\.");
        String output_f = parts[0]+"_bin";
        original = ImageIO.read(original_f);
        grayscale = toGray(original);
        binarized = binarize(grayscale);            //Converts pixel of image in black((0,0,0)) or white( (255,255,255) )

        writeImage(output_f,parts[1]);

}   

private static void writeImage(String output, String part) throws IOException {
        File file = new File(output+".bmp");
        ImageIO.write(binarized, "bmp", file);
}

After binarizing image I'm getting correct answer. Pixel in binarized (variable in code) are either (0,0,0) or (255,255,255) .

But after writing Image in jpg format pixel are no longer correct, and if I write image in bmp format then pixels are correct.

Can anyone please explain what could be the reason???

JPEG is lossy image compression file format. You just loose some accuracy of pixel color values due to lossy compression. Bitmap is lossless format but without compression. For lossless compression you can use PNG.

如果使用的是Sun JDK随附的JPEG编码器,则还必须确保将没有alpha通道的图像传递给它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM