Advertisement
Guest User

Untitled

a guest
May 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. package net.aboutgoods.agkit.scan.utils;
  2.  
  3. import android.media.ExifInterface;
  4. import android.os.Build;
  5. import android.util.Log;
  6.  
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9.  
  10. import java.io.File;
  11.  
  12. public class ExifCopier {
  13.  
  14. private static final String TAG = "ExifCopier";
  15.  
  16. private static String make;
  17. private static String model;
  18. private static float aperture;
  19. private static String datetime;
  20. private static float exposureTime;
  21. private static float flash;
  22. private static String focalLength;
  23. private static String gpsAltitude;
  24. private static String gpsAltitudeRef;
  25. private static String gpsDatestamp;
  26. private static String gpsLatitude;
  27. private static String gpsLatitudeRef;
  28. private static String gpsLongitude;
  29. private static String gpsLongitudeRef;
  30. private static String gpsProcessingMethod;
  31. private static String gpsTimestamp;
  32. private static int imageLength;
  33. private static int imageWidth;
  34. private static String iso;
  35. private static int orientation;
  36. private static int whiteBalance;
  37.  
  38. private ExifCopier() {
  39. }
  40.  
  41. public static ExifCopier saveExifData(File file) {
  42. try {
  43. // copy paste exif information from original file
  44. ExifInterface exif = new ExifInterface(file.getPath());
  45.  
  46. int build = Build.VERSION.SDK_INT;
  47.  
  48. // From API 11
  49. if (build >= 11) {
  50. aperture = Float.parseFloat(exif.getAttribute("FNumber"));
  51. exposureTime = Float.parseFloat(exif.getAttribute("ExposureTime"));
  52. iso = exif.getAttribute("ISOSpeedRatings");
  53. }
  54. // From API 9
  55. if (build >= 9) {
  56. gpsAltitude = exif.getAttribute("GPSAltitude");
  57. gpsAltitudeRef = exif.getAttribute("GPSAltitudeRef");
  58. }
  59. // From API 8
  60. if (build >= 8) {
  61. focalLength = exif.getAttribute("FocalLength");
  62. gpsDatestamp = exif.getAttribute("GPSDateStamp");
  63. gpsProcessingMethod = exif.getAttribute("GPSProcessingMethod");
  64. gpsTimestamp = exif.getAttribute("GPSTimeStamp");
  65. }
  66.  
  67. datetime = exif.getAttribute("DateTime");
  68. //flash = Float.parseFloat(exif.getAttribute("Flash"));
  69. gpsLatitude = exif.getAttribute("GPSLatitude");
  70. gpsLatitudeRef = exif.getAttribute("GPSLatitudeRef");
  71. gpsLongitude = exif.getAttribute("GPSLongitude");
  72. gpsLongitudeRef = exif.getAttribute("GPSLongitudeRef");
  73.  
  74. imageLength = Integer.parseInt(exif.getAttribute("ImageLength"));
  75. imageWidth = Integer.parseInt(exif.getAttribute("ImageWidth"));
  76.  
  77. make = exif.getAttribute("Make");
  78. model = exif.getAttribute("Model");
  79. orientation = Integer.parseInt(exif.getAttribute("Orientation"));
  80. whiteBalance = Integer.parseInt(exif.getAttribute("WhiteBalance"));
  81.  
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. Log.e(TAG, "saveExifData: " + e.getMessage());
  85. }
  86.  
  87. return new ExifCopier();
  88. }
  89.  
  90. public void copyTo(File file) {
  91. try {
  92. // copy paste exif information from original file to new
  93. // file
  94. ExifInterface exif = new ExifInterface(file.getPath());
  95.  
  96. int build = Build.VERSION.SDK_INT;
  97.  
  98. // From API 11
  99. if (build >= 11) {
  100. exif.setAttribute("FNumber", String.valueOf(aperture));
  101. exif.setAttribute("ExposureTime", String.valueOf(exposureTime));
  102. exif.setAttribute("ISOSpeedRatings", iso);
  103. }
  104. // From API 9
  105. if (build >= 9) {
  106. exif.setAttribute("GPSAltitude", gpsAltitude);
  107. exif.setAttribute("GPSAltitudeRef", gpsAltitudeRef);
  108. }
  109. // From API 8
  110. if (build >= 8) {
  111. exif.setAttribute("FocalLength", focalLength);
  112. exif.setAttribute("GPSDateStamp", gpsDatestamp);
  113. exif.setAttribute("GPSProcessingMethod", gpsProcessingMethod);
  114. exif.setAttribute("GPSTimeStamp", gpsTimestamp);
  115. }
  116.  
  117. exif.setAttribute("DateTime", datetime);
  118. exif.setAttribute("Flash", String.valueOf(flash));
  119. exif.setAttribute("GPSLatitude", gpsLatitude);
  120. exif.setAttribute("GPSLatitudeRef", gpsLatitudeRef);
  121. exif.setAttribute("GPSLongitude", gpsLongitude);
  122. exif.setAttribute("GPSLongitudeRef", gpsLongitudeRef);
  123.  
  124. exif.setAttribute("ImageLength", String.valueOf(imageLength));
  125. exif.setAttribute("ImageWidth", String.valueOf(imageWidth));
  126.  
  127. exif.setAttribute("Make", make);
  128. exif.setAttribute("Model", model);
  129. exif.setAttribute("Orientation", String.valueOf(orientation));
  130. exif.setAttribute("WhiteBalance", String.valueOf(whiteBalance));
  131.  
  132. exif.saveAttributes();
  133.  
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. }
  138.  
  139. public int getOrientation() {
  140. return orientation;
  141. }
  142.  
  143. public JSONObject getJsonExifInfo() {
  144.  
  145. JSONObject json = new JSONObject();
  146.  
  147. try {
  148. json.put("make", make);
  149. json.put("model", model);
  150. json.put("aperture", aperture);
  151. json.put("datetime", datetime);
  152. json.put("exposureTime", exposureTime);
  153. json.put("flash", flash);
  154. json.put("focalLength", focalLength);
  155. json.put("gpsAltitude", gpsAltitude);
  156. json.put("gpsAltitudeRef", gpsAltitudeRef);
  157. json.put("gpsDatestamp", gpsDatestamp);
  158. json.put("gpsLatitude", gpsLatitude);
  159. json.put("gpsLatitudeRef", gpsLatitudeRef);
  160. json.put("gpsLongitude", gpsLongitude);
  161. json.put("gpsLongitudeRef", gpsLongitudeRef);
  162. json.put("gpsProcessingMethod", gpsProcessingMethod);
  163. json.put("gpsTimestamp", gpsTimestamp);
  164. json.put("imageLength", imageLength);
  165. json.put("imageWidth", imageWidth);
  166. json.put("iso", iso);
  167. json.put("orientation", orientation);
  168. json.put("whiteBalance", whiteBalance);
  169. } catch (JSONException e) {
  170. e.printStackTrace();
  171. }
  172.  
  173. return json;
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement