2014-01-14 95 views
0

的Android儲存的圖像到畫廊不正確的方向 INT getOrientationFromExif()總是相同後得到的值:1 ... 不知道怎麼解決.... 請幫助我!的Android儲存的圖像到畫廊沒有權利本位

你能幫我解決嗎?問題是內部

private PictureCallback mPicture = new PictureCallback() 
{ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) 
    @Override 
    public void onPictureTaken(byte[] data, Camera camera){ 

     File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);  

     try 
     { 
      FileOutputStream fos = new FileOutputStream(pictureFile); 

      fos.write(data); 
      fos.close(); 

      File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), "MyCameraApp"); 

      Bitmap myBitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath()); 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      myBitmap = rotateImage(getOrientationFromExif(pictureFile), myBitmap); 
      myBitmap.compress(Bitmap.CompressFormat.JPEG, 70 /*ignored for PNG*/, bos); 
      byte[] bitmapdata = bos.toByteArray(); 

      fos = new FileOutputStream(pictureFile); 
      fos.write(bitmapdata); 
      fos.close(); 

      sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
      Uri.parse("file://"+ mediaStorageDir))); 
     } 

     catch(FileNotFoundException e) 
     { 
      Log.d(TAG, "File not found: "+e.getMessage()); 
     } 

     catch(IOException e) 
     { 
      Log.d(TAG, "Error accessing file: "+e.getMessage()); 
     } 
    } 
}; 

public Bitmap rotateImage(int angle, Bitmap bitmapSrc) 
{ 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 
    return Bitmap.createBitmap(bitmapSrc, 0, 0, 
    bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true); 
} 

public int getOrientationFromExif(File imagePath) 
{ 
    int orientation = -1; 

    try 
    { 
     ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath()); 
     int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 
     System.out.println("yuri"+exifOrientation); 

     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(TAG, "Unable to get image exif orientation", e); 
    } 

    return orientation; 
} 

public static boolean isSdPresent() { 
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
} 


private static File getOutputMediaFile(int type){ 
    File mediaFile = null; 
    if(isSdPresent() == false) 
    { 
     Log.d(TAG, "There is no Sd card. Cannot use the camera"); 
    } 

    else 
    { 
     File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"."); 

     if(!mediaStorageDir.exists()) 
     { 
      if(!mediaStorageDir.mkdirs()) 
      { 
       Log.d("WorldCupApp", "failed to create directory"); 
       return null; 
      } 
     } 

     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());  
     if (type == MEDIA_TYPE_IMAGE) 
     {   
      mediaFile = new File(mediaStorageDir.getPath() + "IMG_"+ timeStamp + ".JPEG");  
     } 
     else 
     {  
      return null; 
     }   
    } 
    return mediaFile; 
}  
+0

如果這是關於拍照,你可能想要檢查http://stackoverflow.com/questions/4645960/how-to-set-android-camera-orientation-properly – pentadecagon

回答

0
ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath()); 
    int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
      ExifInterface.ORIENTATION_NORMAL); 

問題是在這裏始終是1,則意味着不需要旋轉:(

0

當您解決您的活動,以橫向模式自定義相機實現的方向EXIF數據將始終具有對於TAG_ORIENTATION ORIENTATION_NORMAL,這是具有activity.getWindowManager()相同的副作用。getDefaultDisplay()。getRotation()總返回ROTATION_90。

我通過用OrientationEve檢測所述設備旋轉固定這ntListener,然後將我的onPictureTaken回調中的EXIF方向標記重寫爲正確的旋轉值。

不確定此行爲是否與設備有關,這是運行4.1.2的Galaxy S2上的行爲。