How to rotate JPEG images based on the orientation metadata?

后端 未结 8 1803
轻奢々
轻奢々 2020-11-27 12:30

I have some server code that is generating thumbnails when an image is uploaded. The issue is that when the image was taken and the camera/device was rotated, the thumbnail

8条回答
  •  执笔经年
    2020-11-27 13:18

    My solution is a combination of @PerLindberg's answer as well as @AntoineMartin's. I tried the other answers with Java 8 on Windows 10 and none seemed to do the trick. @AntoinMartin's com.drew.imaging solution was slow and image turned out black and white and full of artifacts. @PerLindberg's JavaXT solution did not read Exif 2.2 data.

    1) Use com.drew.imaging to read exif information:

    // Inner class containing image information
    public static class ImageInformation {
        public final int orientation;
        public final int width;
        public final int height;
    
        public ImageInformation(int orientation, int width, int height) {
            this.orientation = orientation;
            this.width = width;
            this.height = height;
        }
    
        public String toString() {
            return String.format("%dx%d,%d", this.width, this.height, this.orientation);
        }
    }
    
    public ImageInformation readImageInformation(File imageFile)  throws IOException, MetadataException, ImageProcessingException {
        Metadata metadata = ImageMetadataReader.readMetadata(imageFile);
        Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
        JpegDirectory jpegDirectory = metadata.getFirstDirectoryOfType(JpegDirectory.class);
    
        int orientation = 1;
        if (directory != null) {
            try {
                orientation = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
            } catch (MetadataException me) {
                logger.warn("Could not get orientation");
            }
            int width = jpegDirectory.getImageWidth();
            int height = jpegDirectory.getImageHeight();
    
            return new ImageInformation(orientation, width, height);
        } else {
            return null;
        }
    }
    

    2) Use JavaXT to perform the rotation based on Exif data.

    public void rotateMyImage(String imageDownloadFilenme);
        File imageDownloadFile =  new File(imgageDownloadFilenme);
        Image image = new Image(imgageDownloadFilenme);
        ImageInformation imageInformation = readImageInformation(imageDownloadFile);
        if (imageInformation != null) {
            rotate(imageInformation, image);
        }
        image.saveAs(imgageDownloadFilenme);
    }
    
    public void rotate(ImageInformation info, Image image) {
    
        switch(info.orientation) {
            case 1:
                return;
            case 2:
                image.flip();
                break;
            case 3:
                image.rotate(180.0D);
                break;
            case 4:
                image.flip();
                image.rotate(180.0D);
                break;
            case 5:
                image.flip();
                image.rotate(270.0D);
                break;
            case 6:
                image.rotate(90.0D);
                break;
            case 7:
                image.flip();
                image.rotate(90.0D);
                break;
            case 8:
                image.rotate(270.0D);
        }
    
    }
    

提交回复
热议问题