Created
August 23, 2012 07:13
-
-
Save jonasgraudums/3433741 to your computer and use it in GitHub Desktop.
Get EXIF value from image
This file contains hidden or 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
import java.io.InputStream; | |
import javax.microedition.io.Connector; | |
import javax.microedition.io.file.FileConnection; | |
import net.rim.device.api.io.IOUtilities; | |
import net.rim.device.api.script.ScriptableFunction; | |
import net.rim.device.api.system.EncodedImage; | |
/** | |
* This extension returns the EXIF value for a given image and EXIF key. | |
* <p> | |
* Example: | |
* <code>getExifValue('path/to/image', 'rotation', successCallback);</code> | |
* </p> | |
*/ | |
public final class GetExifValueFunction extends ScriptableFunction { | |
public Object invoke(Object obj, Object[] args) throws Exception { | |
try { | |
if (args.length == 3) { | |
String imagePath = (String) args[0]; | |
String exifKey = (String) args[1]; | |
ScriptableFunction callback = (ScriptableFunction) args[2]; | |
new Thread(new GetExifValueRunnable(imagePath, exifKey, callback)) | |
.start(); | |
} | |
} catch (Exception e) { | |
throw new Exception(e.getMessage()); | |
} | |
return UNDEFINED; | |
} | |
private String getExifValue(String imagePath, String exifKey) | |
throws Exception { | |
FileConnection fileConnection = null; | |
InputStream inputStream = null; | |
String exifValue = ""; | |
byte[] imageBytes = null; | |
try { | |
fileConnection = (FileConnection) Connector.open(imagePath); | |
inputStream = fileConnection.openInputStream(); | |
imageBytes = new byte[(int) fileConnection.fileSize()]; | |
imageBytes = IOUtilities.streamToBytes(inputStream); | |
inputStream.close(); | |
fileConnection.close(); | |
EncodedImage encodedImage = EncodedImage.createEncodedImage(imageBytes, | |
0, imageBytes.length); | |
exifValue = encodedImage.getMetaData().getKeyValue(exifKey); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} finally { | |
if (inputStream != null) | |
inputStream.close(); | |
if (fileConnection != null) | |
fileConnection.close(); | |
} | |
return exifValue; | |
} | |
private class GetExifValueRunnable implements Runnable { | |
private final ScriptableFunction callBack; | |
private final String imagePath; | |
private final String exifKey; | |
public GetExifValueRunnable(String imagePath, String exifKey, | |
ScriptableFunction callback) { | |
this.exifKey = exifKey; | |
this.imagePath = imagePath; | |
this.callBack = callback; | |
} | |
public void run() { | |
try { | |
String exifValue = getExifValue(imagePath, exifKey); | |
String[] args = new String[1]; | |
args[0] = exifValue; | |
callBack.invoke(callBack, args); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment