Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import android.util.Log;
import org.apache.sanselan.formats.tiff.constants.TagInfo;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
public class ImageDataParser extends ImageDataContainer{
private static HashMap<String, TagInfo> Tags = initializeTags();
public static ArrayList<File> traverse(File dir) {
if (dir.exists()) {
ArrayList<File> listOfFiles = new ArrayList<>();
File[] files = dir.listFiles();
for (int i = 0; i < files.length; ++i) {
File file = files[i];
if (file.isDirectory()) {
traverse(file);
} else {
listOfFiles.add(file);
// do something here with the file
}
}
return listOfFiles;
}
return null;
}
public static ArrayList<JpgMetadata> iterateFiles(String filePath){
ArrayList<File> listOfFiles = traverse(new File(filePath));
if(listOfFiles == null){
Log.e("sdcard NOT FOUND", "SDCARD NOT FOUND in ImageDataParser");
return null;
}
ArrayList<JpgMetadata> listJpgData = new ArrayList<>();
for(int i = 0; i < listOfFiles.size(); i++){
File file = listOfFiles.get(i);
String name = file.getName();
String x_resolution = getFileTag(file, Tags.get("x_resolution"));
String y_resolution = getFileTag(file, Tags.get("y_resolution"));
String date = getFileTag(file, Tags.get("date"));
String lat_ref = getFileTag(file, Tags.get("gps_lat_ref"));
String lon_ref = getFileTag(file, Tags.get("gps_lon_ref"));
String lat_dms = getFileTag(file, Tags.get("gps_lat"));
String lon_dms = getFileTag(file, Tags.get("gps_lon"));
String desc = getFileTag(file, Tags.get("description"));
double[] lat = parseDMS(lat_dms);
double[] lon = parseDMS(lon_dms);
JpgMetadata jpgData = formatToJPGMetadata(file, name, x_resolution, y_resolution, date, lat_ref, lon_ref, lat, lon, desc);
listJpgData.add(jpgData);
}
return listJpgData;
}
private static JpgMetadata formatToJPGMetadata(File file, String name, String x_res, String y_res, String date, String lat_ref, String lon_ref, double[] lat, double[] lon, String desc){
int x_rs = -1;
int y_rs = -1;
if(isEmptyTag(name)) name = "";
if(!isEmptyTag(x_res)) x_rs = Integer.valueOf(x_res);
if(!isEmptyTag(y_res)) y_rs = Integer.valueOf(y_res);
if(isEmptyTag(date)) date = "";
if(isEmptyTag(lat_ref)) lat_ref = "";
if(isEmptyTag(lon_ref)) lon_ref ="";
if(isEmptyTag(desc)) desc = "";
return new JpgMetadata(name, file, lat_ref, lon_ref, lat, lon, x_rs, y_rs);
}
private static double[] parseDMS(String str){
double[] res = new double[3];
//String format : val degs, val mins, val secs
if(str == "NoTagVal"){
res[0] = -1; res[1] = -1; res[2] = -1;
return res;
}
int i1 = str.indexOf(" degs");
double degs = Double.valueOf(str.substring(0,i1));
i1 += " degs,".length();
int i2 = str.indexOf(" mins,");
double mins = Double.valueOf(str.substring(i1, i2));
i1 = i2 + " mins,".length();
i2 = str.indexOf(" secs");
double secs = Double.valueOf(str.substring(i1,i2));
res[0] = degs;
res[1] = mins;
res[2] = secs;
return res;
}
private static boolean isEmptyTag(String tagVal){
if(tagVal.equals("NoTagVal")){
return true;
}
return false;
}
public static String[] getImageGPSInfo(File file){
String gps_lat_ref = getFileTag(file, Tags.get("gps_lat_ref"));
String gps_lon_ref = getFileTag(file, Tags.get("gps_lon_ref"));
String gps_lat = getFileTag(file, Tags.get("gps_lat"));
String gps_lon = getFileTag(file, Tags.get("gps_lon"));
System.out.printf("GPS LAT REF " + gps_lat_ref);
System.out.printf("GPS LON REF " + gps_lon_ref);
System.out.printf("GPS LAT " + gps_lat);
System.out.printf("GPS LON " + gps_lon);
String[] res = new String[4];
res[0] = gps_lat_ref;
res[1] = gps_lon_ref;
res[2] = gps_lat;
res[3] = gps_lon;
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment