android 图像 exif 阅读器 3rd party api

发布于 2024-08-27 02:39:44 字数 63 浏览 5 评论 0原文

android 是否有任何第三部分 api 从图像中读取 exif 标签,支持从 1.5 开始的 api 级别。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北凤男飞 2024-09-03 02:39:44

Drew Noakes 的元数据提取库非常适合在早期 Android 平台版本上提取 EXIF 标签, 稍作修改。我在 Android 1.6 上使用它从​​ JPEG 图像中提取标签。

注意:新版本的metadata-extractor无需修改即可直接在Android上运行。

您需要下载并自行构建源代码,然后打包与您的应用程序一起使用。 (我使用的是版本 2.3.1。)对 com.drew.imaging.jpeg.JpegMetadataReader 进行以下更改:

  • 删除以下导入语句:

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

  • 删除以下方法(在 Android 上不需要):

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

删除 com.drew.metadata.SampleUsage 类,该类引用了上面删除的方法。同时删除所有测试包。

这就是全部内容。以下是使用 JpegMetadataReader 从存储在 SD 卡上的 JPEG 图像中提取日期时间标签的示例:

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;
}

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;
}
半仙 2024-09-03 02:39:44

对于它的价值,您是否尝试使用本机 ExifInterface 类?

http://developer.android.com/reference/android/media/ExifInterface。 html

应该比使用第三方库更快;)

For what it worth, did you try to use the native ExifInterface class ?

http://developer.android.com/reference/android/media/ExifInterface.html

Should be must faster than using a 3rd party library ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文