2017-03-10 72 views
1

选择以图片从相机,我在显示GridView控件后。但是,当它在电网的方向变化填充并保存到服务器这个方向变化。机器人形象定位从相机意图改变(或画廊

我。找到了一些代码,这有助于填充图像无定向,但是当保存到服务器其方向仍然改变 下面是代码帮助设置图像无取向:

Bitmap resultBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
     Bitmap returnBitmap = resultBitmap; 
     try { 
      ExifInterface exifInterface = new ExifInterface(filePath); 
      int orientation =                   exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,  ExifInterface.ORIENTATION_UNDEFINED); 
      switch (orientation) { 
       default: 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_90: 
        returnBitmap = rotateImage(resultBitmap, 90); 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_180: 
        returnBitmap = rotateImage(resultBitmap, 180); 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_270: 
        returnBitmap = rotateImage(resultBitmap, 270); 
        break; 
       case ExifInterface.ORIENTATION_NORMAL: 
        break; 

      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return returnBitmap; 

所以我需要显示在图像网格的方向,在它被采取,它应该保存在我的服务器(我可以在我的网站中看到)以相同的方向

回答

0

您是否尝试在拍照后旋转图像而不是将其保存到文件?

获取相机显示方向:

private static int getCameraDisplayOrientation(int cameraId, Activity activity) { 
     int rotation = ((WindowManager)activity.getSystemService(Context.WINDOW_SERVICE)) 
       .getDefaultDisplay().getRotation(); 
     android.hardware.Camera.CameraInfo info = 
       new android.hardware.Camera.CameraInfo(); 
     android.hardware.Camera.getCameraInfo(cameraId, info); 
     int result; 
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
      result = (info.orientation + rotation) % 360; 
      result = (360 - result) % 360; // compensate the mirror 
     } else { // back-facing 
      result = (info.orientation - rotation + 360) % 360; 
     } 
     return result; 
    } 

然后,解码字节数组使用旋转度的1/64从相机显示方向有:

public static Bitmap decodeByteArray(byte[] data, float rotationDegree) { 
    try { 
     Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); 
     if (rotationDegree != 0) { 
      bm = createRotatedBitmap(bm, rotationDegree); 
     } 
     return bm; 
    } catch (OutOfMemoryError e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

希望这有助于。