2010-02-17 15 views
20

Quando scatto una foto con l'app fotocamera Androids, rileva l'orientamento del telefono e salva la foto di conseguenza. Quindi, se faccio una foto di un edificio, il tetto sarà sul lato superiore, sia che tenga il telefono in posizione orizzontale o verticale.Come posso trovare l'orientamento di una foto scattata con Intent MediaStore.ACTION_IMAGE_CAPTURE?

Tuttavia, quando uso

intenti imageCaptureIntent = new intenti (MediaStore.ACTION_IMAGE_CAPTURE);

per ottenere un'immagine, l'app fotocamera non reagisce all'orientamento. Se tengo il telefono verticalmente (ritratto), la foto risultante verrà ruotata, con il tetto di detto edificio a sinistra dello schermo.

Come è possibile impostare l'intento in modo che la fotocamera tenga conto dell'orientamento?

Oppure posso dedurre in qualche modo in quale orientamento è stata scattata la foto e ruotarla in seguito?

O qualsiasi altro suggerimento sarà molto apprezzato.

~ Grazie in anticipo, cordiali saluti.

+0

Qui .. http://stackoverflow.com/a/7411824/294884 – Fattie

+0

Leggi la mia soluzione, se ExifInterface non ha funzionato per voi. http://stackoverflow.com/a/24969432/513413 – Hesam

risposta

26

Trovato la risposta. L'exif dell'immagine ha un indicatore dell'orientamento. Solo nel caso, exif può essere visualizzato in Android come questo:

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

Disponibile solo dal livello API 10 e in ... Sto usando questo metodo, ma vorrei un metodo che funzionasse su versioni API inferiori –

+4

Tutte le cose relative all'orientamento come API Level 5: http://developer.android.com/reference/android/media/ExifInterface.html#TAG_ORIENTATION Ecco Android 2.0: http://developer.android.com/guide/appendix/api-levels .html Lo sto usando in 2.1/Api livello 7 con successo. –

+1

Ho implementato questa funzione nella nostra applicazione per il problema delle immagini. Funzionalità davvero buona per la visualizzazione dell'orientamento dell'immagine e gestione nello sviluppo dell'applicazione ........ –

10

Leggi dal Exif se disponibile, altrimenti leggere da 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

Qual è la costante IMAGE_PROVIDER_URI? Grazie. – anivaler

4

Per una soluzione rapida è possibile verificare se la larghezza dell'immagine è più grande altezza dell'immagine significa che è orizzontale e puoi cambiarlo in verticale.

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; 
    } 
Problemi correlati