首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无损JPEG旋转(90/180/270度)在Java?

无损JPEG旋转(90/180/270度)在Java?
EN

Stack Overflow用户
提问于 2009-04-02 01:51:25
回答 4查看 14.2K关注 0票数 21

有没有一个Java库可以将JPEG文件以90度的增量旋转,而不会导致图像质量下降?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-04-01 17:52:12

Stack Overflow用户

发布于 2013-03-09 04:24:46

在亨利答案的基础上,下面是一个如何使用MediaUtil根据EXIF数据执行无损JPEG旋转的示例:

try {
    // Read image EXIF data
    LLJTran llj = new LLJTran(imageFile);
    llj.read(LLJTran.READ_INFO, true);
    AbstractImageInfo<?> imageInfo = llj.getImageInfo();
    if (!(imageInfo instanceof Exif))
        throw new Exception("Image has no EXIF data");

    // Determine the orientation
    Exif exif = (Exif) imageInfo;
    int orientation = 1;
    Entry orientationTag = exif.getTagValue(Exif.ORIENTATION, true);
    if (orientationTag != null)
        orientation = (Integer) orientationTag.getValue(0);

    // Determine required transform operation
    int operation = 0;
    if (orientation > 0
            && orientation < Exif.opToCorrectOrientation.length)
        operation = Exif.opToCorrectOrientation[orientation];
    if (operation == 0)
        throw new Exception("Image orientation is already correct");

    OutputStream output = null;
    try {   
        // Transform image
        llj.read(LLJTran.READ_ALL, true);
        llj.transform(operation, LLJTran.OPT_DEFAULTS
                | LLJTran.OPT_XFORM_ORIENTATION);

        // Overwrite original file
        output = new BufferedOutputStream(new FileOutputStream(imageFile));
        llj.save(output, LLJTran.OPT_WRITE_ALL);

    } finally {
        IOUtils.closeQuietly(output);
        llj.freeMemory();
    }

} catch (Exception e) {
    // Unable to rotate image based on EXIF data
    ...
}
票数 7
EN

Stack Overflow用户

发布于 2014-03-10 13:43:40

关于EXIF数据不一定被正确处理的问题,由于EXIF数据在许多情况下是无关紧要的,下面的示例代码仅演示了LLJTran无损JPEG旋转功能(感谢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(); 
}

如果您使输入和输出File对象引用同一文件,则可以反复运行该文件,并观察到图像不会降级,无论它经过多少次迭代。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/706665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档