| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.tools; |
| 22 |
|
| 23 |
import com.drew.imaging.jpeg.JpegProcessingException; |
| 24 |
import com.drew.imaging.jpeg.JpegSegmentData; |
| 25 |
import com.drew.imaging.jpeg.JpegSegmentReader; |
| 26 |
import com.drew.imaging.jpeg.JpegSegmentType; |
| 27 |
import com.drew.lang.Iterables; |
| 28 |
import com.drew.lang.annotations.NotNull; |
| 29 |
|
| 30 |
import java.io.File; |
| 31 |
import java.io.IOException; |
| 32 |
import java.util.HashSet; |
| 33 |
import java.util.List; |
| 34 |
import java.util.Set; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class ExtractJpegSegmentTool |
| 50 |
{ |
| 51 |
public static void main(String[] args) throws IOException, JpegProcessingException |
| 52 |
{ |
| 53 |
if (args.length < 1) { |
| 54 |
printUsage(); |
| 55 |
System.exit(1); |
| 56 |
} |
| 57 |
|
| 58 |
String filePath = args[0]; |
| 59 |
|
| 60 |
if (!new File(filePath).exists()) { |
| 61 |
System.err.println("File does not exist"); |
| 62 |
printUsage(); |
| 63 |
System.exit(1); |
| 64 |
} |
| 65 |
|
| 66 |
Set<JpegSegmentType> segmentTypes = new HashSet<JpegSegmentType>(); |
| 67 |
|
| 68 |
for (int i = 1; i < args.length; i++) { |
| 69 |
JpegSegmentType segmentType = JpegSegmentType.valueOf(args[i].toUpperCase()); |
| 70 |
if (!segmentType.canContainMetadata) { |
| 71 |
System.err.printf("WARNING: Segment type %s cannot contain metadata so it may not be necessary to extract it%n", segmentType); |
| 72 |
} |
| 73 |
segmentTypes.add(segmentType); |
| 74 |
} |
| 75 |
|
| 76 |
if (segmentTypes.size() == 0) { |
| 77 |
|
| 78 |
segmentTypes.addAll(JpegSegmentType.canContainMetadataTypes); |
| 79 |
} |
| 80 |
|
| 81 |
System.out.println("Reading: " + filePath); |
| 82 |
|
| 83 |
JpegSegmentData segmentData = JpegSegmentReader.readSegments(new File(filePath), segmentTypes); |
| 84 |
|
| 85 |
saveSegmentFiles(filePath, segmentData); |
| 86 |
} |
| 87 |
|
| 88 |
public static void saveSegmentFiles(@NotNull String jpegFilePath, @NotNull JpegSegmentData segmentData) throws IOException |
| 89 |
{ |
| 90 |
for (JpegSegmentType segmentType : segmentData.getSegmentTypes()) { |
| 91 |
List<byte[]> segments = Iterables.toList(segmentData.getSegments(segmentType)); |
| 92 |
if (segments.size() == 0) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
if (segments.size() > 1) { |
| 97 |
for (int i = 0; i < segments.size(); i++) { |
| 98 |
String outputFilePath = String.format("%s.%s.%d", jpegFilePath, segmentType.toString().toLowerCase(), i); |
| 99 |
System.out.println("Writing: " + outputFilePath); |
| 100 |
FileUtil.saveBytes(new File(outputFilePath), segments.get(i)); |
| 101 |
} |
| 102 |
} else { |
| 103 |
String outputFilePath = String.format("%s.%s", jpegFilePath, segmentType.toString().toLowerCase()); |
| 104 |
System.out.println("Writing: " + outputFilePath); |
| 105 |
FileUtil.saveBytes(new File(outputFilePath), segments.get(0)); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
private static void printUsage() |
| 111 |
{ |
| 112 |
System.out.println("USAGE:\n"); |
| 113 |
System.out.println("\tjava com.drew.tools.ExtractJpegSegmentTool <filename> [<segment> ...]\n"); |
| 114 |
|
| 115 |
System.out.print("Where <segment> is zero or more of:"); |
| 116 |
for (JpegSegmentType segmentType : JpegSegmentType.class.getEnumConstants()) { |
| 117 |
if (segmentType.canContainMetadata) { |
| 118 |
System.out.print(" " + segmentType.toString()); |
| 119 |
} |
| 120 |
} |
| 121 |
System.out.println(); |
| 122 |
} |
| 123 |
} |