| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.metadata.jfif; |
| 22 |
|
| 23 |
import com.drew.imaging.jpeg.JpegSegmentMetadataReader; |
| 24 |
import com.drew.imaging.jpeg.JpegSegmentType; |
| 25 |
import com.drew.lang.ByteArrayReader; |
| 26 |
import com.drew.lang.RandomAccessReader; |
| 27 |
import com.drew.lang.annotations.NotNull; |
| 28 |
import com.drew.metadata.Metadata; |
| 29 |
import com.drew.metadata.MetadataReader; |
| 30 |
|
| 31 |
import java.io.IOException; |
| 32 |
import java.util.Collections; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class JfifReader implements JpegSegmentMetadataReader, MetadataReader |
| 45 |
{ |
| 46 |
public static final String PREAMBLE = "JFIF"; |
| 47 |
|
| 48 |
@NotNull |
| 49 |
public Iterable<JpegSegmentType> getSegmentTypes() |
| 50 |
{ |
| 51 |
return Collections.singletonList(JpegSegmentType.APP0); |
| 52 |
} |
| 53 |
|
| 54 |
public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType) |
| 55 |
{ |
| 56 |
for (byte[] segmentBytes : segments) { |
| 57 |
|
| 58 |
if (segmentBytes.length >= PREAMBLE.length() && PREAMBLE.equals(new String(segmentBytes, 0, PREAMBLE.length()))) |
| 59 |
extract(new ByteArrayReader(segmentBytes), metadata); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata) |
| 68 |
{ |
| 69 |
JfifDirectory directory = new JfifDirectory(); |
| 70 |
metadata.addDirectory(directory); |
| 71 |
|
| 72 |
try { |
| 73 |
|
| 74 |
|
| 75 |
directory.setInt(JfifDirectory.TAG_VERSION, reader.getUInt16(JfifDirectory.TAG_VERSION)); |
| 76 |
directory.setInt(JfifDirectory.TAG_UNITS, reader.getUInt8(JfifDirectory.TAG_UNITS)); |
| 77 |
directory.setInt(JfifDirectory.TAG_RESX, reader.getUInt16(JfifDirectory.TAG_RESX)); |
| 78 |
directory.setInt(JfifDirectory.TAG_RESY, reader.getUInt16(JfifDirectory.TAG_RESY)); |
| 79 |
directory.setInt(JfifDirectory.TAG_THUMB_WIDTH, reader.getUInt8(JfifDirectory.TAG_THUMB_WIDTH)); |
| 80 |
directory.setInt(JfifDirectory.TAG_THUMB_HEIGHT, reader.getUInt8(JfifDirectory.TAG_THUMB_HEIGHT)); |
| 81 |
} catch (IOException me) { |
| 82 |
directory.addError(me.getMessage()); |
| 83 |
} |
| 84 |
} |
| 85 |
} |