EXIF information processing under android2.2

Source: Internet
Author: User
Tags color gamut

1. What is EXIF
EXIF (exchangeable image file can exchange image files) is an image file format, and its data storage is exactly the same as JPEG format. In fact, the EXIF format is to insert digital photo information in the JPEG format header, including the aperture, shutter, white balance, ISO, focal length, date and time at the time of shooting and various shooting conditions, as well as the camera brand, model, color encoding, recording sound at the time of shooting, and the Global Positioning System (GPS) and thumbnails. In short, EXIF = JPEG + shooting parameters. Therefore, you can use any image viewer software that can view JPEG files to browse photos in EXIF format, but not all graphics programs can process EXIF information.

All JPEG files start with the string "0xffd8" and end with the string "0xffd9. The file header contains a series of "0xff ??" Format String, called "identifier", used to mark the information segment of a jpeg file. "0xffd8" indicates that the image information starts, and "0xffd9" indicates that the image information ends. The two identifiers do not have any information, while other identifiers follow some information characters.
0xffe0 -- the identifier between 0xffef is called "application tag" and is not used by common JPEG files. EXIF records the shooting information such as the shutter speed and aperture value by using these information strings, it can even include the full-ball positioning information. According to the definition of these identifiers in exif2.1, digital cameras can record all kinds of shooting information into digital images, and application software can read the data, and then follow the standards of exif2.1, the specific meanings of these parameters are retrieved, which generally includes the following information:
Image Description: Image Description and source. It refers to the image generation tool.
Artist author some cameras can enter users' names
Make producer refers to the product manufacturer
Model indicates the device model.
Orientation supports some cameras, and some do not.
Xresolution/yresolution x/y direction resolution this topic already has a special item to explain this problem.
Resolutionunit resolution unit is generally PPI
The software displays the firmware version.
Datetime Date and Time
Ycbcrpositioning color phase Positioning
Exifoffsetexif information location, defines the write of EXIF information in the file, some software does not show.
Exposuretime: shutter speed
Fnumber aperture Coefficient
Exposureprogram specifies the program-based Automatic exposure settings. Different cameras may have Suter priority (SHUTTER first), aperture priority (SHUTTER first), and so on.
ISO speed ratings sensitivity
Exifversionexif version
Datetimeoriginal Creation Time
Datetimedigitized digitization time
Componentsconfiguration Image Construction (Multi-finger color combination solution)
Compressedbitsperpixel (BPP) indicates the degree of compression of each pixel color bit.
Exposurebiasvalue exposure compensation.
Maxaperturevalue maximum aperture
Meteringmode: Average, central, and point.
Lightsource light source refers to white balance settings
Whether or not Flash is used.
Focallength focal length. Generally, the physical focal length of the lens is displayed. Some software can define a coefficient to display the author Mark, description, and record of the makernote (User comment) equivalent to the focal length of 35mm cameras.
Flashpixversionflashpix version (supported by some models)
Colorspace Color Gamut and Color Space
Exifimagewidth (pixel x dimension) Image Width refers to the number of horizontal dimensions
Exifimagelength (pixel y dimension) Image Height refers to the number of vertical dimensions
Interoperability IFD generic extended item definition pointer is related to the TIFF file, the specific meaning is unknown
The compression ratio of the filesource File compression.

Ii. Photo taking process in camera

EXIF-related knowledge is required during the development of the android camera program. improper processing may cause the JPEG image to be unable to be viewed normally.
The camera application in froyo (Android 2.2) Source Code does not perform write operations on EXIF information, but only read operations. Write operations on EXIF are performed on the camera hardware abstraction layer, this is the design logic of Google. However, different Android platforms and their related sub-platforms, coupled with different camera applications, may exchange and arrange and combine each other. This may happen when the base layer does not write EXIF, the upper-layer applications do not write EXIF information, so the image display information will be lost. The parameter orientation is the most influential parameter.

The froyo camera logic is as follows:
In the camera activity, there is an internal class imagecapture, which contains an important method:

Copy to clipboardJava code
  1. Private void capture (){
  2. // Set rotation.
  3. Mparameters. setrotation (mlastorientation );
  4. ....................
  5. .....................
  6. Mcameradevice. setparameters (mparameters );
  7. Mcameradevice. takepicture (mshuttercallback, mrawpicturecallback, mpostviewpicturecallback, new jpegpicturecallback (LOC ));
  8. }

The general process is as follows:
1. Add the camera direction to the camera. Parameters instance;
2. Upload All camera parameters to the Android. Hardware. Camera object;
3. Call the method takepicture and set four important callback;
4. Hal is responsible for generating EXIF data;
5. 4th callback returned data (This callback is the most important and cannot be defaulted. That is to say, setting the first three callback to null does not affect the photo taking function). See the following code:

Copy to clipboardJava code
  1. Private final class implements picturecallback {
  2. Public void onpicturetaken (final byte [] upload data, final Android. Hardware. Camera camera ){
  3. // JPEG data is generated by the Hal layer based on the parameters transmitted by the application (that is, the camera. Parameters instance) and the JPEG compression algorithm.
  4. Mimagecapture. storeimage (metric data, camera, mlocation );
  5. }
  6. }

Iii. EXIF usage and code optimization solution

Where does EXIF information be used? I have met at least the following:
1. Generate a thumbnail in the upper right corner;
2. Image Display applications, such as the gallery3d app that comes with Android;
3. image echo;
4. Applications that need to add camera attachments, such as short (color) messages.

Let's look at the source code: imagemanager reads the EXIF direction parameters in this way.

Copy to clipboardJava code
  1. Public static int getexiforientation (string filepath ){
  2. Int degree = 0;
  3. Exifinterface EXIF = NULL;
  4. Try {
  5. EXIF = new exifinterface (filepath );
  6. } Catch (ioexception ex ){
  7. Log. E (TAG, "cannot read EXIF", ex );
  8. }
  9. If (EXIF! = NULL ){
  10. Int orientation = EXIF. getattributeint (
  11. Exifinterface. tag_orientation,-1 );
  12. If (orientation! =-1 ){
  13. // We only recognize a subset of orientation tag values.
  14. Switch (orientation ){
  15. Case exifinterface. orientation_rotate_90:
  16. Degree = 90;
  17. Break;
  18. Case exifinterface. orientation_rotate_180:
  19. Degree = 180;
  20. Break;
  21. Case exifinterface. orientation_rotate_270:
  22. Degree = 270;
  23. Break;
  24. }
  25. }
  26. }
  27. Return degree;
  28. }

This method can be further optimized so that writing EXIF information is no longer dependent on the underlying layer. It is to compare whether the orientation transmitted to the underlying layer is equal to the actual returned results. If not equal, an error occurs when the underlying EXIF information is written, and we can modify it at the application layer.
You can add a judgment branch as follows: (exif_orientation is the value passed to the underlying layer by our cached application ).

Copy to clipboardJava code
  1. Else if (orientation = 0 & exif_orientation! = 0 ){
  2. Switch (exif_orientation ){
  3. Case 90:
  4. Orientation = exifinterface. orientation_rotate_90;
  5. Degree = 90;
  6. Break;
  7. Case 180:
  8. Orientation = exifinterface. orientation_rotate_180;
  9. Degree = 180;
  10. Break;
  11. Case 270:
  12. Orientation = exifinterface. orientation_rotate_270;
  13. Degree = 270;
  14. Break;
  15. }
  16. EXIF. setattribute (exifinterface. tag_orientation, integer. tostring (orientation ));
  17. Try {
  18. EXIF. saveattributes ();
  19. } Catch (ioexception e ){
  20. Log. E (TAG, "cannot save EXIF", e );
  21. }
  22. }

The EXIF operation on the application layer is completed through the Android. Media. exifinterface interface.
Public void setattribute (string tag, string value) is used to set the attribute. You can use public int getattributeint (string tag, int defaultvalue) and Public String getattribute (string tag) to obtain the attribute) both methods can be used. The second parameter of the getattributeint overload method is the default value we set. If the value is successful, the value of the corresponding tag is returned. The specific integer content is the direct return value of the method. In method 2, the method returns the result directly. If the result fails, the return value is null.

In this way, after a simple transformation (only the EXIF information validation and write logic are added), the camera application will be more robust and does not depend on whether the platform underlying layer processes EXIF information, even if an exception occurs, the application can be automatically corrected.
Here, we only use the orientation parameter in EXIF as an example. Other parameters can be used in the same way to ensure that the captured image meets the standards.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.