2010-02-17 32 views
20

Khi tôi chụp ảnh bằng ứng dụng máy ảnh Android, nó sẽ phát hiện hướng của điện thoại và lưu ảnh theo đó. Vì vậy, nếu tôi chụp ảnh một tòa nhà, mái nhà sẽ ở trên đỉnh, cho dù tôi giữ điện thoại ở vị trí ngang hoặc chân dung.Tôi làm cách nào để tìm hướng của ảnh được chụp bằng Intent MediaStore.ACTION_IMAGE_CAPTURE?

Tuy nhiên, khi tôi sử dụng

Ý định imageCaptureIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);

để tải ảnh, ứng dụng máy ảnh không phản ứng với hướng. Nếu tôi giữ điện thoại theo chiều dọc (dọc), ảnh thu được sẽ được xoay, với mái nhà của tòa nhà được nói ở bên trái màn hình.

Làm thế nào tôi có thể đặt mục đích sao cho máy ảnh sẽ định hướng?

Hoặc tôi có thể suy ra một cách nào đó theo định hướng nào mà pic được chụp và xoay nó sau đó không?

Hoặc bất kỳ đề xuất nào khác sẽ được đánh giá cao.

~ Cảm ơn trước, lời chúc mừng tốt đẹp nhất.

+0

Ở đây .. http://stackoverflow.com/a/7411824/294884 – Fattie

+0

Đọc giải pháp của tôi nếu ExifInterface không hoạt động cho bạn. http://stackoverflow.com/a/24969432/513413 – Hesam

Trả lời

26

Tìm câu trả lời. Exif của hình ảnh có chỉ báo hướng. Chỉ trong trường hợp, exif có thể được xem trong Android như thế này:

ExifInterface exif = new ExifInterface("filepath"); 
exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
+1

Chỉ có sẵn từ API Cấp 10 trở lên ... Tôi đang sử dụng phương pháp này, nhưng muốn một phương pháp hoạt động trên các phiên bản API thấp hơn –

+4

Tất cả nội dung định hướng sẽ trông như API cấp 5 cho tôi: http://developer.android.com/reference/android/media/ExifInterface.html#TAG_ORIENTATION Đó là Android 2.0: http://developer.android.com/guide/appendix/api-levels .html Tôi đang sử dụng nó ở cấp 2.1/Api 7 thành công. –

+1

Tôi đã thực hiện tính năng này trong ứng dụng của chúng tôi cho vấn đề về hình ảnh. Tính năng thực sự tốt cho hiển thị định hướng hình ảnh và quản lý trong việc phát triển ứng dụng ........ –

10

đọc từ Exif nếu có, nếu không đọc từ MediaStore

public static int getImageOrientation(Context context, String imagePath) { 
    int orientation = getOrientationFromExif(imagePath); 
    if(orientation <= 0) { 
     orientation = getOrientationFromMediaStore(context, imagePath); 
    } 

    return orientation; 
} 

private static int getOrientationFromExif(String imagePath) { 
    int orientation = -1; 
    try { 
     ExifInterface exif = new ExifInterface(imagePath); 
     int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

     switch (exifOrientation) { 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       orientation = 270; 

       break; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       orientation = 180; 

       break; 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       orientation = 90; 

       break; 

      case ExifInterface.ORIENTATION_NORMAL: 
       orientation = 0; 

       break; 
      default: 
       break; 
     } 
    } catch (IOException e) { 
     Log.e(LOG_TAG, "Unable to get image exif orientation", e); 
    } 

    return orientation; 
} 

private static int getOrientationFromMediaStore(Context context, String imagePath) { 
    Uri imageUri = getImageContentUri(context, imagePath); 
    if(imageUri == null) { 
     return -1; 
    } 

    String[] projection = {MediaStore.Images.ImageColumns.ORIENTATION}; 
    Cursor cursor = context.getContentResolver().query(imageUri, projection, null, null, null); 

    int orientation = -1; 
    if (cursor != null && cursor.moveToFirst()) { 
     orientation = cursor.getInt(0); 
     cursor.close(); 
    } 

    return orientation; 
} 

private static Uri getImageContentUri(Context context, String imagePath) { 
    String[] projection = new String[] {MediaStore.Images.Media._ID}; 
    String selection = MediaStore.Images.Media.DATA + "=? "; 
    String[] selectionArgs = new String[] {imagePath}; 
    Cursor cursor = context.getContentResolver().query(IMAGE_PROVIDER_URI, projection, 
      selection, selectionArgs, null); 

    if (cursor != null && cursor.moveToFirst()) { 
     int imageId = cursor.getInt(0); 
     cursor.close(); 

     return Uri.withAppendedPath(IMAGE_PROVIDER_URI, Integer.toString(imageId)); 
    } 

    if (new File(imagePath).exists()) { 
     ContentValues values = new ContentValues(); 
     values.put(MediaStore.Images.Media.DATA, imagePath); 

     return context.getContentResolver().insert(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    } 

    return null; 
} 
+8

IMAGE_PROVIDER_URI là gì? Cảm ơn bạn. – anivaler

4

Đối với một sửa chữa nhanh chóng, bạn có thể kiểm tra xem chiều rộng hình ảnh là lớn hơn chiều cao của hình ảnh. nó có nghĩa là phong cảnh của nó và bạn có thể thay đổi nó thành chân dung.

private Bitmap fromGallery(final Uri selectedImageUri) { 
     try { 
      Bitmap bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri); 

      ExifInterface exif = new ExifInterface(selectedImageUri.getPath()); 
      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

      int angle = 0; 
      switch (orientation) { 
       case ExifInterface.ORIENTATION_ROTATE_90: 
        angle = 90; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_180: 
        angle = 180; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_270: 
        angle = 270; 
        break; 
       default: 
        angle = 0; 
        break; 
      } 
      Matrix mat = new Matrix(); 
      if (angle == 0 && bm.getWidth() > bm.getHeight()) 
       mat.postRotate(90); 
      else 
       mat.postRotate(angle); 

      return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mat, true); 

     } catch (IOException e) { 
      Log.e("", "-- Error in setting image"); 
     } catch (OutOfMemoryError oom) { 
      Log.e("", "-- OOM Error in setting image"); 
     } 
     return null; 
    } 
Các vấn đề liên quan