Android 는 ExifInterface 를 통 해 카메라 이미지 방향 을 판단 하 는 방법

Android 의 Camera 관련 애플 리 케 이 션 개발 에 있어 반드시 알 아야 할 지식 이 있 는데 그것 이 바로 Camera 의 미리 보기 방향 과 사진 촬영 방향 입 니 다.
이미지 의 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.사진 회전:사용자 가 사진 을 찍 을 때 핸드폰 각도 가 다 르 기 때문에 얻 은 사진 은 회전 이 필요 할 수 있 습 니 다.
총결산
이상 은 이 글 의 모든 내용 입 니 다.본 고의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가 치 를 가지 기 를 바 랍 니 다.여러분 의 저희 에 대한 지지 에 감 사 드 립 니 다.더 많은 내용 을 알 고 싶다 면 아래 링크 를 보 세 요.

좋은 웹페이지 즐겨찾기