Lossless JPEG Rotate (90/180/270 degrees) in Java?

前端 未结 4 1646
悲哀的现实
悲哀的现实 2020-11-30 02:42

Is there a Java library for rotating JPEG files in increments of 90 degrees, without incurring image degradation?

4条回答
  •  隐瞒了意图╮
    2020-11-30 03:12

    Regarding the issue of EXIF data not necessarily being handled correctly, since EXIF data is irrelevant in many situations, here's example code demonstrating only the LLJTran lossless JPEG rotation feature (with thanks to user113215):

    final File              SrcJPEG  = new File("my-input.jpg");
    final File              DestJPEG = new File("my-output.jpg");
    final FileInputStream   In       = new FileInputStream(SrcJPEG);
    
    try {
        final LLJTran           LLJT = new LLJTran(In);
    
        LLJT.read(LLJTran.READ_ALL, true);
        LLJT.transform(LLJTran.ROT_90);
    
        final FileOutputStream  Out = new FileOutputStream(DestJPEG);
    
        try {
            LLJT.save(Out, LLJTran.OPT_WRITE_ALL);
        } finally {
            Out.close();
        }
    
    } finally {
        In.close(); 
    }
    

    If you make the input and output File objects refer to the same file, you can run this over and over again, and observe that the image does not degrade, no matter how many iterations it is put through.

提交回复
热议问题