| 1 | /*
|
|---|
| 2 | * Copyright 2002-2015 Drew Noakes
|
|---|
| 3 | *
|
|---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 5 | * you may not use this file except in compliance with the License.
|
|---|
| 6 | * You may obtain a copy of the License at
|
|---|
| 7 | *
|
|---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 9 | *
|
|---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 13 | * See the License for the specific language governing permissions and
|
|---|
| 14 | * limitations under the License.
|
|---|
| 15 | *
|
|---|
| 16 | * More information about this project is available at:
|
|---|
| 17 | *
|
|---|
| 18 | * https://drewnoakes.com/code/exif/
|
|---|
| 19 | * https://github.com/drewnoakes/metadata-extractor
|
|---|
| 20 | */
|
|---|
| 21 | package com.drew.imaging.jpeg;
|
|---|
| 22 |
|
|---|
| 23 | import java.io.File;
|
|---|
| 24 | import java.io.FileInputStream;
|
|---|
| 25 | import java.io.IOException;
|
|---|
| 26 | import java.io.InputStream;
|
|---|
| 27 | import java.util.Arrays;
|
|---|
| 28 | import java.util.HashSet;
|
|---|
| 29 | import java.util.Set;
|
|---|
| 30 |
|
|---|
| 31 | import com.drew.lang.StreamReader;
|
|---|
| 32 | import com.drew.lang.annotations.NotNull;
|
|---|
| 33 | import com.drew.lang.annotations.Nullable;
|
|---|
| 34 | import com.drew.metadata.Metadata;
|
|---|
| 35 | //import com.drew.metadata.adobe.AdobeJpegReader;
|
|---|
| 36 | import com.drew.metadata.exif.ExifReader;
|
|---|
| 37 | import com.drew.metadata.file.FileMetadataReader;
|
|---|
| 38 | //import com.drew.metadata.icc.IccReader;
|
|---|
| 39 | import com.drew.metadata.iptc.IptcReader;
|
|---|
| 40 | //import com.drew.metadata.jfif.JfifReader;
|
|---|
| 41 | import com.drew.metadata.jpeg.JpegCommentReader;
|
|---|
| 42 | import com.drew.metadata.jpeg.JpegReader;
|
|---|
| 43 | //import com.drew.metadata.photoshop.PhotoshopReader;
|
|---|
| 44 | //import com.drew.metadata.xmp.XmpReader;
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Obtains all available metadata from JPEG formatted files.
|
|---|
| 48 | *
|
|---|
| 49 | * @author Drew Noakes https://drewnoakes.com
|
|---|
| 50 | */
|
|---|
| 51 | public class JpegMetadataReader
|
|---|
| 52 | {
|
|---|
| 53 | public static final Iterable<JpegSegmentMetadataReader> ALL_READERS = Arrays.asList(
|
|---|
| 54 | new JpegReader(),
|
|---|
| 55 | new JpegCommentReader(),
|
|---|
| 56 | //new JfifReader(),
|
|---|
| 57 | new ExifReader(),
|
|---|
| 58 | //new XmpReader(),
|
|---|
| 59 | //new IccReader(),
|
|---|
| 60 | //new PhotoshopReader(),
|
|---|
| 61 | new IptcReader()//,
|
|---|
| 62 | //new AdobeJpegReader()
|
|---|
| 63 | );
|
|---|
| 64 |
|
|---|
| 65 | @NotNull
|
|---|
| 66 | public static Metadata readMetadata(@NotNull InputStream inputStream, @Nullable Iterable<JpegSegmentMetadataReader> readers) throws JpegProcessingException, IOException
|
|---|
| 67 | {
|
|---|
| 68 | Metadata metadata = new Metadata();
|
|---|
| 69 | process(metadata, inputStream, readers);
|
|---|
| 70 | return metadata;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | @NotNull
|
|---|
| 74 | public static Metadata readMetadata(@NotNull InputStream inputStream) throws JpegProcessingException, IOException
|
|---|
| 75 | {
|
|---|
| 76 | return readMetadata(inputStream, null);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | @NotNull
|
|---|
| 80 | public static Metadata readMetadata(@NotNull File file, @Nullable Iterable<JpegSegmentMetadataReader> readers) throws JpegProcessingException, IOException
|
|---|
| 81 | {
|
|---|
| 82 | InputStream inputStream = new FileInputStream(file);
|
|---|
| 83 | Metadata metadata;
|
|---|
| 84 | try {
|
|---|
| 85 | metadata = readMetadata(inputStream, readers);
|
|---|
| 86 | } finally {
|
|---|
| 87 | inputStream.close();
|
|---|
| 88 | }
|
|---|
| 89 | new FileMetadataReader().read(file, metadata);
|
|---|
| 90 | return metadata;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | @NotNull
|
|---|
| 94 | public static Metadata readMetadata(@NotNull File file) throws JpegProcessingException, IOException
|
|---|
| 95 | {
|
|---|
| 96 | return readMetadata(file, null);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | public static void process(@NotNull Metadata metadata, @NotNull InputStream inputStream) throws JpegProcessingException, IOException
|
|---|
| 100 | {
|
|---|
| 101 | process(metadata, inputStream, null);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | public static void process(@NotNull Metadata metadata, @NotNull InputStream inputStream, @Nullable Iterable<JpegSegmentMetadataReader> readers) throws JpegProcessingException, IOException
|
|---|
| 105 | {
|
|---|
| 106 | if (readers == null)
|
|---|
| 107 | readers = ALL_READERS;
|
|---|
| 108 |
|
|---|
| 109 | Set<JpegSegmentType> segmentTypes = new HashSet<JpegSegmentType>();
|
|---|
| 110 | for (JpegSegmentMetadataReader reader : readers) {
|
|---|
| 111 | for (JpegSegmentType type : reader.getSegmentTypes()) {
|
|---|
| 112 | segmentTypes.add(type);
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | JpegSegmentData segmentData = JpegSegmentReader.readSegments(new StreamReader(inputStream), segmentTypes);
|
|---|
| 117 |
|
|---|
| 118 | processJpegSegmentData(metadata, readers, segmentData);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | public static void processJpegSegmentData(Metadata metadata, Iterable<JpegSegmentMetadataReader> readers, JpegSegmentData segmentData)
|
|---|
| 122 | {
|
|---|
| 123 | // Pass the appropriate byte arrays to each reader.
|
|---|
| 124 | for (JpegSegmentMetadataReader reader : readers) {
|
|---|
| 125 | for (JpegSegmentType segmentType : reader.getSegmentTypes()) {
|
|---|
| 126 | reader.readJpegSegments(segmentData.getSegments(segmentType), metadata, segmentType);
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | private JpegMetadataReader() throws Exception
|
|---|
| 132 | {
|
|---|
| 133 | throw new Exception("Not intended for instantiation");
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|