Java Code Examples for com.drew.imaging.jpeg.JpegMetadataReader#readMetadata()

The following examples show how to use com.drew.imaging.jpeg.JpegMetadataReader#readMetadata() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RotateImgUtils.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 获取图片正确显示需要旋转的角度(顺时针)
 */
private static final int getRotateAngle(InputStream is) throws TSException {
    int angle = 0;
    Metadata metadata;
    try {
        metadata = JpegMetadataReader.readMetadata(is);
        Directory directory = metadata.getDirectory(ExifDirectory.class);
        if (directory.containsTag(ExifDirectory.TAG_ORIENTATION)) {
            // Exif信息中方向  
            int orientation = directory.getInt(ExifDirectory.TAG_ORIENTATION);
            // 原图片的方向信息
            if (6 == orientation) {
                //6旋转90
                angle = 90;
            } else if (3 == orientation) {
                //3旋转180
                angle = 180;
            } else if (8 == orientation) {
                //8旋转90
                angle = 270;
            }
        }
    } catch (Exception e) {
        throw new TSException(TSEDictionary.UNDEFINED_FAIL.getCode(), "iphone图片服务器旋转异常");
    }
    return angle;
}
 
Example 2
Source File: RotateImgUtils.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 获取图片正确显示需要旋转的角度(顺时针)
 *
 * @return
 * @throws Exception
 */
private static final int getRotateAngle(File file) throws TSException {
    int angle = 0;
    Metadata metadata;
    try {
        metadata = JpegMetadataReader.readMetadata(file);
        Directory directory = metadata.getDirectory(ExifDirectory.class);
        if (directory.containsTag(ExifDirectory.TAG_ORIENTATION)) {
            // Exif信息中方向  
            int orientation = directory.getInt(ExifDirectory.TAG_ORIENTATION);
            // 原图片的方向信息
            if (6 == orientation) {
                //6旋转90
                angle = 90;
            } else if (3 == orientation) {
                //3旋转180
                angle = 180;
            } else if (8 == orientation) {
                //8旋转90
                angle = 270;
            }
        }
    } catch (Exception e) {
        throw new TSException(TSEDictionary.UNDEFINED_FAIL.getCode(), "iphone图片服务器旋转异常");
    }
    return angle;
}
 
Example 3
Source File: RafMetadataReader.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream) throws JpegProcessingException, IOException
{
    if (!inputStream.markSupported())
        throw new IOException("Stream must support mark/reset");

    inputStream.mark(512);

    byte[] data = new byte[512];
    int bytesRead = inputStream.read(data);

    if (bytesRead == -1)
        throw new IOException("Stream is empty");

    inputStream.reset();

    for (int i = 0; i < bytesRead - 2; i++) {
        // Look for the first three bytes of a JPEG encoded file
        if (data[i] == (byte) 0xff && data[i + 1] == (byte) 0xd8 && data[i + 2] == (byte) 0xff) {
            long bytesSkipped = inputStream.skip(i);
            if (bytesSkipped != i)
                throw new IOException("Skipping stream bytes failed");
            break;
        }
    }

    return JpegMetadataReader.readMetadata(inputStream);
}
 
Example 4
Source File: ImageMetadataReader.java    From metadata-extractor with Apache License 2.0 4 votes vote down vote up
/**
 * Reads metadata from an {@link InputStream} of known length and file type.
 *
 * @param inputStream a stream from which the file data may be read.  The stream must be positioned at the
 *                    beginning of the file's data.
 * @param streamLength the length of the stream, if known, otherwise -1.
 * @param fileType the file type of the data stream.
 * @return a populated {@link Metadata} object containing directories of tags with values and any processing errors.
 * @throws ImageProcessingException if the file type is unknown, or for general processing errors.
 */
@NotNull
public static Metadata readMetadata(@NotNull final InputStream inputStream, final long streamLength, final FileType fileType) throws IOException, ImageProcessingException
{
    switch (fileType) {
        case Jpeg:
            return JpegMetadataReader.readMetadata(inputStream);
        case Tiff:
        case Arw:
        case Cr2:
        case Nef:
        case Orf:
        case Rw2:
            return TiffMetadataReader.readMetadata(new RandomAccessStreamReader(inputStream, RandomAccessStreamReader.DEFAULT_CHUNK_LENGTH, streamLength));
        case Psd:
            return PsdMetadataReader.readMetadata(inputStream);
        case Png:
            return PngMetadataReader.readMetadata(inputStream);
        case Bmp:
            return BmpMetadataReader.readMetadata(inputStream);
        case Gif:
            return GifMetadataReader.readMetadata(inputStream);
        case Ico:
            return IcoMetadataReader.readMetadata(inputStream);
        case Pcx:
            return PcxMetadataReader.readMetadata(inputStream);
        case WebP:
            return WebpMetadataReader.readMetadata(inputStream);
        case Raf:
            return RafMetadataReader.readMetadata(inputStream);
        case Avi:
            return AviMetadataReader.readMetadata(inputStream);
        case Wav:
            return WavMetadataReader.readMetadata(inputStream);
        case QuickTime:
            return QuickTimeMetadataReader.readMetadata(inputStream);
        case Mp4:
            return Mp4MetadataReader.readMetadata(inputStream);
        case Mp3:
            return Mp3MetadataReader.readMetadata(inputStream);
        case Eps:
            return EpsMetadataReader.readMetadata(inputStream);
        case Heif:
            return HeifMetadataReader.readMetadata(inputStream);
        case Unknown:
            throw new ImageProcessingException("File format could not be determined");
        default:
            return new Metadata();
    }
}