2012-05-15 80 views
1

嗨已建立在Camera Preview上。Android:在風景模式下拍攝的照片強制

即使在縱向模式下,它也顯示了風景預覽。我用一些額外的代碼改變了這個。但是,無論您的橫向/縱向,它總是將圖像保存爲橫向模式。

香港專業教育學院也現在被迫肖像模式:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

我基本上採取任何圖像中的風景出來。 (旋轉拍攝的肖像)。

我該如何讓它在定向模式下保存照片,或鎖定到肖像假設?

我考慮過拍照陣列並旋轉90度。但是,這需要將圖像繪製到位圖上,然後旋轉並存儲回數組。矯枉過正?除非你可以直接旋轉圖像數組?

+0

您只需要將圖像保存在橫向模式下? @Doomsknight – Venky

+0

像是,他們只能保存在肖像模式。或者爲了檢測當前的方向,並採取相應的行動 – Doomsknight

回答

7

先檢查使用下面摘錄相機方向:

private int lookupRotation() {  
     WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 
     Display mDisplay = mWindowManager.getDefaultDisplay(); 
     int rotation = mDisplay.getRotation(); 
     Log.v(LOG_TAG, "rotation: " + rotation); 
     return rotation; 
    } 

然後使用檢查所需的旋轉,並設置你的定位:

if (rotation == Surface.ROTATION_0) { 
    int degreesRotate = 90; 
} 

調整的位圖和旋轉你的位圖基於以下使用方向代碼段:

private Bitmap createBitmap(byte[] imageData, int maxWidth, int maxHeight, 
     int rotationDegrees) throws FileNotFoundException { 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 2; 
    options.inDensity = 240; 
    int imageWidth = 0; 
    int imageHeight = 0; 

    Bitmap image = BitmapFactory.decodeByteArray(imageData, 0, 
      imageData.length, options); 

    imageWidth = image.getWidth(); 
    imageHeight = image.getHeight(); 

    if (imageWidth > maxWidth || imageHeight > maxHeight) { 

     double imageAspect = (double) imageWidth/imageHeight; 
     double desiredAspect = (double) maxWidth/maxHeight; 
     double scaleFactor; 

     if (imageAspect < desiredAspect) { 
      scaleFactor = (double) maxHeight/imageHeight; 
     } else { 
      scaleFactor = (double) maxWidth/imageWidth; 
     } 

     float scaleWidth = ((float) scaleFactor) * imageWidth; 
     float scaleHeight = ((float) scaleFactor) * imageHeight; 

     Bitmap scaledBitmap = Bitmap.createScaledBitmap(image, 
       (int) scaleWidth, (int) scaleHeight, true); 
     image = scaledBitmap; 
    } 

    if (rotationDegrees != 0) { 

     int w = image.getWidth(); 
     int h = image.getHeight(); 
     mtx.postRotate(rotationDegrees); 
     Bitmap rotatedBMP = Bitmap.createBitmap(image, 0, 0, w, h, mtx, 
       true); 
     image = rotatedBMP; 
    } 

    return image; 
} 

上述方法將retu基於方向的rns位圖。

+0

這仍然適用於定向被迫肖像? (旋轉屏幕導致一切不必要的重新創建 - 導致滯後和壞的動畫旋轉效果)。看看getrotation代碼,它似乎在檢查手機方向而不是我的應用程序? - 我試試看,看起來很有前途 – Doomsknight

+0

這是一個處理方向的想法,只需根據您的需求定製 – Venky

+0

這還只是獲得應用的方向。而且,由於速度的原因,現在固定爲肖像,拍照時呈現縱向。不確定如何獲得電話 – Doomsknight