Exif: Manipulating EXIF Data from JPEG Files
This tutorial demonstrates how you can use EXIF (exchangeable image format) information stored in JPEG files.
The Exif API is mandatory for both Tizen mobile and wearable profiles, which means that it is supported in all mobile and wearable devices. The Exif API is supported on all Tizen Emulators.
Warm-up
Become familiar with the Exif API basics by learning about:
-
Loading the EXIF Data
Retrieve the EXIF data from specific file.
-
Adding EXIF Data
Add EXIF data to a JPEG file with no current EXIF information.
-
Updating the EXIF Data
Update the EXIF data in a JPEG file.
-
Copying the EXIF Data
Copy the EXIF data from one JPEG file to another.
Loading the EXIF Data
Learning how to retrieve EXIF data from JPEG files is a useful content management skill:
-
To retrieve the EXIF data from specific file, you need the URI of an image file in an absolute URI format (for example file:///opt/usr/media/Images/tizen.jpg). The tizen.filesystem.resolve() and toURI() methods can be used to convert a virtual path to a URI.
var fileURI = ""; function resolveSuccess(file) { fileURI = file.toURI(); console.log("Successfully resolved file: " + fileURI); } function resolveFail(error) { console.log("resolve() error occurred: " + error.name + " with message: " + error.message); } tizen.filesystem.resolve("images/tizen.jpg", resolveSuccess, resolveFail);
-
With a valid File object, use the getExifInfo() method of the ExifManager interface (in mobile and wearable applications) and pass the URI to the method.
function onSuccess(exifInfo) { console.log("Successfully got EXIF information object"); } function onError(error) { console.log("Error occurred: " + error.name + " with message:" + error.message); } tizen.exif.getExifInfo(fileURI, onSuccess, onError);
With a valid exifInfo object, you can access various ExifInformation attributes (in mobile and wearable applications), such as width, height, orientation, and flash.
- To retrieve an EXIF thumbnail of the image:
- Resolve the input JPEG file:
var fileURI = ""; tizen.filesystem.resolve("images/tizen.jpg", resolveSuccess, resolveFail);
- Use the getThumbnail() method to retrieve the thumbnail:
function onSuccess(thumbData) { console.log("Got thumbnail data from EXIF stored in JPEG file"); if (thumbData) { var img = new Image(); img.src = thumbData; document.body.appendChild(img); } } function onError(error) { console.log("Error occurred: " + error.name + " with message: " + error.message); } function resolveSuccess(file) { fileURI = file.toURI(); console.log("Successfully resolved file: " + file.toURI()); tizen.exif.getThumbnail(fileURI, onSuccess, onError); }
- Resolve the input JPEG file:
Adding EXIF Data
Learning how to add EXIF data to JPEG files is a useful content management skill:
-
Create a new ExifInformation object (in mobile and wearable applications):
var myNewExif = new tizen.ExifInformation(); myNewExif.userComment = "Photo taken on Charles Bridge in Prague"; myNewExif.gpsLocation = new tizen.SimpleCoordinates(50.086447, 14.411856);
-
Resolve the virtual path to the output JPEG file and get the URI:
var fileNoExifURI = ""; function resolveSuccess(file) { fileNoExifURI = file.toURI(); console.log("Successfully resolved file: " + fileNoExifURI); } function resolveFail(error) { console.log("Error occurred: " + error.name + " with message: " + error.message); } tizen.filesystem.resolve("images/image_without_exif.jpg", resolveSuccess, resolveFail);
-
When you have a valid URI to the file, set it in the myNewExif object and call the saveExifInfo() method of the ExifManager interface (in mobile and wearable applications):
function onSaveSuccess() { console.log("Successfully saved EXIF information to JPEG file"); } function onSaveError(error) { console.log("Error occurred:" + error.name + " with message:" + error.message); } myNewExif.uri = fileNoExifURI; tizen.exif.saveExifInfo(myNewExif, onSaveSuccess, onSaveError);
Updating the EXIF Data
Learning how to update EXIF data in JPEG files is a useful content management skill:
-
To update the EXIF data, load the ExifInformation object (in mobile and wearable applications) from the file and change the values of the object properties.
You can remove information from the file by setting the property to null.
function getSuccess(exifInfo) { exifInfo.orientation = "FLIP_HORIZONTAL"; exifInfo.userComment = "Great times!"; /* Remove basic GPS information */ exifInfo.gpsLocation = null; exifInfo.gpsAltitude = null; } tizen.filesystem.resolve("images/photo.jpg", function(file) { tizen.exif.getExifInfo(file.toURI(), getSuccess); });
- After updating the property values, use the saveExifInfo() method of the ExifManager interface (in mobile and wearable applications) to save the changes to the file:
function saveSuccess(exifInfo) { console.log("new EXIF saved"); } tizen.exif.saveExifInfo(exifInfo, saveSuccess);
Copying the EXIF Data
Learning how to copy EXIF data between JPEG files is a useful content management skill:
-
Get the ExifInformation object (in mobile and wearable applications) and resolve the output file, and then change the sourceExifInfo.uri attribute to point to the output JPEG file:
var sourceExifInfo = null; function resolveOutSuccess(outFile) { console.log("Successfully resolved file: " + outFile.toURI()); sourceExifInfo.uri = outFile.toURI(); } function resolveOutFail(error) { console.log("Error occurred: " + error.name + " with message: " + error.message); } function onSuccess(exifInfo) { console.log("Successfully got EXIF information object"); sourceExifInfo = exifInfo; tizen.filesystem.resolve("images/image_without_exif.jpg", resolveOutSuccess, resolveOutFail); } tizen.exif.getExifInfo(fileURI, onSuccess);
- Use the saveExifInfo() method of the ExifManager interface (in mobile and wearable applications) to save the changes in the output JPEG file:
function onSaveSuccess() { console.log("Successfully saved EXIF information to JPEG file"); } function onSaveError(error) { console.log("Error occurred:" + error.name + " with message:" + error.message); } function resolveOutSuccess(outFile) { console.log("Successfully resolved file: " + outFile.toURI()); sourceExifInfo.uri = outFile.toURI(); tizen.exif.saveExifInfo(sourceExifInfo, onSaveSuccess, onSaveError); }