版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/benben_2015/article/details/89331455
在图片自适应旋转显示一文中,了解到相机拍摄的图片可能在显示的时候出现旋转问题,并且文中讲解了一个图片属性orientation
。它用来标记摄像机相对于捕获场景的方向,一些图片显示设备就是依赖它进行旋转控制。
因此对于软件来说,要想实现图片的旋转,首先需要获取这一标记值。下面是利用exif
包进行的orientation
标记读取:
package main
import (
"fmt"
"github.com/rwcarlsen/goexif/exif"
"os"
)
func ReadOrientation(filename string) int {
file, err := os.Open(filename)
if err != nil {
fmt.Println("failed to open file, err: ", err)
return 0
}
defer file.Close()
x, err := exif.Decode(file)
if err != nil {
fmt.Println("failed to decode file, err: ", err)
return 0
}
orientation, err := x.Get(exif.Orientation)
if err != nil {
fmt.Println("failed to get orientation, err: ", err)
return 0
}
orientVal, err := orientation.Int(0)
if err != nil {
fmt.Println("failed to convert type of orientation, err: ", err)
return 0
}
fmt.Println("the value of photo orientation is :", orientVal)
return orientVal
}
func main() {
ReadOrientation("test.jpg")
}
ReadOrientation
方法会获取orientation
的值,总共有0~8这几种情况。分别表示:
orientationUnspecified = 0
orientationNormal = 1
orientationFlipH = 2
orientationRotate180 = 3
orientationFlipV = 4
orientationTranspose = 5
orientationRotate270 = 6
orientationTransverse = 7
orientationRotate90 = 8