首页 > 编程语言 >C#实现根据图片的EXIF自动调整图片方向

C#实现根据图片的EXIF自动调整图片方向

时间:2022-12-15 17:45:29浏览次数:40  
标签:case EXIF 0th C# image RotateFlipType break visual 图片

一、什么是 EXIF

Exif是英文Exchangeable Image File(可交换图像文件)的缩写,最初由日本电子工业发展协会(JEIDA --Japan Electronic Industry Development Association) 制订,目前的最新版本是发表于2002年04月的2.21 版。国际标准化组织(ISO)正在制订的相机文件设计标准(DCF -- Design role for Camera File system)可能以Exif2.1为基础。

所有的JPEG文件以字符串“0xFFD8”开头,并以字符串“0xFFD9”结束。文件头中有一系列“0xFF??”格式的字符串,称为“标识”,用来标记JPEG文件的信息段。“0xFFD8”表示图像信息开始,“0xFFD9”表示图像信息结束,这两个标识后面没有信息,而其它标识紧跟一些信息字符。

0xFFE0 -- 0xFFEF之间的标识符称为“应用标记”,没有被常规JPEG文件利用,Exif正是利用这些信息串记录拍摄信息如快门速度、光圈值等,甚至可以包括全球定位信息。其中拍摄方向的ID为“0x0112”,有1至8共8种值。
在这里插入图片描述

二、EXIF Orientation

Orientation
The image orientation viewed in terms of rows and columns.
按行和列查看的图像方向

Tag = 274 (112.H)

Type = SHORT
Count = 1
Default = 1

1 = The 0th row is at the visual top of the image, and the 0th column is the visual left-hand side.
2 = The 0th row is at the visual top of the image, and the 0th column is the visual right-hand side.
3 = The 0th row is at the visual bottom of the image, and the 0th column is the visual right-hand side.
4 = The 0th row is at the visual bottom of the image, and the 0th column is the visual left-hand side.
5 = The 0th row is the visual left-hand side of the image, and the 0th column is the visual top.
6 = The 0th row is the visual right-hand side of the image, and the 0th column is the visual top.
7 = The 0th row is the visual right-hand side of the image, and the 0th column is the visual bottom.
8 = The 0th row is the visual left-hand side of the image, and the 0th column is the visual bottom.
Other = reserved

三、使用C#旋转图片

  1.   public static void rotating(Bitmap img, ref int width, ref int height, int orien)
  2.   {
  3.   int ow = width;
  4.   switch(orien)
  5.   {
  6.   case 2:
  7.   img.RotateFlip(RotateFlipType.RotateNoneFlipX); //horizontal flip
  8.   break;
  9.   case 3:
  10.   img.RotateFlip(RotateFlipType.Rotate180FlipNone); //right-top
  11.   break;
  12.   case 4:
  13.   img.RotateFlip(RotateFlipType.RotateNoneFlipY); //vertical flip
  14.   break;
  15.   case 5:
  16.   img.RotateFlip(RotateFlipType.Rotate90FlipX);
  17.   break;
  18.   case 6:
  19.   img.RotateFlip(RotateFlipType.Rotate90FlipNone); //right-top
  20.   width = height;
  21.   height = ow;
  22.   break;
  23.   case 7:
  24.   img.RotateFlip(RotateFlipType.Rotate270FlipX);
  25.   break;
  26.   case 8:
  27.   img.RotateFlip(RotateFlipType.Rotate270FlipNone); //left-bottom
  28.   width = height;
  29.   height = ow;
  30.   break;
  31.   default:
  32.   break;
  33.   }
  34.   }

.Net Core 通过Exif处理手机图片旋转问题

获取图片 exif 的 orientation 信息核心代码,然后根据 orientation 进行图片不同方向的旋转

效果展示

在这里插入图片描述

核心代码

  1.   private Image OrientationImage(Image image)
  2.   {
  3.   if (Array.IndexOf(image.PropertyIdList, 274) > -1)
  4.   {
  5.   var orientation = (int)image.GetPropertyItem(274).Value[0];
  6.   switch (orientation)
  7.   {
  8.   case 1:
  9.   // No rotation required.
  10.   break;
  11.   case 2:
  12.   image.RotateFlip(RotateFlipType.RotateNoneFlipX);
  13.   break;
  14.   case 3:
  15.   image.RotateFlip(RotateFlipType.Rotate180FlipNone);
  16.   break;
  17.   case 4:
  18.   image.RotateFlip(RotateFlipType.Rotate180FlipX);
  19.   break;
  20.   case 5:
  21.   image.RotateFlip(RotateFlipType.Rotate90FlipX);
  22.   break;
  23.   case 6:
  24.   image.RotateFlip(RotateFlipType.Rotate90FlipNone);
  25.   break;
  26.   case 7:
  27.   image.RotateFlip(RotateFlipType.Rotate270FlipX);
  28.   break;
  29.   case 8:
  30.   image.RotateFlip(RotateFlipType.Rotate270FlipNone);
  31.   break;
  32.   }
  33.   image.RemovePropertyItem(274);
  34.    
  35.   }
  36.   return image;
  37.   }

文件转为image类型

上传的图片文件(file(IFormFile) 或者 base64(string) 需转为 image,然后直接调用上面方法,输出新的 image
保存(save方法)即可

file 的话

 Image img = Bitmap.FromStream(file.OpenReadStream()); 

base64 的话

  1.   string base64string = HttpUtility.UrlDecode(base64);
  2.   base64string = base64string.Replace(" ", "+").Split(',')[1];
  3.   byte[] bt = Convert.FromBase64String(base64string);
  4.   MemoryStream ms = new MemoryStream(bt);
  5.  

https://blog.csdn.net/net_xxl519/article/details/119355114

标签:case,EXIF,0th,C#,image,RotateFlipType,break,visual,图片
From: https://www.cnblogs.com/raincedar/p/16985676.html

相关文章

  • 图片
    importurllib3importos#PIL图像处理标准库fromPILimportImagefromioimportBytesIOhttp=urllib3.PoolManager()response=http.request('GET','f.hiphoto......
  • 15.Oracle事物
    简介oracle事物主要用于保持oracle数据库的数据一致性,oracle事物可以看成一个SQL块的整体,只要某一个SQL语句错误,那么事物中的SQL被看做一个整体,一起回滚,要么就是一起成功全......
  • 二、memcache 启动参数详解
    memcache启动参数:(以最新1.6.17为例)memcache--help命令提示点击查看代码memcached1.6.17-p,--port=<num>小写p,memcached监听的tcp端口。(默认端口为11211)......
  • Java:SpringBoot使用EasyExcel实现Excel文件的导出下载和上传导入功能
    SpringBoot使用EasyExcel实现Excel文件的导出下载和上传导入功能文件目录$tree-Itarget.├──README.md├──pom.xml└──src└──main├─......
  • VUE使用axios数据请求时报错 TypeError Cannot set property 'xxxx' of undefined 的
    正常情况下在data里面都有做了定义data(){list:"haha"}在函数里面进行赋值this.list=response.data.result这时候你运行时会发现,数据可以请求到,但是会报错TypeErr......
  • shell_product信息
    HWASK-H:/$getprop|grepproduct[hw_mc.settings.product_cust_date]:1603075625[ro.boot.product.hardware.sku]:ASKH-AL00[ro.comp.hl.product_base_version]:......
  • 深度学习入门No module named 'common'问题
    这⾥的“common”模块是源代码中作者⾃⼰写的,将下载的源⽂件夹“【源代码】深度学习⼊门:基于Python的理论与实现”改名为book_code,并且将sys.path.append(os.pardir)#......
  • 基于云原生的集群自愈系统 Flink Cluster Inspector
    作者:舟柒、楼台1.业务背景与挑战1.1实时计算集群现状关于热点机器处理一直是阿里云Flink集群运维的一大痛点,不管在日常还是大促都已经是比较严重的问题,同时这也是分布......
  • SAP ABAP CDS view 里 INNER JOIN 和 Association 的区别
    最近有朋友在我的[知识星球]里向我提问,SAPABAPCDSview的INNERJOIN和Association的功能可以理解为一样吗?本文就来聊一聊这个话题。既然CDSview里同时支持了INN......
  • 安装vue-element-admin启动报错error:0308010C:digital envelope routines::unsupport
    这个,一般都是,node.js的版本匹配问题造成的。后面我通过安装nvm,在nvm下重新安装了一个低版本的Node.js才搞定。1、安装nvm管理工具(先关掉360等软件,不然会弹出警告!)从官网......