今天就跟大家聊聊有關怎么在Android中使用ExifInterface判斷Camera圖片的方向,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
創新互聯專注于同江網站建設服務及定制,我們擁有豐富的企業做網站經驗。 熱誠為您提供同江營銷型網站建設,同江網站制作、同江網頁設計、同江網站官網定制、小程序制作服務,打造同江網絡公司原創品牌,更為您提供同江網站排名全網營銷落地服務。
Android的Camera相關應用開發中,有一個必須搞清楚的知識點,就是Camera的預覽方向和拍照方向
圖像的Sensor方向:手機Camera的圖像數據都是來自于攝像頭硬件的圖像傳感器(Image Sensor),這個Sensor被固定到手機之后是有一個默認的取景方向的,這個方向如下圖所示,坐標原點位于手機橫放時的左上角:

android應用里使用相機圖片時必須要考慮的一個問題就是圖片朝向,只有判斷對朝向才能調整圖片從而更好的展現。本文將介紹一種通過ExifInterface判斷圖片朝向的方法!上代碼:
/**
* 利用給定路徑下的圖片設置ImageView
* @param imgPath 手機圖片文件路徑
* @param imgView 需要設置的ImageView
*/
public void setImg(String imgPath, ImageView imgView) {
File file = new File(imgPath);
if (file.exists() && file.canRead()) {
// -------1.圖片縮放--------
// 手機屏幕信息
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int dw = metric.widthPixels; // 屏幕寬
int dh = metric.heightPixels; // 屏幕高
// 加載圖像,只是為了獲取尺寸
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 設置之后可以獲取尺寸信息
Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
// 計算水平和垂直縮放系數
int heightRatio = (int) Math.ceil(options.outHeight / (float) dh);
int widthRatio = (int) Math.ceil(options.outWidth / (float) dw);
// 判斷哪個大
if (heightRatio > 1 && widthRatio > 1) {
if (heightRatio > widthRatio) {
options.inSampleSize = heightRatio;
} else {
options.inSampleSize = widthRatio;
}
}
// 圖片縮放
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(imgPath, options);
// -------2.判斷圖片朝向--------
try {
ExifInterface exif = new ExifInterface(imgPath);
int degree = 0; // 圖片旋轉角度
if (exif != null) {
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
if (orientation != -1) {
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:
break;
}
}
}
if (degree != 0) { // 圖片需要旋轉
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preRotate(degree);
Bitmap mRotateBitmap = Bitmap.createBitmap(bitmap, 0, 0,
width, height, matrix, true);
imgView.setImageBitmap(mRotateBitmap);
} else {
imgView.setImageBitmap(bitmap);
}
} catch (IOException e) {
}
}
}Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
看完上述內容,你們對怎么在Android中使用ExifInterface判斷Camera圖片的方向有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創新互聯行業資訊頻道,感謝大家的支持。
網站欄目:怎么在Android中使用ExifInterface判斷Camera圖片的方向
標題路徑:http://www.yijiale78.com/article12/pjdcgc.html
成都網站建設公司_創新互聯,為您提供網站內鏈、營銷型網站建設、微信公眾號、搜索引擎優化、Google、網站設計公司
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯