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
- Private void capture (){
- // Set rotation.
- Mparameters. setrotation (mlastorientation );
- ....................
- .....................
- Mcameradevice. setparameters (mparameters );
- Mcameradevice. takepicture (mshuttercallback, mrawpicturecallback, mpostviewpicturecallback, new jpegpicturecallback (LOC ));
- }
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
- Private final class implements picturecallback {
- Public void onpicturetaken (final byte [] upload data, final Android. Hardware. Camera camera ){
- // 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.
- Mimagecapture. storeimage (metric data, camera, mlocation );
- }
- }
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
- Public static int getexiforientation (string filepath ){
- Int degree = 0;
- Exifinterface EXIF = NULL;
- Try {
- EXIF = new exifinterface (filepath );
- } Catch (ioexception ex ){
- Log. E (TAG, "cannot read EXIF", ex );
- }
- If (EXIF! = NULL ){
- Int orientation = EXIF. getattributeint (
- Exifinterface. tag_orientation,-1 );
- If (orientation! =-1 ){
- // We only recognize a subset of orientation tag values.
- Switch (orientation ){
- Case exifinterface. orientation_rotate_90:
- Degree = 90;
- Break;
- Case exifinterface. orientation_rotate_180:
- Degree = 180;
- Break;
- Case exifinterface. orientation_rotate_270:
- Degree = 270;
- Break;
- }
- }
- }
- Return degree;
- }
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
- Else if (orientation = 0 & exif_orientation! = 0 ){
- Switch (exif_orientation ){
- Case 90:
- Orientation = exifinterface. orientation_rotate_90;
- Degree = 90;
- Break;
- Case 180:
- Orientation = exifinterface. orientation_rotate_180;
- Degree = 180;
- Break;
- Case 270:
- Orientation = exifinterface. orientation_rotate_270;
- Degree = 270;
- Break;
- }
- EXIF. setattribute (exifinterface. tag_orientation, integer. tostring (orientation ));
- Try {
- EXIF. saveattributes ();
- } Catch (ioexception e ){
- Log. E (TAG, "cannot save EXIF", e );
- }
- }
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.
Conclusion
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.