2015-02-23 128 views
0

我試圖從我的前置攝像頭捕獲圖像並將其保存到我的畫廊,它在兩個方向都可以正常使用後置攝像頭肖像和風景 但前置攝像頭它工作正常,只有山水,圖像從前置攝像頭倒置保存

,如果它的肖像圖像旋轉180度顛倒 我曾嘗試下面的代碼,因爲它的最常見的解決辦法,但沒有任何工程

FileOutputStream fos; 
       try { 
        fos = new FileOutputStream(fileName); 
        fos.write(data); 
        Bitmap bm = BitmapFactory.decodeFile(fileName); 

        Matrix matrix = new Matrix(); 
        matrix.postRotate(180); 
        Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, 
          bm.getWidth(), bm.getHeight(), matrix, false); 
        rotatedBitmap.compress(CompressFormat.JPEG, 100, fos); 

        fos.close(); 

       } catch (FileNotFoundException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }`` 
+0

'但沒有什麼作用。請更好地解釋發生了什麼。 'fos = new FileOutputStream(fileName);'。此時該文件被刪除並設置爲0。'fos.write(data);'這裏發生了什麼? '位圖bm = BitmapFactory.decodeFile(文件名);'。你在這裏加載這個空文件。更好:把'Bitmap bm = BitmapFactory.decodeFile(fileName);作爲第一個陳述。 ' – greenapps 2015-02-23 13:59:34

+0

我現在看到,也許你首先嚐試將數據中的位圖保存爲文件,然後將該文件作爲位圖加載,旋轉並再次保存。那麼,只有你把fos.close();在使用decodeFile()之前。然後在循環後你應該創建一個新的FileOutputStream。但爲什麼不保存之前旋轉位圖? – greenapps 2015-02-23 14:04:52

回答

0

每當用新相機切換相機時撥打此號碼CameraId

public static void setCameraDisplayOrientation(Activity activity, 
     int cameraId, android.hardware.Camera camera) { 
    android.hardware.Camera.CameraInfo info = 
      new android.hardware.Camera.CameraInfo(); 
    android.hardware.Camera.getCameraInfo(cameraId, info); 
    int rotation = activity.getWindowManager().getDefaultDisplay() 
      .getRotation(); 
    int degrees = 0; 
    switch (rotation) { 
     case Surface.ROTATION_0: degrees = 0; break; 
     case Surface.ROTATION_90: degrees = 90; break; 
     case Surface.ROTATION_180: degrees = 180; break; 
     case Surface.ROTATION_270: degrees = 270; break; 
    } 

    int result; 
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
     result = (info.orientation + degrees) % 360; 
     result = (360 - result) % 360; // compensate the mirror 
    } else { // back-facing 
     result = (info.orientation - degrees + 360) % 360; 
    } 
    camera.setDisplayOrientation(result); 
}