Android: Manipulating picture Exif information

Source: Internet
Author: User
Tags constant gettext trim

What is EXIF

First to understand what is EXIF. EXIF is an image file format, its data stored in the JPEG format is exactly the same, in fact, the EXIF format is the JPEG format header inserted in the digital photos of information, including shooting aperture, shutter, balance white, ISO, focal length, date and time and so on various and shooting conditions and camera brand, model, Color coding and GPS and so on. In simple terms, exif= takes the parameters +jped. Therefore, you can use any view of the JPEG file to browse the image of the EXIF information, but not all graphics programs can handle EXIF information, and since the Android2.0, added to the image EXIF data support.

Exifinterface

Under Android, the Exifinterface class operates on the EXIF information of the image, although the name of the class contains interface, but it is not an interface, it is a class, under the "Android.media.ExifInterface" package, is the realization of a part of the Media Library's functionality. Exifinterface has a constructor that accepts a string type of data, which is the address of the picture file to read.

EXIF data can be understood in the picture as a way to store Key-value key-value pairs, typically by following several methods:

String getattribute (String tag): Gets the strings value in the picture in which the property is tag.

Double getattribute (String tag,double defaultvalue): Gets the double value in the picture in which the property is tag.

int Getattributeint (String tag,defaultvalue): Gets the int value in the picture in which the property is tag.

void setattribute (String tag,string value): Sets the value of the picture EXIF according to the input parameters.

void Saveattrubutes (): The Exif of an in-memory picture is written to the picture.

As you can see, most of the above methods manipulate a string tag parameter, which is an EXIF property, in Exifinterface defines some string static constants that represent these tag values, commonly used as follows:

Tag_aperture: Aperture value.

Tag_datetime: Time to shoot depends on when the device is set up.

Tag_exposure_time: Exposure time.

Tag_flash: Flash.

Tag_focal_length: Focal length.

Tag_image_length: Picture height.

Tag_image_width: Picture width.

Tag_iso:iso.

Tag_make: Equipment brand.

Tag_model: Device model, plastic, in the exifinterface has a constant corresponding representation.

Tag_orientation: The rotation angle, the shaping representation, has the constant correspondence expression in the exifinterface.

The above constants do not include GPS information, in fact, Exif can also save the camera when the GPS information, but need device support. The following is a demo to explain the parameters of the acquisition and value of the display:

The code is as follows:

1 Btn_readexifinlog.setonclicklistener (new View.onclicklistener () {

2

3 @Override

4 public void OnClick (View v) {

5 try {

6 Exifinterface exifinterface = new Exifinterface (

7 "/sdcard/a.jpg");

8 String Ffnumber = Exifinterface

9. getattribute (Exifinterface.tag_aperture);

Ten String fdatetime = Exifinterface

GetAttribute (Exifinterface.tag_datetime);

A String fexposuretime = Exifinterface

GetAttribute (Exifinterface.tag_exposure_time);

String Fflash = Exifinterface

GetAttribute (Exifinterface.tag_flash);

String ffocallength = Exifinterface

GetAttribute (exifinterface.tag_focal_length);

A String fimagelength = Exifinterface

GetAttribute (exifinterface.tag_image_length);

String fimagewidth = Exifinterface

GetAttribute (Exifinterface.tag_image_width);

String fisospeedratings = Exifinterface

GetAttribute (Exifinterface.tag_iso);

String Fmake = Exifinterface

GetAttribute (Exifinterface.tag_make);

-String Fmodel = Exifinterface

GetAttribute (Exifinterface.tag_model);

String forientation = Exifinterface

GetAttribute (exifinterface.tag_orientation);

String fwhitebalance = Exifinterface

GetAttribute (exifinterface.tag_white_balance);

32

LOG.I (TAG, "Ffnumber:" + ffnumber);

LOG.I (TAG, "fdatetime:" + fdatetime);

LOG.I (TAG, "fexposuretime:" + fexposuretime);

LOG.I (TAG, "Fflash:" + Fflash);

Panax Notoginseng log.i (TAG, "ffocallength:" + ffocallength);

LOG.I (TAG, "fimagelength:" + fimagelength);

LOG.I (TAG, "fimagewidth:" + fimagewidth);

LOG.I (TAG, "fisospeedratings:" + fisospeedratings);

LOG.I (TAG, "Fmake:" + fmake);

LOG.I (TAG, "Fmodel:" + Fmodel);

LOG.I (TAG, "forientation:" + forientation);

LOG.I (TAG, "fwhitebalance:" + fwhitebalance);

\ catch (Exception e) {

/TODO auto-generated Catch block

E.printstacktrace ();

48}

49}

50});

Get Data:

Operation EXIF

Above mentioned, get and set the EXIF information of the picture, use the method in the Exifinterface, above already enumerated, the main is to specify the storage by tag.

Here's a description, EXIF information in the picture in the form of binary storage, the number of data stored in each field is fixed, and the number of tags is fixed, so we can only manipulate the image EXIF information already exists in the value of the tag, and the saved data to be stored in accordance with the number of digits of the limit, If the stored data type is incorrect, the stored data may not be removed correctly, and the number of bytes exceeded will be intercepted. If you cannot store data for a string in tag_orientation, it must store a value of type int, and the extra will be intercepted.

It is also important to note that the Saveattributes () method is mainly used to save all the current EXIF information in memory to the target image, according to the official document, it is inefficient, it will take all the EXIF information of the picture, and then save it to the target picture again, Therefore, it is recommended to use the SetAttribute () method to set EXIF information. However, in the practical application, if only use SetAttribute () Set EXIF information, will not be written to the target picture, only after the change EXIF information, call Saveattribute () can write new Exif to the target picture. This process is less efficient, the simulator will be a bit, but the real machine test is not the case, the reaction is very fast.

The following is a simple demo to illustrate the preservation of EXIF in read:

1%20btn_saveexif.setonclicklistener (New%20view.onclicklistener ()%20{

2

3%20@override

4%20public%20void%20onclick (view%20v)%20{

5%20try%20{

6%20//%20tag

7%20string%20strattr%20=%20et_attr.gettext (). toString (). Trim ();

8%20//%20tag-value

9%20string%20strvalue%20=%20et_value.gettext (). toString (). Trim ();

10

11%20if%20 (Textutils.isempty (strattr)

12%20| | %20textutils.isempty (strvalue))%20{

13%20toast.maketext (mainactivity.this,%20 "Please fill in the attribute and value",

14%20toast.length_short). Show ();

15%20return;

16%20}

17%20//%20 Get Pictures EXIF

18%20exifinterface%20exif%20=%20new%20exifinterface ("/sdcard/a.jpg");

19//Save the value of the specified tag

Exif.setattribute (Strattr,strvalue);

21///write EXIF information to target picture

Exif.saveattributes ();

Toast.maketext (Mainactivity.this, "Exif Information saved successfully",

Toast.length_short). Show ();

A catch (Exception e) {

E.printstacktrace ();

27}

28}

29});

Btn_readexif.setonclicklistener (New View.onclicklistener () {

31

@Override

The public void OnClick (View v) {

The try {

//Tag

The String strattr = Et_attr.gettext (). toString (). Trim ();

37

if (Textutils.isempty (strattr)) {

Toast.maketext (mainactivity.this, "Please fill in the Attributes",

Toast.length_short). Show ();

The return;

42}

43

44//Get Pictures EXIF

Exifinterface exif = new Exifinterface ("/sdcard/a.jpg");

46//Get the property value of the specified tag

The String strvalue = Exif.getattribute (strattr);

if (! Textutils.isempty (strvalue)) {

Toast.maketext (mainactivity.this, strattr+ "=" +strvalue,

Toast.length_short). Show ();

The other {

Toast.maketext (Mainactivity.this, "There is no information in the image EXIF that has a property value of" +strattr+ ",

Toast.length_short). Show ();

54}

+} catch (Exception e) {

E.printstacktrace ();

57}

58}

59});

Effect display, first read make information, then write make information and re-read:

Note that in the example above, if you write any value, you will be prompted to save successfully, but it is not written to the EXIF information of the target picture.

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.