Last active
May 24, 2017 10:08
-
-
Save DamienDabernat/897e29143311af5cae4200a6018bceec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.aboutgoods.agkit.scan.utils; | |
import android.media.ExifInterface; | |
import android.os.Build; | |
import android.util.Log; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.File; | |
public class ExifCopier { | |
private static final String TAG = "ExifCopier"; | |
private static String make; | |
private static String model; | |
private static float aperture; | |
private static String datetime; | |
private static float exposureTime; | |
private static float flash; | |
private static String focalLength; | |
private static String gpsAltitude; | |
private static String gpsAltitudeRef; | |
private static String gpsDatestamp; | |
private static String gpsLatitude; | |
private static String gpsLatitudeRef; | |
private static String gpsLongitude; | |
private static String gpsLongitudeRef; | |
private static String gpsProcessingMethod; | |
private static String gpsTimestamp; | |
private static int imageLength; | |
private static int imageWidth; | |
private static String iso; | |
private static int orientation; | |
private static int whiteBalance; | |
private ExifCopier() { | |
} | |
public static ExifCopier saveExifData(File file) { | |
try { | |
// copy paste exif information from original file | |
ExifInterface exif = new ExifInterface(file.getPath()); | |
int build = Build.VERSION.SDK_INT; | |
// From API 11 | |
if (build >= 11) { | |
aperture = Float.parseFloat(exif.getAttribute("FNumber")); | |
exposureTime = Float.parseFloat(exif.getAttribute("ExposureTime")); | |
iso = exif.getAttribute("ISOSpeedRatings"); | |
} | |
// From API 9 | |
if (build >= 9) { | |
gpsAltitude = exif.getAttribute("GPSAltitude"); | |
gpsAltitudeRef = exif.getAttribute("GPSAltitudeRef"); | |
} | |
// From API 8 | |
if (build >= 8) { | |
focalLength = exif.getAttribute("FocalLength"); | |
gpsDatestamp = exif.getAttribute("GPSDateStamp"); | |
gpsProcessingMethod = exif.getAttribute("GPSProcessingMethod"); | |
gpsTimestamp = exif.getAttribute("GPSTimeStamp"); | |
} | |
datetime = exif.getAttribute("DateTime"); | |
//flash = Float.parseFloat(exif.getAttribute("Flash")); | |
gpsLatitude = exif.getAttribute("GPSLatitude"); | |
gpsLatitudeRef = exif.getAttribute("GPSLatitudeRef"); | |
gpsLongitude = exif.getAttribute("GPSLongitude"); | |
gpsLongitudeRef = exif.getAttribute("GPSLongitudeRef"); | |
imageLength = Integer.parseInt(exif.getAttribute("ImageLength")); | |
imageWidth = Integer.parseInt(exif.getAttribute("ImageWidth")); | |
make = exif.getAttribute("Make"); | |
model = exif.getAttribute("Model"); | |
orientation = Integer.parseInt(exif.getAttribute("Orientation")); | |
whiteBalance = Integer.parseInt(exif.getAttribute("WhiteBalance")); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
Log.e(TAG, "saveExifData: " + e.getMessage()); | |
} | |
return new ExifCopier(); | |
} | |
public void copyTo(File file) { | |
try { | |
// copy paste exif information from original file to new | |
// file | |
ExifInterface exif = new ExifInterface(file.getPath()); | |
int build = Build.VERSION.SDK_INT; | |
// From API 11 | |
if (build >= 11) { | |
exif.setAttribute("FNumber", String.valueOf(aperture)); | |
exif.setAttribute("ExposureTime", String.valueOf(exposureTime)); | |
exif.setAttribute("ISOSpeedRatings", iso); | |
} | |
// From API 9 | |
if (build >= 9) { | |
exif.setAttribute("GPSAltitude", gpsAltitude); | |
exif.setAttribute("GPSAltitudeRef", gpsAltitudeRef); | |
} | |
// From API 8 | |
if (build >= 8) { | |
exif.setAttribute("FocalLength", focalLength); | |
exif.setAttribute("GPSDateStamp", gpsDatestamp); | |
exif.setAttribute("GPSProcessingMethod", gpsProcessingMethod); | |
exif.setAttribute("GPSTimeStamp", gpsTimestamp); | |
} | |
exif.setAttribute("DateTime", datetime); | |
exif.setAttribute("Flash", String.valueOf(flash)); | |
exif.setAttribute("GPSLatitude", gpsLatitude); | |
exif.setAttribute("GPSLatitudeRef", gpsLatitudeRef); | |
exif.setAttribute("GPSLongitude", gpsLongitude); | |
exif.setAttribute("GPSLongitudeRef", gpsLongitudeRef); | |
exif.setAttribute("ImageLength", String.valueOf(imageLength)); | |
exif.setAttribute("ImageWidth", String.valueOf(imageWidth)); | |
exif.setAttribute("Make", make); | |
exif.setAttribute("Model", model); | |
exif.setAttribute("Orientation", String.valueOf(orientation)); | |
exif.setAttribute("WhiteBalance", String.valueOf(whiteBalance)); | |
exif.saveAttributes(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public int getOrientation() { | |
return orientation; | |
} | |
public JSONObject getJsonExifInfo() { | |
JSONObject json = new JSONObject(); | |
try { | |
json.put("make", make); | |
json.put("model", model); | |
json.put("aperture", aperture); | |
json.put("datetime", datetime); | |
json.put("exposureTime", exposureTime); | |
json.put("flash", flash); | |
json.put("focalLength", focalLength); | |
json.put("gpsAltitude", gpsAltitude); | |
json.put("gpsAltitudeRef", gpsAltitudeRef); | |
json.put("gpsDatestamp", gpsDatestamp); | |
json.put("gpsLatitude", gpsLatitude); | |
json.put("gpsLatitudeRef", gpsLatitudeRef); | |
json.put("gpsLongitude", gpsLongitude); | |
json.put("gpsLongitudeRef", gpsLongitudeRef); | |
json.put("gpsProcessingMethod", gpsProcessingMethod); | |
json.put("gpsTimestamp", gpsTimestamp); | |
json.put("imageLength", imageLength); | |
json.put("imageWidth", imageWidth); | |
json.put("iso", iso); | |
json.put("orientation", orientation); | |
json.put("whiteBalance", whiteBalance); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return json; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment