문제

Is there any 3rd part api for android to read exif tags from image which support api level starting from 1.5.

도움이 되었습니까?

해결책

The metadata extraction library by Drew Noakes works well for extracting EXIF tags on earlier Android platform versions, with a slight modification. I am using it on Android 1.6 to extract tags from JPEG images.

NOTE: Newer versions of metadata-extractor work directly on Android without modification.

You will need to download and build the source code yourself, and package it with your app. (I'm using release 2.3.1.) Make the following changes to com.drew.imaging.jpeg.JpegMetadataReader:

  • Remove the following import statement:

    import com.sun.image.codec.jpeg.JPEGDecodeParam;

  • Delete the following method (which you won't need on Android):

    public static Metadata readMetadata(JPEGDecodeParam decodeParam) { ... }

Remove the com.drew.metadata.SampleUsage class, which references the method deleted above. Also remove all of the test packages.

That's all there is to it. Here's an example of using the JpegMetadataReader to extract a date-time tag from a JPEG image stored on the SD card:

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifDirectory;

// other imports and class definition removed for brevity

public static Date extractExifDateTime(String imagePath)
{
    Log.d("exif", "Attempting to extract EXIF date/time from image at " + imagePath);
    Date datetime = new Date(0); // or initialize to null, if you prefer
    try
    {
        Metadata metadata = JpegMetadataReader.readMetadata(new File(imagePath));
        Directory exifDirectory = metadata.getDirectory(ExifDirectory.class);

        // these are listed in order of preference
        int[] datetimeTags = new int[] { ExifDirectory.TAG_DATETIME_ORIGINAL,
                                         ExifDirectory.TAG_DATETIME,
                                         ExifDirectory.TAG_DATETIME_DIGITIZED };
        int datetimeTag = -1;
        for (int tag : datetimeTags)
        {
            if (exifDirectory.containsTag(tag))
            {
                datetimeTag = tag;
                break;
            }
        }

        if (datetimeTag != -1)
        {
            Log.d("exif", "Using tag " + exifDirectory.getTagName(datetimeTag) + " for timestamp");

            SimpleDateFormat exifDatetimeFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
            datetime = exifDatetimeFormat.parse(exifDirectory.getString(datetimeTag));
        }
        else
        {
            Log.d("exif", "No date/time tags were found");
        }
    }
    catch (Exception e)
    {
        Log.w("exif", "Unable to extract EXIF metadata from image at " + imagePath, e);
    }
    return datetime;
}

다른 팁

자원을 JAR 자체에 포함 시키고이 API를 사용하여 파일을 읽으 려합니다.

getresourceasstream

먼저 자원이 JAR에 있는지 (열 수 있고 그곳에 있음)이 있는지 확인해야하며, 두 개가 IO 메커니즘이 아닌 파일 및 파일이 아닌 API 호출을 사용하는지 확인해야합니다 (이미지항아리에 있으십시오. 파일 시스템에 없음).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top