Metadata-extractor use of EXIF metadata to read pictures in Android

Source: Internet
Author: User
Tags color gamut


I. Introduction and INTRODUCTION


Recently in the development of the use of Metadata-extractor-xxx.jar and Xmpcore-xxx.jar this thing, simply consult a large number of articles to learn, to share.

The work itself is often dealing with large pictures, groping and groping is the more the more.

First introduce what is EXIF. EXIF is an abbreviation for exchangeable image File , which is a format specifically set for digital camera photos. This format can be used to record the properties of a digital photo, such as the camera's brand and model, the time of the photograph taken, the aperture size set at the time of shooting, the shutter speed, and the ISO information. In addition, it can record the shooting data, as well as the format of the image. This enables output to a peripheral that is compatible with the EXIF format. such as photo printers.


The most common image format that supports EXIF information at the moment is JPG. Very many image tools are able to directly display the EXIF information of the picture, including some of today's well-known albums site also provides the EXIF information that the page uses to display the photo. This article describes how Java reads the EXIF information of an image, including how to adjust the image based on EXIF information to fit the user's browsing.

When using the BufferedImage class to read. Too big picture often throws outofmemoryexception abnormal, very egg ache.

BufferedImage image = Imageio.read (file file);


The most simple and easy-to-use EXIF processing Java package is the metadata-extractorwritten by Drew Noakes .

This is a simple Java library that can read meta data (Exif, IPTC, XMP, ICC, etc.) from an image file, using simple:

Metadata Metadata = Imagemetadatareader.readmetadata (ImagePath);

The library can understand metadata in multiple formats, many of which can exist in a single image:
Exif, IPTC, XMP, JFIF/JFXX, ICC Profiles, Photoshop fields, PNG properties, BMP properties, GIF properties

It can handle types of files:JPEG, TIFF, PSD, PNG, BMP, GIF, Camera Raw (nef/cr2/orf/arw/rw2/...)


Note:Not every JPG image file includes EXIF information that you can click after Windows Explorer has clicked the selected picture. Assuming that the picture includes EXIF information, the model of the camera is displayed in the form's status bar.




ii. Demo Sample code and descriptive narrative

Below we give some code that will contain EXIF image information all printed out.

Demo Example 1):

import java.io.File;         Import Java.util.Iterator;    Import Com.drew.imaging.jpeg.JpegMetadataReader;    Import Com.drew.metadata.Directory;    Import Com.drew.metadata.Metadata;    Import Com.drew.metadata.Tag;         Import Com.drew.metadata.exif.ExifDirectory; /** * Read EXIF information for pictures */public class Exiftest {public static void main (string[] args) throws Exce ption {//Includes the image address of EXIF information file Jpegfile = new file ("D:\\xxxx\\xxxx\\xxxx.                          JPG ");                          Metadata Metadata = Jpegmetadatareader.readmetadata (jpegfile);                          Directory exif = Metadata.getdirectory (Exifdirectory.class);                          Iterator tags = exif.gettagiterator ();                                  while (Tags.hasnext ()) {tag tag = (tag) tags.next ();                              SYSTEM.OUT.PRINTLN (tag); }         }    }


Demo Example 2:)

public static void Main (string[] args) throws Exception {file Mfile = new File ("F:/xxx.        JPG ");        Metadata Metadata = Imagemetadatareader.readmetadata (mfile); For (Directory directory:metadata.getDirectories ()) {if ("Exifsubifddirectory". Equalsignorecase (directory.ge Tclass (). Getsimplename ())) {//aperture f value = lens focal length/lens aperture diameter System.out.println ("Aperture value: f/" + DIRECTORY.G                Etstring (Exifsubifddirectory.tag_fnumber));                SYSTEM.OUT.PRINTLN ("Exposure time:" + directory.getstring (exifsubifddirectory.tag_exposure_time) + "SEC");                System.out.println ("ISO speed:" + directory.getstring (exifsubifddirectory.tag_iso_equivalent));                SYSTEM.OUT.PRINTLN ("focal length:" + directory.getstring (exifsubifddirectory.tag_focal_length) + "mm");                System.out.println ("Photo time:" + directory.getstring (exifsubifddirectory.tag_datetime_original)); System.out.println ("width:" + directory.getstring (exifsubifddirectory.tag_exif_image_width));            System.out.println ("Gao:" + directory.getstring (exifsubifddirectory.tag_exif_image_height)); } if ("Exififd0directory". Equalsignorecase (Directory.getclass (). Getsimplename ())) {SYSTEM.OUT.P                Rintln ("Camera Manufacturer:" + directory.getstring (exififd0directory.tag_make));                SYSTEM.OUT.PRINTLN ("Camera model:" + directory.getstring (Exififd0directory.tag_model));                SYSTEM.OUT.PRINTLN ("Horizontal resolution:" + directory.getstring (exififd0directory.tag_x_resolution));            SYSTEM.OUT.PRINTLN ("Vertical resolution:" + directory.getstring (exififd0directory.tag_y_resolution)); }        }    }


Demo Example 3):

File mfilepath= "C://xxx.jpg";    Metadata Metadata = Com.drew.imaging.jpeg.JpegMetadataReader.readMetadata (Mfilepath);    jpegdirectory JD = (jpegdirectory) metadata.getdirectory (jpegdirectory.class);    System.out.println ("------------" + jd.getimageheight ()); High System.out.println of the picture    ("------------" + jd.getimagewidth ());  The width of the picture        //Because it is only the header information of the image, so no matter how large the picture can be read, and very fast.


You can see the exact shooting time of the photo from the running. Camera model, exposure time, aperture value for shooting. Focal length, ISO value and so on.


You can also directly specify the value of the random number of readings. Very many integer constants that begin with Tag_ are defined in the Exifdirectory class. These constants represent a specific number of parameters, such as the model of the camera to be read, and can be obtained using the following code.


Metadata Metadata = Jpegmetadatareader.readmetadata (jpegfile);        Directory exif = Metadata.getdirectory (exifdirectory.class);        String model = exif.getstring (Exifdirectory.tag_model);


The above mentioned is how to obtain the EXIF information of the photos, including a very important message is the direction of shooting.

For example, the image used for shooting direction is: Orientation-top, left side (Horizontal/normal). When we take pictures, we often choose the direction of the camera according to the different scenes, such as shooting a tall tree. We'll shoot the camera vertically to make the scene fit the entire viewfinder. However, the resulting picture assumes that the ordinary picture browser is inverted, you need to adjust the angle ability to get a normal image.


By reading the EXIF information of the picture, we can get a result about the direction of shooting:Orientation-left side, Bottom (Rotate CW).

The value of the direct read exitdirectory.tag_orientation tag is 8.

to see how this project defines these return values, open the source code package. Exifdescriptor Class of getorientationdescription () , the method code such as the following:

public String getorientationdescription () throws metadataexception{if (!_dir                Ectory.containstag (exifdirectory.tag_orientation)) return null;                int orientation = _directory.getint (exifdirectory.tag_orientation);            Switch (orientation) {case 1:return ' Top, left side (Horizontal/normal) ";            Case 2:return "Top, right side (Mirror horizontal)";            Case 3:return "Bottom, right side (Rotate 180)";            Case 4:return "Bottom, left side (Mirror vertical)";            Case 5:return "left side, top (Mirror horizontal and rotate CW)";            Case 6:return "right side, Top (Rotate-CW)";            Case 7:return "right side, bottom (Mirror horizontal and rotate-CW)";            Case 8:return "left side, Bottom (Rotate CW)";        Default:return string.valueof (orientation); }    }


The meaning of each return value can be clearly seen in this way, so we can rotate or mirror the image based on the actual return value.


The following gives the code to rotate the picture, other images about the image processing readers can and so on:

string MPath = "d:\\xxx.        JPG ";    File img = new file (MPath);      BufferedImage old_img = (bufferedimage) imageio.read (IMG);    int w = old_img.getwidth ();         int h = old_img.getheight ();          BufferedImage new_img = new BufferedImage (H,W,BUFFEREDIMAGE.TYPE_INT_BGR);              Graphics2D g2d =new_img.creategraphics ();    AffineTransform origxform = G2d.gettransform ();    AffineTransform newxform = (affinetransform) (Origxform.clone ());    Center of rotation is center of the panel double xrot = w/2.0; Newxform.rotate (Math.toradians (270.0), Xrot, Xrot);     Rotate 270 degrees G2d.settransform (newxform);    Draw image centered in panel g2d.drawimage (old_img, 0, 0, NULL);        Reset to Original G2d.settransform (origxform);    Write to the new file FileOutputStream out = FileOutputStream ("d:\\xxx2.jpg");    try{Imageio.write (new_img, "JPG", out);    }finally{Out.close (); }


Note: after using the above code to rotate the photo. The EXIF information included in the original photo does not exist.

This issue requires that the EXIF information be read before the photo is rotated. Then after the rotation to write new photos, can use the mediautil package to write EXIF information to the image file, about the use of this package can refer to the final link.

The mirror flip of the photo can be achieved directly using the graphic2d DrawImage method:


Public abstract Boolean DrawImage (Image img,            int dx1,int dy1,            int dx2,int dy2,            int sx1,int sy1,            int SX2, int SY2,            imageobserver observer);


Iii. Additional Information


Explain the actual meaning of some of the parameters:

make producer refers to the product manufacturer

Model model refers to the type of equipment

Orientation Direction Some camera support, and some do not support

× resolution/y Resolutionx/y direction Resolution This column already has a specific article explaining this issue

Resolutionunit Resolution units are typically PPI

Software software display firmware firmware version number

DateTime Date and time

Ycbcrpositioning Hue positioning

Exifoffsetexif Information location. Defines the writing of Exif in a file. Some software is not displayed.

Exposuretime exposure time is the shutter speed

Fnumber aperture coefficient

ISO Speed ratings sensitivity

Exifversionexif Version number

Datetimeoriginal creation time

datetimedigitized Digital Time

Componentsconfiguration Image Construction (multi-finger color combination scheme)

Compressedbitsperpixel (BPP) compression per pixel color position refers to the degree of compression

Exposurebiasvalue exposure compensation.

Maxaperturevalue Maximum aperture

Meteringmode measured light, the average measurement of light, the central measurement of light, light and so on.

LightSource Light source refers to the white balance setting

Whether Flash uses Flash.

FocalLength focal length. Generally displays the lens physical focal length. Some software can define a factor. Displays the 35mm camera's focal length Makernote (User Comment) Author's tag, description, record

Flashpixversionflashpix Version number (individual models supported)

ColorSpace color gamut, color space

Exifimagewidth (Pixel X Dimension) image width refers to the number of transverse pixels

Exifimagelength (Pixel Y Dimension) image height refers to the number of longitudinal pixels


iv. References and resources download

The latest version number of the project and its source code download:

GitHub Home:https://github.com/drewnoakes/metadata-extractor

http://www.drewnoakes.com/code/exif/

Mediautil http://mediachest.sourceforge.net/mediautil/

Metadata-extractor use of EXIF metadata to read pictures in Android

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.