2012-04-21 21 views
5

Có thể mở Mục đích máy ảnh trong Android ở chế độ chân dung duy nhất, ngay cả khi người dùng lật thiết bị, máy ảnh sẽ vẫn ở chế độ dọc,Buộc máy ảnh luôn mở ở chế độ dọc trong android

tôi đã thử các câu trả lời sau đây: Force Portrait Mode nhưng đã không làm việc, Cảm ơn

+0

xem thêm http://stackoverflow.com/questions/3491961/android-capture-photo và http : //stackoverflow.com/questions/2543059/android-camera-in-portrait-on-surfaceview/6762941#6762941 – EpicPandaForce

Trả lời

4

Thử -

private 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); 
} 

private void initCamera() { 
    if(mCamera == null) { 
      mCamera = Camera.open(); 
      setCameraDisplayOrientation(activity, CameraInfo.CAMERA_FACING_BACK, mCamera); 
      mCamera.unlock(); 
     } 
} 
+1

'cameraID' chỉ khả dụng ở trên API Lv.9. Phương pháp này từ Google có thể hoạt động đối với những thiết bị trên API Lv.9, đối với những người dưới 9 tuổi, câu lệnh if để xem liệu 'Build.VERSION.SDK_INT' có lớn hơn' Build.VERSION_CODES.GINGERBREAD' là cần thiết hay không. – dumbfingers

+1

Đã hoạt động trên API Lv14 trở lên, đã bị lỗi trên 2.3.6 –

Các vấn đề liên quan