前言:
最近在用Camera2 API做相關的拍照操作處理時,遇到了拍的照片角度旋轉的問題,在網上查找相應的資料后,發現網上寫的大多是只說明了如何通過返回的數據拿到當前的照片的角度,但是由于最終顯示出來的照片要跟相機拍的一樣,所以這還需要拿到當前設備的旋轉角度,通過照片原生的角度跟設備的角度最終得出顯示出來的照片的角度。
本篇的照片數據格式只針對JPEG格式,因為下面將用到通過讀取圖片的EXIF獲取當前照片的旋轉角度。
Exif簡介:
可交換圖像文件格式(英語:Exchangeable image file format,官方簡稱Exif),是專門為數碼相機的照片設定的,可以記錄數碼照片的屬性信息和拍攝數據。
Exif最初由日本電子工業發展協會在1996年制定,版本為1.0。1998年,升級到2.1,增加了對音頻文件的支持。2002年3月,發表了2.2版。
Exif可以附加于JPEG、TIFF、RIFF等文件之中,為其增加有關數碼相機拍攝信息的內容和索引圖或圖像處理軟件的版本信息。
Windows 7操作系統具備對Exif的原生支持,通過鼠標右鍵點擊圖片打開菜單,點擊屬性并切換到詳細信息標簽下即可直接查看Exif信息。
Exif信息是可以被任意編輯的,因此只有參考的功能。Exif信息以0xFFE1作為開頭標記,后兩個字節表示Exif信息的長度。所以Exif信息最大為64 kB,而內部采用TIFF格式。 [1]
也就是說只有JPEG,TIFF,RIFF格式的照片才會有對應的EXIF信息
對于JPEG格式的圖片,EXIF信息是存在于其文件頭的某個區域,因為JPEG圖片保存下來是以二進制的形式,所以這里也有相關的信息可以知道當前的照片是什么格式的
二進制形式打開文件,文件開始字節為FF D8,文件結束兩字節為FF D9。則初步判定文件為jpeg。
jpeg的SOI(start of image) 為ff d8,EOD(end of image)為ff d9
相關信息可參考 : 理解JPEG文件頭的格式
正文 :
在用camera2 api拍照的時候,有個方法可以設置拍出來的圖片的旋轉角度跟實際拍的角度是一樣:
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, mSensorOrientation);
但是,這個方法是否可用,是依賴于底層的,也就是說,底層沒有做相應的處理的話,設置之后才有效果,如果底層沒有做相應的處理,是沒有作用。(如三星手機是沒有做相應的處理的)
拍照回調拿到對應得JPEG二進制數據:
private ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireNextImage();
ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes); //拿到Jpeg圖片的二進制數據bytes
}
};
拿到JPEG的二進制數據后,如果是在Api(24)以上的話,可以通過ExifInterface 接口拿到對應的Exif相關信息:
@TargetApi(24)
public static int readPictureDegree(InputStream stream) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(stream);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
default:
degree = 0;
}
// exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, "no");
// exifInterface.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
如果想在所有系統都可以用,就要通過讀取相關的二進制中對應的EXIf信息:
/**
* 從底層返回的數據拿到對應的圖片的角度,有些手機hal層會對手機拍出來的照片作相應的旋轉,有些手機不會(比如三星手機)
* @param jpeg
* @return
*/
public static int getNaturalOrientation(byte[] jpeg) {
if (jpeg == null) {
return 0;
}
int offset = 0;
int length = 0;
// ISO/IEC 10918-1:1993(E)
while (offset + 3 < jpeg.length && (jpeg[offset++] & 0xFF) == 0xFF) {
int marker = jpeg[offset] & 0xFF;
// Check if the marker is a padding.
if (marker == 0xFF) {
continue;
}
offset++;
// Check if the marker is SOI or TEM.
if (marker == 0xD8 || marker == 0x01) {
continue;
}
// Check if the marker is EOI or SOS.
if (marker == 0xD9 || marker == 0xDA) {
break;
}
// Get the length and check if it is reasonable.
length = pack(jpeg, offset, 2, false);
if (length < 2 || offset + length > jpeg.length) {
KSLog.e(TAG, "Invalid length");
return 0;
}
// Break if the marker is EXIF in APP1.
if (marker == 0xE1 && length >= 8 &&
pack(jpeg, offset + 2, 4, false) == 0x45786966 &&
pack(jpeg, offset + 6, 2, false) == 0) {
offset += 8;
length -= 8;
break;
}
// Skip other markers.
offset += length;
length = 0;
}
// JEITA CP-3451 Exif Version 2.2
if (length > 8) {
// Identify the byte order.
int tag = pack(jpeg, offset, 4, false);
if (tag != 0x49492A00 && tag != 0x4D4D002A) {
KSLog.e(TAG, "Invalid byte order");
return 0;
}
boolean littleEndian = (tag == 0x49492A00);
// Get the offset and check if it is reasonable.
int count = pack(jpeg, offset + 4, 4, littleEndian) + 2;
if (count < 10 || count > length) {
KSLog.e(TAG, "Invalid offset");
return 0;
}
offset += count;
length -= count;
// Get the count and go through all the elements.
count = pack(jpeg, offset - 2, 2, littleEndian);
while (count-- > 0 && length >= 12) {
// Get the tag and check if it is orientation.
tag = pack(jpeg, offset, 2, littleEndian);
if (tag == 0x0112) {
// We do not really care about type and count, do we?
int orientation = pack(jpeg, offset + 8, 2, littleEndian);
switch (orientation) {
case 1:
return 0;
case 3:
return 180;
case 6:
return 90;
case 8:
return 270;
}
KSLog.i(TAG, "Unsupported orientation");
return 0;
}
offset += 12;
length -= 12;
}
}
return 0;
}
private static int pack(byte[] bytes, int offset, int length,
boolean littleEndian) {
int step = 1;
if (littleEndian) {
offset += length - 1;
step = -1;
}
int value = 0;
while (length-- > 0) {
value = (value << 8) | (bytes[offset] & 0xFF);
offset += step;
}
return value;
}
上面拿到了JPEG圖片的旋轉角度,其實查看ExifInterface的源碼,也是跟上面的獲取方式一樣,只不過ExifInterface提供更多的圖片格式獲取.
一般情況下(如果底層沒做旋轉處理的話),拿著手機正著拍照的時候,拍出來的圖片都是逆時針旋轉90度:
這個時候圖片需要順時針選擇90度才是所拍即所得。但是如果手機不是正著,而是各種角度旋轉拍照:
如圖所表示:
手機水平拍的時候,照片是旋轉了180度,但是我們想得到的是圖片水平呈現,也是說手機什么角度,拍出來的圖片什么角度,也就是照片在原來的基礎上,再旋轉到跟手機的旋轉角度一致就行。
實時獲取手機的角度:
public class MyOrientationEventListener extends OrientationEventListener{
public MyOrientationEventListener(Context context) {
super(context);
}
@Override
public void onOrientationChanged(int orientation) {
// We keep the last known orientation. So if the user first orient
// the camera then point the camera to floor or sky, we still have
// the correct orientation.
if (orientation != ORIENTATION_UNKNOWN) {
mDeviceOrientation = normalize(orientation);
}
}
private int normalize(int orientation) {
if ((orientation > 315) || (orientation <= 45)) {
return 0;
}
if (orientation > 45 && orientation <= 135) {
return 90;
}
if (orientation <= 225) {
return 180;
}
if (orientation > 225 && orientation <= 315) {
return 270;
}
return 0;
}
}
根據當前sensor獲取手機的角度, 最終拍出來的圖片需要旋轉的角度如下:
public static int getJpegOrientation(int naturalJpegOrientation, int deviceOrientation) {
return (naturalJpegOrientation+ deviceOrientation) % 360;
}
最終圖片如下:
Bitmap thumb = BitmapFactory.decodeByteArray(bytes, offset, length, options);
Matrix matrix = new Matrix();
matrix.postRotate(mJpegOrientation);
Bitmap newThumb = Bitmap.createBitmap(thumb, 0, 0, thumb.getWidth(), thumb.getHeight(), matrix, true);
以上,分析到此接受 thx