2012-12-17 10 views
6

ecco mio codicemantenendo orientamento dell'immagine dopo la compressione

 // 
     // reading an image captured using phone camera. Orientation of this 
     // image is always return value 6 (ORIENTATION_ROTATE_90) no matter if 
     // it is captured in landscape or portrait mode 
     // 
     Bitmap bmp = BitmapFactory.decodeFile(imagePath.getAbsolutePath()); 

     // 
     // save as : I am compressing this image and writing it back. Orientation 
     //of this image always returns value 0 (ORIENTATION_UNDEFINED) 
     imagePath = new File(imagePath.getAbsolutePath().replace(".jpg", "_1.jpg")); 



     FileOutputStream fos0 = new FileOutputStream(imagePath); 
     boolean b = bmp.compress(CompressFormat.JPEG, 10, fos0); 
     fos0.flush(); 
     fos0.close(); 
     fos0 = null; 

Dopo la compressione e il salvataggio, l'immagine viene ruotata di 90 gradi se ExifInterface restituisce 0 (ORIENTATION_UNDEFINED). Qualsiasi puntatore, come posso mantenere l'orientamento dell'immagine sorgente? in questo caso è 6 (o ORIENTATION_ROTATE_90).

grazie.

risposta

4

Dopo poco più di ricerca su StackOverflow, ha scoperto che qualcuno ha già spiegato la questione here splendidamente con la soluzione (che ho upvoted).

0

Il codice seguente restituirà l'angolo dell'immagine con il suo ORIENTATION.

public static float rotationForImage(Context context, Uri uri) { 
     if (uri.getScheme().equals("content")) { 
     String[] projection = { Images.ImageColumns.ORIENTATION }; 
     Cursor c = context.getContentResolver().query(
       uri, projection, null, null, null); 
     if (c.moveToFirst()) { 
      return c.getInt(0); 
     } 
    } else if (uri.getScheme().equals("file")) { 
     try { 
      ExifInterface exif = new ExifInterface(uri.getPath()); 
      int rotation = (int)exifOrientationToDegrees(
        exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
          ExifInterface.ORIENTATION_NORMAL)); 
      return rotation; 
     } catch (IOException e) { 
      Log.e(TAG, "Error checking exif", e); 
     } 
    } 
     return 0f; 
    } 

    private static float exifOrientationToDegrees(int exifOrientation) { 
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { 
     return 90; 
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { 
     return 180; 
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { 
     return 270; 
    } 
    return 0; 
} 
} 
+0

grazie a Chintan per la risposta. Quello che hai mostrato non è esattamente il mio problema. Conosco l'orientamento. Il problema è che, dopo la compressione, l'immagine salvata viene sempre ruotata automaticamente di 90 gradi e restituisce 0 o UNDEFINED se interrogato tramite ExifInterface. Quello che voglio è, lo stesso orientamento dell'immagine compressa durante la scrittura su file. Spero di aver spiegato il problema. – iuq

Problemi correlati