| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.drew.metadata.adobe; |
| 23 |
|
| 24 |
import com.drew.imaging.jpeg.JpegSegmentMetadataReader; |
| 25 |
import com.drew.imaging.jpeg.JpegSegmentType; |
| 26 |
import com.drew.lang.SequentialByteArrayReader; |
| 27 |
import com.drew.lang.SequentialReader; |
| 28 |
import com.drew.lang.annotations.NotNull; |
| 29 |
import com.drew.metadata.Directory; |
| 30 |
import com.drew.metadata.Metadata; |
| 31 |
|
| 32 |
import java.io.IOException; |
| 33 |
import java.util.Collections; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@SuppressWarnings("WeakerAccess") |
| 42 |
public class AdobeJpegReader implements JpegSegmentMetadataReader |
| 43 |
{ |
| 44 |
public static final String PREAMBLE = "Adobe"; |
| 45 |
|
| 46 |
@NotNull |
| 47 |
public Iterable<JpegSegmentType> getSegmentTypes() |
| 48 |
{ |
| 49 |
return Collections.singletonList(JpegSegmentType.APPE); |
| 50 |
} |
| 51 |
|
| 52 |
public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType) |
| 53 |
{ |
| 54 |
for (byte[] bytes : segments) { |
| 55 |
if (bytes.length == 12 && PREAMBLE.equalsIgnoreCase(new String(bytes, 0, PREAMBLE.length()))) |
| 56 |
extract(new SequentialByteArrayReader(bytes), metadata); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public void extract(@NotNull SequentialReader reader, @NotNull Metadata metadata) |
| 61 |
{ |
| 62 |
Directory directory = new AdobeJpegDirectory(); |
| 63 |
metadata.addDirectory(directory); |
| 64 |
|
| 65 |
try { |
| 66 |
reader.setMotorolaByteOrder(false); |
| 67 |
|
| 68 |
if (!reader.getString(PREAMBLE.length()).equals(PREAMBLE)) { |
| 69 |
directory.addError("Invalid Adobe JPEG data header."); |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
directory.setInt(AdobeJpegDirectory.TAG_DCT_ENCODE_VERSION, reader.getUInt16()); |
| 74 |
directory.setInt(AdobeJpegDirectory.TAG_APP14_FLAGS0, reader.getUInt16()); |
| 75 |
directory.setInt(AdobeJpegDirectory.TAG_APP14_FLAGS1, reader.getUInt16()); |
| 76 |
directory.setInt(AdobeJpegDirectory.TAG_COLOR_TRANSFORM, reader.getInt8()); |
| 77 |
} catch (IOException ex) { |
| 78 |
directory.addError("IO exception processing data: " + ex.getMessage()); |
| 79 |
} |
| 80 |
} |
| 81 |
} |