|
| 1 | +package com.aspose.imaging.examples.ModifyingImages; |
| 2 | + |
| 3 | +import com.aspose.imaging.Image; |
| 4 | +import com.aspose.imaging.ImageOptionsBase; |
| 5 | +import com.aspose.imaging.examples.Logger; |
| 6 | +import com.aspose.imaging.examples.Path; |
| 7 | +import com.aspose.imaging.examples.Utils; |
| 8 | +import com.aspose.imaging.exif.ExifData; |
| 9 | +import com.aspose.imaging.exif.enums.ExifOrientation; |
| 10 | +import com.aspose.imaging.metadata.IImageMetadataFormat; |
| 11 | +import com.aspose.imaging.xmp.XmpPacketWrapper; |
| 12 | +import com.aspose.imaging.xmp.schemas.xmpbaseschema.XmpBasicPackage; |
| 13 | + |
| 14 | +import java.util.Arrays; |
| 15 | + |
| 16 | +public class ExtendExifMetadataForRasterImage |
| 17 | +{ |
| 18 | + public static void main(String... args) |
| 19 | + { |
| 20 | + Logger.startExample(); |
| 21 | + // The path to the documents' directory. |
| 22 | + String dataDir = Utils.getSharedDataDir() + "PNG/"; |
| 23 | + |
| 24 | + String filePath = Path.combine(dataDir, "00020.png"); |
| 25 | + String outputPath = filePath + ".edited.png"; |
| 26 | + editSourceImageMetadata(filePath, outputPath); |
| 27 | + |
| 28 | + Logger.endExample(); |
| 29 | + } |
| 30 | + |
| 31 | + public static void editSourceImageMetadata(String inputPath, String outputPath) |
| 32 | + { |
| 33 | + try (Image image = Image.load(inputPath)) |
| 34 | + { |
| 35 | + if (image.getXmpData() != null) |
| 36 | + { |
| 37 | + XmpBasicPackage newPackage = new XmpBasicPackage(); |
| 38 | + newPackage.addValue("New key", "New value"); |
| 39 | + |
| 40 | + image.getXmpData().addPackage(newPackage); |
| 41 | + } |
| 42 | + |
| 43 | + if (image.getExifData() != null) |
| 44 | + { |
| 45 | + image.getExifData().setOrientation(ExifOrientation.RightTop); |
| 46 | + } |
| 47 | + |
| 48 | + image.save(outputPath); |
| 49 | + |
| 50 | + Utils.deleteFile(outputPath); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void exportSourceImageMetadata(String inputPath, String outputPath, ImageOptionsBase outputOptions) |
| 55 | + { |
| 56 | + try (Image inputImage = Image.load(inputPath)) |
| 57 | + { |
| 58 | + // Set KeepMetadata to true to export inputImage metadata profiles, if outputOptions instance does not contain ones. |
| 59 | + outputOptions.setKeepMetadata(true); |
| 60 | + |
| 61 | + inputImage.save(outputPath, outputOptions); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static void overwriteSourceImageMetadata(String inputPath, String outputPath, ImageOptionsBase outputOptions) |
| 66 | + { |
| 67 | + try (Image image = Image.load(inputPath)) |
| 68 | + { |
| 69 | + IImageMetadataFormat[] newMetadata = GetNewMetadata(); |
| 70 | + |
| 71 | + // Try to set metadata, if the image format support metadata format type. |
| 72 | + for (IImageMetadataFormat metadata : newMetadata) |
| 73 | + { |
| 74 | + if (outputOptions.trySetMetadata(metadata)) |
| 75 | + { |
| 76 | + System.out.format("%s image supports %s", |
| 77 | + outputOptions.getClass().getName(), metadata.getClass().getName()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Or set metadata directly without image and metadata format compatibility check. |
| 82 | + outputOptions.setExifData(Arrays.stream(newMetadata) |
| 83 | + .filter(m -> m instanceof ExifData) |
| 84 | + .limit(1) |
| 85 | + .map(it -> (ExifData)it) |
| 86 | + .findFirst().get()); |
| 87 | + outputOptions.setXmpData(Arrays.stream(newMetadata) |
| 88 | + .filter(m -> m instanceof XmpPacketWrapper) |
| 89 | + .limit(1) |
| 90 | + .map(it -> (XmpPacketWrapper)it) |
| 91 | + .findFirst().get()); //as XmpPacketWrapper; |
| 92 | + |
| 93 | + image.save(outputPath, outputOptions); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static IImageMetadataFormat[] GetNewMetadata() |
| 98 | + { |
| 99 | + XmpPacketWrapper xmpData = new XmpPacketWrapper(); |
| 100 | + XmpBasicPackage xmpPackage = new XmpBasicPackage(); |
| 101 | + xmpPackage.addValue("User key", "User value"); |
| 102 | + xmpData.addPackage(xmpPackage); |
| 103 | + |
| 104 | + return new IImageMetadataFormat[] |
| 105 | + { |
| 106 | + xmpData, |
| 107 | + new ExifData() |
| 108 | + {{ |
| 109 | + setOrientation(ExifOrientation.RightTop); |
| 110 | + }}, |
| 111 | + }; |
| 112 | + } |
| 113 | +} |
0 commit comments