前言:
最近在用Camera2 API做相關(guān)的拍照操作處理時,遇到了拍的照片角度旋轉(zhuǎn)的問題,在網(wǎng)上查找相應的資料后啡省,發(fā)現(xiàn)網(wǎng)上寫的大多是只說明了如何通過返回的數(shù)據(jù)拿到當前的照片的角度,但是由于最終顯示出來的照片要跟相機拍的一樣,所以這還需要拿到當前設(shè)備的旋轉(zhuǎn)角度力九,通過照片原生的角度跟設(shè)備的角度最終得出顯示出來的照片的角度耍铜。
本篇的照片數(shù)據(jù)格式只針對JPEG格式,因為下面將用到通過讀取圖片的EXIF獲取當前照片的旋轉(zhuǎn)角度跌前。
Exif簡介:
可交換圖像文件格式(英語:Exchangeable image file format棕兼,官方簡稱Exif),是專門為數(shù)碼相機的照片設(shè)定的抵乓,可以記錄數(shù)碼照片的屬性信息和拍攝數(shù)據(jù)程储。
Exif最初由日本電子工業(yè)發(fā)展協(xié)會在1996年制定,版本為1.0臂寝。1998年章鲤,升級到2.1,增加了對音頻文件的支持咆贬。2002年3月败徊,發(fā)表了2.2版。
Exif可以附加于JPEG掏缎、TIFF皱蹦、RIFF等文件之中,為其增加有關(guān)數(shù)碼相機拍攝信息的內(nèi)容和索引圖或圖像處理軟件的版本信息眷蜈。
Windows 7操作系統(tǒng)具備對Exif的原生支持沪哺,通過鼠標右鍵點擊圖片打開菜單,點擊屬性并切換到詳細信息標簽下即可直接查看Exif信息酌儒。
Exif信息是可以被任意編輯的辜妓,因此只有參考的功能。Exif信息以0xFFE1作為開頭標記忌怎,后兩個字節(jié)表示Exif信息的長度籍滴。所以Exif信息最大為64 kB,而內(nèi)部采用TIFF格式榴啸。 [1]
也就是說只有JPEG,TIFF,RIFF格式的照片才會有對應的EXIF信息
對于JPEG格式的圖片孽惰,EXIF信息是存在于其文件頭的某個區(qū)域,因為JPEG圖片保存下來是以二進制的形式鸥印,所以這里也有相關(guān)的信息可以知道當前的照片是什么格式的
二進制形式打開文件勋功,文件開始字節(jié)為FF D8,文件結(jié)束兩字節(jié)為FF D9库说。則初步判定文件為jpeg狂鞋。
jpeg的SOI(start of image) 為ff d8,EOD(end of image)為ff d9
相關(guān)信息可參考 : 理解JPEG文件頭的格式
正文 :
在用camera2 api拍照的時候璃弄,有個方法可以設(shè)置拍出來的圖片的旋轉(zhuǎn)角度跟實際拍的角度是一樣:
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, mSensorOrientation);
但是要销,這個方法是否可用,是依賴于底層的夏块,也就是說疏咐,底層沒有做相應的處理的話纤掸,設(shè)置之后才有效果,如果底層沒有做相應的處理浑塞,是沒有作用借跪。(如三星手機是沒有做相應的處理的)
拍照回調(diào)拿到對應得JPEG二進制數(shù)據(jù):
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圖片的二進制數(shù)據(jù)bytes
}
};
拿到JPEG的二進制數(shù)據(jù)后,如果是在Api(24)以上的話酌壕,可以通過ExifInterface 接口拿到對應的Exif相關(guān)信息:
@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;
}
如果想在所有系統(tǒng)都可以用掏愁,就要通過讀取相關(guān)的二進制中對應的EXIf信息:
/**
* 從底層返回的數(shù)據(jù)拿到對應的圖片的角度,有些手機hal層會對手機拍出來的照片作相應的旋轉(zhuǎn)卵牍,有些手機不會(比如三星手機)
* @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圖片的旋轉(zhuǎn)角度果港,其實查看ExifInterface的源碼,也是跟上面的獲取方式一樣糊昙,只不過ExifInterface提供更多的圖片格式獲取.
一般情況下(如果底層沒做旋轉(zhuǎn)處理的話)辛掠,拿著手機正著拍照的時候,拍出來的圖片都是逆時針旋轉(zhuǎn)90度:
這個時候圖片需要順時針選擇90度才是所拍即所得释牺。但是如果手機不是正著萝衩,而是各種角度旋轉(zhuǎn)拍照:
如圖所表示:
手機水平拍的時候,照片是旋轉(zhuǎn)了180度没咙,但是我們想得到的是圖片水平呈現(xiàn)猩谊,也是說手機什么角度,拍出來的圖片什么角度祭刚,也就是照片在原來的基礎(chǔ)上牌捷,再旋轉(zhuǎn)到跟手機的旋轉(zhuǎn)角度一致就行。
實時獲取手機的角度:
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;
}
}
根據(jù)當前sensor獲取手機的角度, 最終拍出來的圖片需要旋轉(zhuǎn)的角度如下:
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