|
4 | 4 |
|
5 | 5 | package io.flutter.plugins.imagepicker;
|
6 | 6 |
|
7 |
| -import android.util.Log; |
8 | 7 | import androidx.exifinterface.media.ExifInterface;
|
| 8 | +import java.io.IOException; |
9 | 9 | import java.util.Arrays;
|
10 | 10 | import java.util.List;
|
11 | 11 |
|
12 | 12 | class ExifDataCopier {
|
13 |
| - void copyExif(String filePathOri, String filePathDest) { |
14 |
| - try { |
15 |
| - ExifInterface oldExif = new ExifInterface(filePathOri); |
16 |
| - ExifInterface newExif = new ExifInterface(filePathDest); |
17 |
| - |
18 |
| - List<String> attributes = |
19 |
| - Arrays.asList( |
20 |
| - "FNumber", |
21 |
| - "ExposureTime", |
22 |
| - "ISOSpeedRatings", |
23 |
| - "GPSAltitude", |
24 |
| - "GPSAltitudeRef", |
25 |
| - "FocalLength", |
26 |
| - "GPSDateStamp", |
27 |
| - "WhiteBalance", |
28 |
| - "GPSProcessingMethod", |
29 |
| - "GPSTimeStamp", |
30 |
| - "DateTime", |
31 |
| - "Flash", |
32 |
| - "GPSLatitude", |
33 |
| - "GPSLatitudeRef", |
34 |
| - "GPSLongitude", |
35 |
| - "GPSLongitudeRef", |
36 |
| - "Make", |
37 |
| - "Model", |
38 |
| - "Orientation"); |
39 |
| - for (String attribute : attributes) { |
40 |
| - setIfNotNull(oldExif, newExif, attribute); |
41 |
| - } |
42 |
| - |
43 |
| - newExif.saveAttributes(); |
44 |
| - |
45 |
| - } catch (Exception ex) { |
46 |
| - Log.e("ExifDataCopier", "Error preserving Exif data on selected image: " + ex); |
| 13 | + /** |
| 14 | + * Copies all exif data not related to image structure and orientation tag. Data not related to |
| 15 | + * image structure consists of category II (Shooting condition related metadata) and category III |
| 16 | + * (Metadata storing other information) tags. Category I tags are not copied because they may be |
| 17 | + * invalidated as a result of resizing. The exception is the orientation tag which is known to not |
| 18 | + * be invalidated and is crucial for proper display of the image. |
| 19 | + * |
| 20 | + * <p>The categories mentioned refer to standard "CIPA DC-008-Translation-2012 Exchangeable image |
| 21 | + * file format for digital still cameras: Exif Version 2.3" |
| 22 | + * https://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf. Version 2.3 has been chosen because |
| 23 | + * {@code ExifInterface} is based on it. |
| 24 | + */ |
| 25 | + void copyExif(ExifInterface oldExif, ExifInterface newExif) throws IOException { |
| 26 | + @SuppressWarnings("deprecation") |
| 27 | + List<String> attributes = |
| 28 | + Arrays.asList( |
| 29 | + ExifInterface.TAG_IMAGE_DESCRIPTION, |
| 30 | + ExifInterface.TAG_MAKE, |
| 31 | + ExifInterface.TAG_MODEL, |
| 32 | + ExifInterface.TAG_SOFTWARE, |
| 33 | + ExifInterface.TAG_DATETIME, |
| 34 | + ExifInterface.TAG_ARTIST, |
| 35 | + ExifInterface.TAG_COPYRIGHT, |
| 36 | + ExifInterface.TAG_EXPOSURE_TIME, |
| 37 | + ExifInterface.TAG_F_NUMBER, |
| 38 | + ExifInterface.TAG_EXPOSURE_PROGRAM, |
| 39 | + ExifInterface.TAG_SPECTRAL_SENSITIVITY, |
| 40 | + ExifInterface.TAG_PHOTOGRAPHIC_SENSITIVITY, |
| 41 | + ExifInterface.TAG_ISO_SPEED_RATINGS, |
| 42 | + ExifInterface.TAG_OECF, |
| 43 | + ExifInterface.TAG_SENSITIVITY_TYPE, |
| 44 | + ExifInterface.TAG_STANDARD_OUTPUT_SENSITIVITY, |
| 45 | + ExifInterface.TAG_RECOMMENDED_EXPOSURE_INDEX, |
| 46 | + ExifInterface.TAG_ISO_SPEED, |
| 47 | + ExifInterface.TAG_ISO_SPEED_LATITUDE_YYY, |
| 48 | + ExifInterface.TAG_ISO_SPEED_LATITUDE_ZZZ, |
| 49 | + ExifInterface.TAG_EXIF_VERSION, |
| 50 | + ExifInterface.TAG_DATETIME_ORIGINAL, |
| 51 | + ExifInterface.TAG_DATETIME_DIGITIZED, |
| 52 | + ExifInterface.TAG_OFFSET_TIME, |
| 53 | + ExifInterface.TAG_OFFSET_TIME_ORIGINAL, |
| 54 | + ExifInterface.TAG_OFFSET_TIME_DIGITIZED, |
| 55 | + ExifInterface.TAG_SHUTTER_SPEED_VALUE, |
| 56 | + ExifInterface.TAG_APERTURE_VALUE, |
| 57 | + ExifInterface.TAG_BRIGHTNESS_VALUE, |
| 58 | + ExifInterface.TAG_EXPOSURE_BIAS_VALUE, |
| 59 | + ExifInterface.TAG_MAX_APERTURE_VALUE, |
| 60 | + ExifInterface.TAG_SUBJECT_DISTANCE, |
| 61 | + ExifInterface.TAG_METERING_MODE, |
| 62 | + ExifInterface.TAG_LIGHT_SOURCE, |
| 63 | + ExifInterface.TAG_FLASH, |
| 64 | + ExifInterface.TAG_FOCAL_LENGTH, |
| 65 | + ExifInterface.TAG_MAKER_NOTE, |
| 66 | + ExifInterface.TAG_USER_COMMENT, |
| 67 | + ExifInterface.TAG_SUBSEC_TIME, |
| 68 | + ExifInterface.TAG_SUBSEC_TIME_ORIGINAL, |
| 69 | + ExifInterface.TAG_SUBSEC_TIME_DIGITIZED, |
| 70 | + ExifInterface.TAG_FLASHPIX_VERSION, |
| 71 | + ExifInterface.TAG_FLASH_ENERGY, |
| 72 | + ExifInterface.TAG_SPATIAL_FREQUENCY_RESPONSE, |
| 73 | + ExifInterface.TAG_FOCAL_PLANE_X_RESOLUTION, |
| 74 | + ExifInterface.TAG_FOCAL_PLANE_Y_RESOLUTION, |
| 75 | + ExifInterface.TAG_FOCAL_PLANE_RESOLUTION_UNIT, |
| 76 | + ExifInterface.TAG_EXPOSURE_INDEX, |
| 77 | + ExifInterface.TAG_SENSING_METHOD, |
| 78 | + ExifInterface.TAG_FILE_SOURCE, |
| 79 | + ExifInterface.TAG_SCENE_TYPE, |
| 80 | + ExifInterface.TAG_CFA_PATTERN, |
| 81 | + ExifInterface.TAG_CUSTOM_RENDERED, |
| 82 | + ExifInterface.TAG_EXPOSURE_MODE, |
| 83 | + ExifInterface.TAG_WHITE_BALANCE, |
| 84 | + ExifInterface.TAG_DIGITAL_ZOOM_RATIO, |
| 85 | + ExifInterface.TAG_FOCAL_LENGTH_IN_35MM_FILM, |
| 86 | + ExifInterface.TAG_SCENE_CAPTURE_TYPE, |
| 87 | + ExifInterface.TAG_GAIN_CONTROL, |
| 88 | + ExifInterface.TAG_CONTRAST, |
| 89 | + ExifInterface.TAG_SATURATION, |
| 90 | + ExifInterface.TAG_SHARPNESS, |
| 91 | + ExifInterface.TAG_DEVICE_SETTING_DESCRIPTION, |
| 92 | + ExifInterface.TAG_SUBJECT_DISTANCE_RANGE, |
| 93 | + ExifInterface.TAG_IMAGE_UNIQUE_ID, |
| 94 | + ExifInterface.TAG_CAMERA_OWNER_NAME, |
| 95 | + ExifInterface.TAG_BODY_SERIAL_NUMBER, |
| 96 | + ExifInterface.TAG_LENS_SPECIFICATION, |
| 97 | + ExifInterface.TAG_LENS_MAKE, |
| 98 | + ExifInterface.TAG_LENS_MODEL, |
| 99 | + ExifInterface.TAG_LENS_SERIAL_NUMBER, |
| 100 | + ExifInterface.TAG_GPS_VERSION_ID, |
| 101 | + ExifInterface.TAG_GPS_LATITUDE_REF, |
| 102 | + ExifInterface.TAG_GPS_LATITUDE, |
| 103 | + ExifInterface.TAG_GPS_LONGITUDE_REF, |
| 104 | + ExifInterface.TAG_GPS_LONGITUDE, |
| 105 | + ExifInterface.TAG_GPS_ALTITUDE_REF, |
| 106 | + ExifInterface.TAG_GPS_ALTITUDE, |
| 107 | + ExifInterface.TAG_GPS_TIMESTAMP, |
| 108 | + ExifInterface.TAG_GPS_SATELLITES, |
| 109 | + ExifInterface.TAG_GPS_STATUS, |
| 110 | + ExifInterface.TAG_GPS_MEASURE_MODE, |
| 111 | + ExifInterface.TAG_GPS_DOP, |
| 112 | + ExifInterface.TAG_GPS_SPEED_REF, |
| 113 | + ExifInterface.TAG_GPS_SPEED, |
| 114 | + ExifInterface.TAG_GPS_TRACK_REF, |
| 115 | + ExifInterface.TAG_GPS_TRACK, |
| 116 | + ExifInterface.TAG_GPS_IMG_DIRECTION_REF, |
| 117 | + ExifInterface.TAG_GPS_IMG_DIRECTION, |
| 118 | + ExifInterface.TAG_GPS_MAP_DATUM, |
| 119 | + ExifInterface.TAG_GPS_DEST_LATITUDE_REF, |
| 120 | + ExifInterface.TAG_GPS_DEST_LATITUDE, |
| 121 | + ExifInterface.TAG_GPS_DEST_LONGITUDE_REF, |
| 122 | + ExifInterface.TAG_GPS_DEST_LONGITUDE, |
| 123 | + ExifInterface.TAG_GPS_DEST_BEARING_REF, |
| 124 | + ExifInterface.TAG_GPS_DEST_BEARING, |
| 125 | + ExifInterface.TAG_GPS_DEST_DISTANCE_REF, |
| 126 | + ExifInterface.TAG_GPS_DEST_DISTANCE, |
| 127 | + ExifInterface.TAG_GPS_PROCESSING_METHOD, |
| 128 | + ExifInterface.TAG_GPS_AREA_INFORMATION, |
| 129 | + ExifInterface.TAG_GPS_DATESTAMP, |
| 130 | + ExifInterface.TAG_GPS_DIFFERENTIAL, |
| 131 | + ExifInterface.TAG_GPS_H_POSITIONING_ERROR, |
| 132 | + ExifInterface.TAG_INTEROPERABILITY_INDEX, |
| 133 | + ExifInterface.TAG_ORIENTATION); |
| 134 | + for (String attribute : attributes) { |
| 135 | + setIfNotNull(oldExif, newExif, attribute); |
47 | 136 | }
|
| 137 | + |
| 138 | + newExif.saveAttributes(); |
48 | 139 | }
|
49 | 140 |
|
50 | 141 | private static void setIfNotNull(ExifInterface oldExif, ExifInterface newExif, String property) {
|
|
0 commit comments