2012-12-17 11 views
6

hier ist mein CodeHaltebildausrichtung nach der Kompression

 // 
     // 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; 

Nach der Kompression und Speicherung wird das Bild um 90 Grad gedreht, obwohl ExifInterface gibt 0 (ORIENTATION_UNDEFINED). Beliebiger Zeiger, wie kann ich die Ausrichtung des Quellbildes beibehalten? In diesem Fall ist es 6 (oder ORIENTATION_ROTATE_90).

danke.

Antwort

4

Nach etwas mehr Suche auf Stackoverflow, festgestellt, dass jemand hat dieses Problem bereits erklärt here schön mit Lösung (was ich habe upvoted).

0

Der folgende Code gibt den Bildwinkel durch ORIENTATION zurück.

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

danke Chintan für die Antwort. Was Sie gezeigt haben, ist nicht gerade mein Problem. Ich kenne die Orientierung. Das Problem ist, dass das gespeicherte Bild nach der Komprimierung immer automatisch um 90 Grad gedreht wird und 0 oder UNDEFINED zurückgibt, wenn es über ExifInterface abgefragt wird. Was ich will, ist die gleiche Ausrichtung des komprimierten Bildes beim Schreiben in die Datei. Ich hoffe, ich habe das Problem erklärt. – iuq