Android 는 ExifInterface 를 통 해 카메라 이미지 방향 을 판단 하 는 방법
이미지 의 Sensor 방향:핸드폰 Camera 의 이미지 데 이 터 는 모두 카메라 하드웨어 의 이미지 센서(Image Sensor)에서 나 온 것 입 니 다.이 Sensor 는 핸드폰 에 고정 되면 기본 적 인 촬영 방향 이 있 습 니 다.이 방향 은 다음 그림 과 같 습 니 다.좌표 원점 은 핸드폰 이 가로로 놓 일 때의 왼쪽 상단 에 있 습 니 다.
안 드 로 이 드 애플 리 케 이 션 에서 카메라 그림 을 사용 할 때 반드시 고려 해 야 할 문 제 는 그림 의 방향 이다.방향 을 판단 해 야 그림 을 조정 하여 더욱 잘 표현 할 수 있다.본 고 는 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) {
}
}
}
이 코드 는 두 가지 기능 을 포함 합 니 다.1.그림 크기 조정:원본 그림 은 일반적으로 크 므 로 줄 여야 사용 할 수 있 습 니 다.
2.사진 회전:사용자 가 사진 을 찍 을 때 핸드폰 각도 가 다 르 기 때문에 얻 은 사진 은 회전 이 필요 할 수 있 습 니 다.
총결산
이상 은 이 글 의 모든 내용 입 니 다.본 고의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가 치 를 가지 기 를 바 랍 니 다.여러분 의 저희 에 대한 지지 에 감 사 드 립 니 다.더 많은 내용 을 알 고 싶다 면 아래 링크 를 보 세 요.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.