22

是否有用于以 90 度为增量旋转 JPEG 文件而不会导致图像质量下降的 Java 库?

4

5 回答 5

15

我发现了这个: http: //mediachest.sourceforge.net/mediautil/

API: http: //mediachest.sourceforge.net/mediautil/javadocs/mediautil/image/jpeg/LLJTran.html

于 2009-04-01T17:52:11.753 回答
7

基于 Henry 的回答,这里有一个如何使用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
    ...
}
于 2013-03-08T20:24:46.110 回答
6

关于 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对象引用同一个文件,你可以一遍又一遍地运行它,观察图像不会退化,无论它经过多少次迭代。

于 2014-03-10T05:43:39.730 回答
0

特别是对于 Android,我发现了这个 fork:

https://github.com/juanitobananas/AndroidMediaUtil

优于上游的好处:

  • Gradle/Android Studio 项目
  • 与 jitpack.io 兼容

它甚至可以在普通 Java 上使用,因为代码不会导入任何 Android 特定的包(虽然我没有尝试过)。

于 2021-12-30T00:38:47.333 回答
-4

这种事情不需要外部库,它都内置在 SE 中。最简单的是 Graphics2D 对象的 rotate() 函数。

例如:

   Image rotatedImage = new BufferedImage(imageToRotate.getHeight(null), imageToRotate.getWidth(null), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
    g2d.rotate(Math.toRadians(90.0));
    g2d.drawImage(imageToRotate, 0, -rotatedImage.getWidth(null), null);
    g2d.dispose();

没有损失!

或者,如果您想格外小心,只需使用 BufferedImage.getRGB(x,y),并将其逐个像素地转换到新图像上。

于 2009-04-01T18:49:31.553 回答