問題起源:豎屏應用的預覽圖像和拍攝的照片均旋轉了90度
這個問題的原因是,在Android系統中定義了相機傳感器方向,這個方向默認為:當你面對屏幕時,橫置手機,如果攝像頭靠近上方,那么此時即默認方向。當豎立手機,且前置、后置攝像頭都在上方時,前置攝像頭的旋轉角度為270,后置旋轉角度為90。
怎樣應對
該問題影響兩個方面:
- 相機的預覽畫面方向
- 拍攝的照片存儲方向
對于預覽方向,可以通過 CameraInfo.orientation 參數來獲得前文所說的旋轉角度,再通過 Camera.setDisplayOrientation(int degree) 方法來設置預覽角度即可;
而對于存儲方向,情況就復雜一些。設置旋轉角度的方法如下:
Camera.Parameters parameters;
parameters.setRotation(rotation);
mCamera.setParameters(parameters);
但在這個操作之后,不同手機的處理是不同的。有些手機會直接將照片旋轉指定的角度,但照片文件中的 EXIF信息中的 orientation 不變(即仍為0);有些手機會只將 EXIF 信息中的 orientation 保存為相應的角度,但不操作照片方向。
因此,我們需要讀取照片(JPEG 文件)中的 EXIF 信息,根據其 orientation 來旋轉 Bitmap。在 Android 中,有 ExifInterface 類來獲取文件的 EXIF 信息,但該接口只支持用文件來實例化,如下:
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
} catch (IOException ex) {
Log.e(TAG, "cannot read exif", ex);
}
if (exif != null) {
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
}
而相機的 takePicture(shutter, raw, jpeg) 方法回調的 PictureCallback 中,onPictureTaken(final byte[] data, Camera camera) 方法得到的是相片的 byte[] 形式,是否能直接從中讀取我們需要的 orientation 信息呢?
其實,在 Android 4.1.1 的 Camera 源碼中,就已經實現了一個非常簡潔的方法,如下:
// Returns the degrees in clockwise. Values are 0, 90, 180, or 270.
public static int getOrientation(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) {
Log.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) {
Log.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) {
Log.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;
}
Log.i(TAG, "Unsupported orientation");
return 0;
}
offset += 12;
length -= 12;
}
}
Log.i(TAG, "Orientation not found");
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;
}
取得照片的方向信息之后,只要再通過 Bitmap 操作即可得到正確方向的圖片了:
public static Bitmap rotateBitmap(Bitmap bm, float degree) {
Matrix matrix = new Matrix();
matrix.reset();
matrix.setRotate(degree);
Bitmap temp = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
return temp;
}
關于 EXIF 信息的格式
JPEG 文件中的 EXIF 信息一般包括圖像、相機和縮略圖等信息。EXIF 文件格式基本上與 JPEG 文件格式一致,因此加入 EXIF 信息并不影響 JPEG 文件的查看。
JPEG 文件中有一些形如 0xFF**
這樣的數據,它們被稱為“標志(Marker)”,它表示 JPEG 信息數據段。例如 0xFFD8 代表 SOI(Start of image), 0xFFD9 代表 EOI(End of image)。這兩個標志是特例,因為它們之后不跟數據。而一般的標志格式如下:
0xFF+Marker Number(1 byte)+Data size(2 bytes)+Data(n bytes)
標志 0xFFE0~0xFFEF 被稱為 "Application Marker",它們不是解碼 JPEG 文件必須的,可以被用來存儲配置信息等。EXIF 也是利用這個標志段來插入信息的,具體來說,是 APP1(0xFFE1) Marker。所有的 EXIF 信息都存儲在該數據段。我們可以跟據具體的數據結構來解析出需要的信息,比如方向。詳細的數據結構,請查看參考1的文章。
參考: