• c#使用ExifLib库提取图像的相机型号、光圈、快门、iso、曝光时间、焦距信息等EXIF信息


    近期公司组织了书画摄影比赛,本人作为摄影爱好者,平时也会拍些照片,这次比赛当然不能错过。为了提高获奖概率,选了19张图像作为参赛作品。但是,摄影作品要提交图像的光圈、曝光时间等参数。一两张还可以通过电脑自带软件右键查看图像参数并一个个复制,但现在有19张,让我一点点复制还不如直接放弃参与了。谁让咱们还有一个身份是coder,那就现场手撸个小程序批量输出图像的EXIF信息。

    开发需求很简单,就是能手动选取一个文件夹,然后读取该路径下的所有文件(图片),然后提取每张图像的exif信息,并将结果显示到界面,然后将Ctrl + A复制所有信息即可。在开发上,语言选择c#,搭配WPF框架,并选择ExifLib这个轻量化的EXIF信息提取库获取图像参数信息,下图是最终的提取信息。
    在这里插入图片描述
    XAML主要代码

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>
            <TextBox x:Name="textBox" Grid.Row="0"/>
            <Button Content="请选择一个路径"  Click="Button_Click"  Grid.Row="1" Background="LightBlue" Margin="2,0,2,2" Padding="5" />
        </Grid>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    cs逻辑代码:

    		using ExifLib;
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                // 创建一个选择文件路径的对话框
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Multiselect = false;
                openFileDialog.CheckFileExists = false;
                openFileDialog.FileName = "Folder Selection.";
                openFileDialog.Filter = "Folders|no.files";
    
                Dictionary<string, string> dic_pathAndName = new Dictionary<string, string>();
                if (openFileDialog.ShowDialog() == true)
                {
                    string folderPath = System.IO.Path.GetDirectoryName(openFileDialog.FileName);
    
                    // 使用 DirectoryInfo 类获取该路径下的所有文件
                    DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
                    FileInfo[] fileInfos = directoryInfo.GetFiles();
    
                    // 处理获取到的文件
                    foreach (FileInfo fileInfo in fileInfos)
                    {
                        Debug.WriteLine(fileInfo.Name);
                        //dic_pathAndName[System.IO.Path.GetFileNameWithoutExtension(fileInfo.FullName)] = fileInfo.FullName;//key为没有后缀名的文件名
                        dic_pathAndName[fileInfo.Name] = fileInfo.FullName;
                    }
                }
    
                if (dic_pathAndName.Count == 0) return;
    
                foreach (var dic in dic_pathAndName)
                {
                    Debug.WriteLine($"{dic.Key}:  {getImageExifInfo(dic.Value)}");
                    textBox.AppendText($"{dic.Key}, {getImageExifInfo(dic.Value)}\n");
                }
    
    
    
            }
    
            string getImageExifInfo(string path)
            {
                if (!File.Exists(path)) return "";
    
                string res = "";
                try
                {
                    using (ExifReader reader = new ExifReader(path))
                    {
                        // 相机制造商
                        if (reader.GetTagValue(ExifTags.Make, out string make))
                            Debug.WriteLine("相机制造商: " + make);
    
                        // 相机型号
                        if (reader.GetTagValue(ExifTags.Model, out string model))
                            Debug.WriteLine("相机型号: " + model);
    
                        // 光圈
                        if (reader.GetTagValue(ExifTags.FNumber, out double fNumber))
                        {
                            var fNumberFraction = FractionFromDouble(fNumber);
                            Debug.WriteLine("光圈: F/" + fNumber);
                        }
    
                        string time = "";
                        // 快门速度
                        if (reader.GetTagValue(ExifTags.ExposureTime, out double exposureTime))
                        {
                            var exposureTimeFraction = FractionFromDouble(exposureTime);
                            time = exposureTimeFraction.Item1 + "/" + exposureTimeFraction.Item2;
                        }
    
                        // ISO
                        if (reader.GetTagValue(ExifTags.ISOSpeedRatings, out ushort isoSpeedRatings))
                            Debug.WriteLine("ISO: " + isoSpeedRatings);
    
                         曝光时间
                        //if (reader.GetTagValue(ExifTags.ExposureTime, out double exposureTime))
                        //    Debug.WriteLine("曝光时间: " + exposureTime.ToString("0.##") + " s");
    
                        // 焦距
                        if (reader.GetTagValue(ExifTags.FocalLength, out double focalLength))
                            Debug.WriteLine("焦距: " + focalLength.ToString("0.##") + " mm");
    
                        res = $"{model} f/{fNumber} {time}s ISO-{isoSpeedRatings} {focalLength}mm";
    
                    }
    
                }
                catch (Exception e)
                {
    
                }
    
    
    
                return res;
            }
    
            private Tuple<int, int> FractionFromDouble(double value, double error = 0.0001)
            {
                int denominator = 1;
                while (Math.Abs(value * denominator - Math.Round(value * denominator)) > error)
                    denominator++;
    
                return Tuple.Create((int)Math.Round(value * denominator), denominator);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108

    以上代码直接复制、拷贝走,然后可以根据自己的需求输出想要的图像参数信息,并随意定义文本格式,爽歪歪。

  • 相关阅读:
    2022年下半年软件设计师知识集锦
    使用JNPF低代码平台提高生产力
    『现学现忘』Git后悔药 — 28、版本回退git reset --soft命令说明
    win平台使用惯导模块:WHEELTEC N100 模块
    JavaScript - 好玩的打字动画
    数据结构--顺序表
    LeetCode 251:展开二维向量
    Cordova插件开发:集成南方测绘RTK实现高精度卫星定位
    猿创征文 |【C++】面向对象之微观部分——类的组成(下)
    跨越边界:虚拟直播间席卷各行业领域,引爆下一代直播风暴
  • 原文地址:https://blog.csdn.net/weixin_32155265/article/details/134027138