- What is ExifInterface in Android?
- How to Get EXIF Data on Android?
- What Are Exif Attributes and How Do I Read Them?
- Understanding ExifInterface
- What is Exif?
- ExifInterface in Android
- How to Read Exif Attributes?
- Extracting info from content Uri
- For Camera Apps
- Handling Orientation
- Bitmap rotation using the Exif rotation angle
- Android – Correcting jpeg image orientation in View Canvas with ExifInterface orientation tag
- 3 Responses to Android – Correcting jpeg image orientation in View Canvas with ExifInterface orientation tag
- Exif Interface Class
- Definition
- Remarks
- Constructors
- Fields
- Properties
- Methods
- Explicit Interface Implementations
- Extension Methods
What is ExifInterface in Android?
We make significant use of images in our apps. For some Picture-Related Applications, all of the image information is necessary. However, we cannot always read the picture’s features immediately from the image. There may be a requirement to know picture information such as GPS position, date/time, settings at the moment of capture, orientation, and so on. Previously, we had to extract metadata from different techniques individually and read it to comprehend the Image attributes. Even if altering the photos, such as deleting GPS tags or changing the orientation, would be difficult jobs when we read. Exif files and ExifInterface are the answer to these problems.
What exactly is Exif?
Exchangeable Image File Format (Exif) is an acronym for Exchangeable Image File Format. This is a standard that specifies detailed information about a photograph or other piece of media recorded by a camera. It may save critical information like camera exposure, the date/time the image was taken, and even GPS position.
The following is an example of an Exif file:
EXIF Data Example Source Link – https://www.photographymad.com/files/images/exif-data.jpg
If you look at the image above, you’ll notice that it has attributes such as image length and width, date and time of image capture, device from which the image was captured, Orientation code, and some of the image’s attributes such as light, white balance, focal length, whether the flash was on or not, and GPS information for the image, i.e. the lat long of the image where it was captured. We can see all of the image’s features just by gazing at it. You should now have a good understanding of EXIF files.
How to Get EXIF Data on Android?
We learned what Exif is and what it includes by using the ExifInterface in Android. Let’s have a look at how we can get Exif data from pictures on Android. Since version 25.1.0, ExifInterface has been included in the android support library. Even though the feature has existed since Android 7.1, with all of the possibilities offered to the UI, it only made sense from Android 9+. This has been enhanced with over 100 characteristics for reading Exif tags, which include information on the camera, camera settings, orientation, and GPS coordinates
It can only read the Exif of JPEG files or a RAW picture file at the moment. JPEG, DNG, CR2, NEF, NRW, ARW, RW2, ORF, PEF, SRW, RAF, and HEIF are all supported formats.
You must add the following dependency to your build.gradle if you wish to utilize ExifInterface.
One thing to remember about Exif data is that there are no mandatory tags: every tag is optional, and some services even remove Exif data entirely. As a result, you should always address instances where there is no Exif data, whether it’s because there is no data for a single property or because the image format cannot allow the data at all to be linked like the infamous WebP format
What Are Exif Attributes and How Do I Read Them?
Let’s look at how ExifInterface can read EXIF characteristics from various sources and some of the usage cases. You’d just use the getAttributeInt(), getAttributeDouble(), or getAttribute() (for Strings) methods for most attributes.
Uri for extracting data from content:
Источник
Understanding ExifInterface
We use Images in our apps extensively. For some of the Image Related Applications, all the details of the images are required. But we cannot read the details of the images always directly from the image. There might be needs to know the image details like GPS location, date/time, settings at the time of capture, orientation, etc.
Previously we were struggling to get image info, for which we had to separately extract metadata from different methods and read it to understand the Image properties. Even though when we read, the changes to the images like removing GPS tags or changing the orientation would be challenging tasks.
The solution for these is Exif files and ExifInterface.
Let us understand what EXIF is?
What is Exif?
Exif stands for Exchangeable Image File Format. This is a standard that defines specific information related to an image or other media captured by a camera. It is capable of storing such important data as camera exposure, date/time the image was captured, and even GPS location. The Sample Exif file looks like,
If you look at the above image, We have attributes like Image length and width, date and time of the image taken, From which device this image was taken, Orientation code, and some of the images attribute like light, white balance, focal length, if the flash was on or not, GPS information of the image i.e lat long of the image where it was taken. By looking at this we can get the complete details of the image. Now you may get a fair idea of EXIF files.
ExifInterface in Android
We understood what is Exif and what it contains. Let’s see how can we extract Exif data from images in Android. ExifInterface has been introduced in the android support library since 25.1.0. Even though this has been existing since 7.1, It made sense from android 9+ with all the capabilities provided to the interface. This has been added with 100+ attributes to read the Exif tags of the images including information about the camera itself, the camera settings, orientation, and GPS coordinates. For now, it can only read the Exif of JPEG files or a RAW image file. The supported formats are JPEG, DNG, CR2, NEF, NRW, ARW, RW2, ORF, PEF, SRW, RAF, and HEIF.
If you want to use ExifInterface you need to add the below dependency to your build.gradle.
One thing that is important to understand with Exif data is that there are no required tags: every tag is optional — some services even specifically strip Exif data. Therefore throughout your code, you should always handle cases where there is no Exif data, either due to no data for a specific attribute or an image format that doesn’t support Exif data at all (say, the ubiquitous PNGs or WebP images).
How to Read Exif Attributes?
Now let us understand how can we read EXIF attributes for different sources and some of the use cases using ExifInterface. For most attributes, you’d simply use the getAttributeInt(), getAttributeDouble(), or getAttribute() (for Strings) methods as appropriate.
Extracting info from content Uri
Note: ExifInterface will not work with remote InputStreams, such as those returned from a HttpURLConnection. It is strongly recommended to only use them with content:// or file:// URIs.
For Camera Apps
For Camera apps, after capturing the image writing attributes is more important. As of now, it’s still limited to JPEG images. Using ExifInterface we can easily alter the already set JPEG_ORIENTATION, JPEG_GPS_LOCATION, or the equivalent. We can also remove these attributes based on the user’s request.
Handling Orientation
One of the most important attributes when it comes to displaying images is the image orientation, stored in the aptly-named TAG_ORIENTATION, which returns one of the ORIENTATION_ constants. To convert this to a rotation angle, you can post-process the value.
Bitmap rotation using the Exif rotation angle
First, create the ExifInterface:
Next, find the current rotation:
Convert Exif rotation to degrees:
Then use the image’s actual rotation as a reference point to rotate the image using a Matrix.
You create the new rotated image with the Bitmap.createBitmap method that takes a Matrix as a parameter:
where matrix holds the new rotation:
Some of the use cases for how we can use ExifInterface.
- ExifInterface is used in writing the image information when the picture is taken. It is also helpful to handle orientation while saving the image to phone storage.
- We may need to decrease the image size and update the Exif info, for this, we will have to fetch the Exif info and save the updated info back on to the image.
- ExifInterface is also used to fetch and parse the image headers.
- Keeping view orientation of the device constant when the device is rotated.
- Fetching image from the gallery and displaying it with the proper orientation and other attributes with which the image was saved.
This was all about the ExifInterface. We have learned about ExifInterface today. Hoping to have given some light on the topic.
Keep Learning, Keep Exploring, Keep Growing
Источник
Android – Correcting jpeg image orientation in View Canvas with ExifInterface orientation tag
We may have noticed that images captured in portrait mode, looks rotated 90 degrees on drawing the image in a view canvas. Also images taken from front camera looks upside down. This issue can be resolved with exif meta data embedded with jpeg images.
In this article, we will develop an Android application which reads the exif data associated with the jpeg file and and uses that information to display the image correctly.
This application is developed in Eclipse 4.2.0 with ADT Plugin (22.0.1) and Android SDK ( 22.0.1 ) .
1. Create new Android application project namely “GraphicsExifOrientation”
Figure 1 : Create new Android application project
2. Configure the project
Figure 2 : Configure the project
3. Design application launcher icon
Figure 3 : Design application launcher icon
4. Create a blank activity
Figure 4 : Create a blank activity
5. Enter MainActivity details
Figure 5 : Enter MainActivity details
6. Update the file res/values/strings.xml
7. Create a java class file src/in/wptrafficanalyzer/graphicsexiforientation/PaintView.java
8. Update the layout file res/layout/activity_main.xml
9. Update the class src/in/wptrafficanalyzer/graphicspickimageviewcanvas/MainActivity.java
10. Screenshots of the application
Figure 6 : Pick up an image via an external application
Figure 7 : Showing an image with Exif orientation applied
11. Download Source Code
I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
Ready to test your knowledge in Android? Take this quiz :
3 Responses to Android – Correcting jpeg image orientation in View Canvas with ExifInterface orientation tag
may i get author e-mail
I have some problem about display kml on android for ask you
Источник
Exif Interface Class
Definition
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
This is a class for reading and writing Exif tags in various image file formats.
Remarks
Portions of this page are modifications based on work created andВ shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Constructors
Reads Exif tags from the specified image file.
Reads Exif tags from the specified image file.
A constructor used when creating managed representations of JNI objects; called by the runtime.
Reads Exif tags from the specified image file.
Reads Exif tags from the specified image file.
Reads Exif tags from the specified image file.
Fields
Constant used to indicate that the input stream contains only Exif data.
Constant used to indicate that the input stream contains the full image data.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
The altitude (in meters) based on the reference in TAG_GPS_ALTITUDE_REF.
0 if the altitude is above sea level.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
A tag used to record the offset from UTC (the time difference from Universal Time Coordinated including daylight saving time) of the time of DateTime tag.
A tag used to record the offset from UTC (the time difference from Universal Time Coordinated including daylight saving time) of the time of DateTimeDigitized tag.
A tag used to record the offset from UTC (the time difference from Universal Time Coordinated including daylight saving time) of the time of DateTimeOriginal tag.
Type is undefined.
Type is rational.
Type is rational.
Type is undefined.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Type is rational.
Properties
Returns the runtime class of this Object .
(Inherited from Object)
Returns parsed #TAG_DATETIME value, or -1 if unavailable or invalid.
Returns parsed #TAG_DATETIME_DIGITIZED value, or -1 if unavailable or invalid.
Returns parsed #TAG_DATETIME_ORIGINAL value, or -1 if unavailable or invalid.
Returns number of milliseconds since Jan.
The handle to the underlying Android instance.
(Inherited from Object)
Returns true if the image file has a thumbnail.
Returns true if thumbnail image is JPEG Compressed, or false if either thumbnail image does not exist or thumbnail image is uncompressed.
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
Creates and returns a Bitmap object of the thumbnail image based on the byte array and the thumbnail compression value, or null if the compression type is unsupported.
Methods
Creates and returns a copy of this object.
(Inherited from Object)
Indicates whether some other object is «equal to» this one.
(Inherited from Object)
Return the altitude in meters.
Returns the value of the specified tag or null if there is no such tag in the image file.
Returns the raw bytes for the value of the requested tag inside the image file, or null if the tag is not contained.
Returns the double value of the tag that is specified as rational or contains a double-formatted value.
Returns the integer value of the specified tag.
Returns the offset and length of the requested tag inside the image file, or null if the tag is not contained.
Returns a hash code value for the object.
(Inherited from Object)
Stores the latitude and longitude value in a float array.
Returns the JPEG compressed thumbnail inside the image file, or null if there is no JPEG compressed thumbnail.
Returns the thumbnail bytes inside the image file, regardless of the compression type of the thumbnail image.
Returns the offset and length of thumbnail inside the image file, or null if either there is no thumbnail or the thumbnail bytes are stored non-consecutively.
Returns true if the image file has the given attribute defined.
Returns whether ExifInterface currently supports reading data from the specified mime type or not.
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
(Inherited from Object)
Wakes up a single thread that is waiting on this object’s monitor.
(Inherited from Object)
Wakes up all threads that are waiting on this object’s monitor.
(Inherited from Object)
Save the tag data into the original image file.
Set the value of the specified tag.
Sets the Handle property.
(Inherited from Object)
Returns a string representation of the object.
(Inherited from Object)
Causes the current thread to wait until another thread invokes the java.lang.Object#notify() method or the java.lang.Object#notifyAll() method for this object.
(Inherited from Object)
Causes the current thread to wait until another thread invokes the java.lang.Object#notify() method or the java.lang.Object#notifyAll() method for this object.
(Inherited from Object)
Causes the current thread to wait until another thread invokes the java.lang.Object#notify() method or the java.lang.Object#notifyAll() method for this object.
(Inherited from Object)
Explicit Interface Implementations
IJavaPeerable.Disposed() | (Inherited from Object) |
IJavaPeerable.DisposeUnlessReferenced() | (Inherited from Object) |
IJavaPeerable.Finalized() | (Inherited from Object) |
IJavaPeerable.JniManagedPeerState | (Inherited from Object) |
IJavaPeerable.SetJniIdentityHashCode(Int32) | (Inherited from Object) |
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) | (Inherited from Object) |
IJavaPeerable.SetPeerReference(JniObjectReference) | (Inherited from Object) |
Extension Methods
Performs an Android runtime-checked type conversion.
Источник