Skip to content

Instantly share code, notes, and snippets.

@haideralipunjabi
Last active January 27, 2017 13:58
Show Gist options
  • Save haideralipunjabi/b2a27316b08e5c430c1e184a11da6d29 to your computer and use it in GitHub Desktop.
Save haideralipunjabi/b2a27316b08e5c430c1e184a11da6d29 to your computer and use it in GitHub Desktop.
Class used to extract exif information from a given text
/**
* ExifCaption
* Class used to extract exif information from a given text
* @param ISO the ISO info
* @oaram Aperture the Aperture info
* @param ShutterSpeed the Shutter Speed info
* @param DeviceName the DeviceName info
* @oaram caption the String from which the EXIF info is to be extracted
*/
public static class ExifCaption{
public String ISO, Aperture, ShutterSpeed, DeviceName;
private String caption;
public ExifCaption(String caption){
this.caption = caption;
FindExif();
Log.e("Test", ISO+ Aperture+ShutterSpeed);
}
/**
* FindExif()
* Method to find different exif info from the caption
*/
/**
* EXTRA INFO
* I used String[] names as isoLabels, apertureLabels, etc so that if required, mulitple filter strings can be used as done in case of Shutter Speed
* if identified means if the text has distinguished different Exif Data using ISO:, Aperture:, etc
*/
private void FindExif(){
caption = caption.toLowerCase(); /**lowered the case of everything to make search easier**/
if(caption.contains("exif")){ /**Deletes string before 'exif' if it exists, because of low probability of any exif data before it**/
caption = caption.substring(caption.indexOf("exif"));
}
/**
* Filtering algorithm for ISO if identified
*/
String[] isoLabels = {"iso"};
String isoLabel = null;
for(String str: isoLabels){
if(isoLabel == null && caption.contains(str)) isoLabel = str;
}
if(isoLabel != null) /**Based on the fact that string has the iso labelled **/
{
String tempISO = caption.substring(caption.indexOf(isoLabel)).substring(isoLabel.length()).replace(":", "").replace(" ", "").replace("-", ""); /**Deletes string before and including 'iso', removes all spaces, hyphens '-' and colons ':' **/
if(indexOfFirstDigit(tempISO) == 0){ /**Checks whether the first character in string is a digit or not, if it does, it has high chances of being the ISO value**/
ISO = filterForFirstDigits(tempISO); /**Returns the first group of digits without having any other character between them**/
}
}
/**
* Filtering algorithm for Aperture if identified
*/
String[] apertureLabels = {"aperture"};
String apertureLabel = null;
for(String str: apertureLabels){
if(apertureLabel == null && caption.contains(str)) apertureLabel = str;
}
if(apertureLabel != null) /**Based on the fact that string has the aperture labelled**/
{
String tempAperture = caption.substring(caption.indexOf(apertureLabel)).substring(apertureLabel.length()).replace(":", "").replace(" ", "").replace("-", "").replace("f/", ""); /**Deletes string before and including 'aperture', removes all spaces, hyphens '-', colons ':' and 'f/' which some devices use as prefix with aperture values **/
if(indexOfFirstDigit(tempAperture) == 0)
{
Aperture = filterForFirstDecimals(tempAperture); /**Returns the first group found of the form 'x.x' **/
}
}
/**
* Filtering algorithm for Shutter Speed if identified
*/
String[] sSpeedLabels = {"shutter speed", "speed", "shutter"};
String sSpeedLabel = null;
for(String str: sSpeedLabels){
if(sSpeedLabel == null &&caption.contains(str)) sSpeedLabel = str;
}
if(sSpeedLabel != null) /**Based on the fact that string has the shutter speed labelled**/
{
String tempSpeed = caption.substring(caption.indexOf(sSpeedLabel)).substring(sSpeedLabel.length()).replace(":", "").replace(" ", "").replace("-", "").replace("f/", ""); /**Deletes string before and including shutter speed, removes all spaces, hyphens '-', colons ':' and 'f/' which some devices use as prefix with aperture values **/
if(indexOfFirstDigit(tempSpeed) == 0)
{
ShutterSpeed = filterForFirstFraction(tempSpeed); /**Returns the first string of the form 1/x or 1x**/
if(ShutterSpeed == null || ShutterSpeed.matches("null")){ /**Called if Shutter Speed isn't of form 1/x or 1x**/
ShutterSpeed = filterForFirstDigits(tempSpeed);
}
}
}
/**
* TODO LIST
* Write algorithms for ISO, Aperture, Shutter Speed when they aren't identified
* Test using multiple situations
*/
}
/**
* indexOfFirstDigit(String string)
* Description: Returns the index of first digit found in the string
* @param string String to search in
* @return
*/
private int indexOfFirstDigit(String string){
int index = -1;
boolean indexFound = false; /**boolean flag which becomes true when the first index is found**/
for(char c: string.toCharArray()){
if(!indexFound && Character.isDigit(c))
{
index = string.indexOf(c);
indexFound = true;
}
}
return index;
}
/**
* filterForFirstDigits(String string)
* Description: Returns the first group of digits found without any other character between them
* @param string String to search in
* @return
*/
private String filterForFirstDigits(String string)
{
String toReturn = null;
int initialIndex = -1;
int finalIndex = -1;
boolean indexFound = false; /**boolean flag which becomes true when the first index is found**/
for (char c:string.toCharArray() /**foreach loop to find the index of first digit**/
) {
if(!indexFound &&Character.isDigit(c)) {
initialIndex = string.indexOf(c);
indexFound = true;
}
}
if(initialIndex != -1) { /**If there is any digit present**/
string = string.substring(initialIndex); /**Delete string before first digit**/
finalIndex = string.length(); /**Keep finalIndex as the last possible index in case there is no non-digit character in string**/
indexFound = false;
for(char c:string.toCharArray()) /**foreach loop to find any non-digit character, and stores it's index as the final index**/
{
if(!indexFound &&!Character.isDigit(c)) {
finalIndex = string.indexOf(c);
indexFound = true;
}
}
string = string.substring(initialIndex, finalIndex);
toReturn = string;
}
return toReturn;
}
/**
* filterForFirstDecimals(String string)
* Description: Returns the first group of digits found with only one '.' between them
* @param string String to search in
* @return
*/
private String filterForFirstDecimals(String string)
{
String toReturn = null;
int initialIndex = -1;
int finalIndex = -1;
boolean indexFound = false; /**boolean flag which becomes true when the first index is found**/
boolean decimalFound = false;
for (char c:string.toCharArray() /**foreach loop to find the index of first digit**/
) {
if(!indexFound &&Character.isDigit(c)) {
initialIndex = string.indexOf(c);
indexFound = true;
}
}
if(initialIndex != -1) { /**If there is any digit present**/
string = string.substring(initialIndex); /**Delete string before first digit**/
finalIndex = string.length(); /**Keep finalIndex as the last possible index in case there is no non-digit character in string**/
indexFound = false;
for(char c:string.toCharArray()) /**foreach loop to find any non-digit character, and stores it's index as the final index**/
{
if(!indexFound && (!Character.isDigit(c) && c!='.')) {
finalIndex = string.indexOf(c);
indexFound = true;
}
if(c == '.' && decimalFound) {
finalIndex = string.indexOf(c);
indexFound = true;
}
if(c == '.' && !decimalFound) decimalFound = true;
}
string = string.substring(initialIndex, finalIndex);
toReturn = string;
}
return toReturn;
}
/**
* filterForFirstFraction(String string)
* Description: Returns the first group of digits found with maximum of one '/' between them
* @param string String to search in
* @return
*/
private String filterForFirstFraction(String string)
{
String toReturn = null;
int initialIndex = -1;
int finalIndex = -1;
boolean indexFound = false; /**boolean flag which becomes true when the first index is found**/
boolean decimalFound = false;
for (char c:string.toCharArray() /**foreach loop to find the index of first digit**/
) {
if(!indexFound && c == '1') {
initialIndex = string.indexOf(c);
indexFound = true;
}
}
if(initialIndex != -1) { /**If there is any digit present**/
string = string.substring(initialIndex); /**Delete string before first digit**/
finalIndex = string.length(); /**Keep finalIndex as the last possible index in case there is no non-digit character in string**/
indexFound = false;
for(char c:string.toCharArray()) /**foreach loop to find any non-digit character, and stores it's index as the final index**/
{
if(!indexFound && (!Character.isDigit(c) && c!='/')) {
finalIndex = string.indexOf(c);
indexFound = true;
}
if(c == '/' && decimalFound) {
finalIndex = string.indexOf(c);
indexFound = true;
}
if(c == '/' && !decimalFound) decimalFound = true;
}
string = string.substring(initialIndex, finalIndex);
toReturn = string;
}
return toReturn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment