2017-03-21 12 views
1

Android Oneの電話機やエミュレータでアプリケーションをテストすると問題はなくなりますが、他のデバイスで問題はあります。アンドロイドでカメラの方向を検出する方法は?

問題は、私は基本的にカメラの意図を送信し、ユーザーが写真を撮った後にデータをインテントから取得し、カメラから取得したものをImageViewのピクセルに設定します。いくつかのデバイス(サムスンの大部分)の画像は回転しているので、撮影されたときには表示されません。

私のアプリはポートレートモードでのみ動作しますが、画像を撮るときに電話機を回転させると、風景モードで写真を撮ることもできます。

デバイスがイメージを回転するデフォルトの角度を検出する方法はありますか?イメージを撮影した後にビットマップを回転させますか?ここで

コードです:

意図送信:あなたがMAKしたい場合は

public void setBitmapOfImageView(int photoId, Bitmap bitmap) 
{ 
    mPhotos[photoId].setImageBitmap(bitmap); 

    mPhotosState[photoId] = 1; 
} 

public Bitmap decodeAndReturnBitmap(String filePath) 
{ 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 

    BitmapFactory.decodeFile(filePath, o); 

    final int REQUIRED_SIZE = 512; 

    int widthTemp = o.outWidth, heightTemp = o.outHeight; 
    int scale  = 1; 

    while (true) { 
     if (widthTemp < REQUIRED_SIZE && heightTemp < REQUIRED_SIZE) { 
      break; 
     } 

     widthTemp /= 2; 
     heightTemp /= 2; 
     scale  *= 2; 
    } 

    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize   = scale; 

    return BitmapFactory.decodeFile(filePath, o2); 
} 

答えて

0

:フラグメント

File path = new File(getActivity().getFilesDir(), "map_roomie"); 

if (!path.exists()) path.mkdirs(); 

File imageFile = new File(path, mFileName); 

setBitmapOfImageView(mCurrentPhotoId, decodeAndReturnBitmap(imageFile.getAbsolutePath())); 

ヘルパー関数の意図を取り込む

File path = new File(getActivity().getFilesDir(), "map_roomie"); 

if (!path.exists()) path.mkdirs(); 

mFileName = createImageFileName(); 

File image = new File(path, mFileName); 
Uri imageUri = FileProvider.getUriForFile(getActivity(), AddRoomFragment.CAPTURE_IMAGE_FILE_PROVIDER, image); 

Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
             imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
              startActivityForResult(imageCaptureIntent, AddRoomFragment.CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 

をカメラ画像がディスプレイと同じ向きで表示されている場合は、次のコードを使用できます。さらに詳細については

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

https://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)

+0

ありがとう:ここ

はコードです。 2番目と3番目の引数に引数をつけて渡す必要がありますか? –

+0

あなたが書いたコードを共有していただけますか? –

0

を参照してください見てください - 私はExifInterfaceを使用して問題を修正しました

private int getCameraId() { 
     int curCameraId = 0; 

     if (Camera.getNumberOfCameras() > 0) { 
      curCameraId = (curCameraId + 1) % Camera.getNumberOfCameras(); 
     } else { 
      curCameraId = 0; 
     } 
     return curCameraId; 
    } 

    public void setCameraDisplayOrientation(Activity activity, int curCameraId) { 
     if (camera == null) { 
      try { 
       camera = Camera.open(curCameraId); 
      } catch (Exception e) { 
      } 
     } 
     Camera.CameraInfo info = new Camera.CameraInfo(); 

     Camera.getCameraInfo(curCameraId, info); 
     WindowManager winManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE); 
     int rotation = winManager.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); 
    } 
+0

java.lang.RuntimeException:アクティビティを開始できませんでした。ComponentInfo java.lang.NullPointerException:ヌルオブジェクトリファレンスで仮想メソッド 'void android.hardware.Camera.setDisplayOrientation(int)'を呼び出そうとしています –

0

。しかし、画像を撮るときに、画像に適用されるデフォルトの角度を検出することに誰かが光を当てることができれば、それは素晴らしいことです。迅速な応答のための

public Bitmap decodeAndReturnBitmap(String filePath) 
{ 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 

    BitmapFactory.decodeFile(filePath, o); 

    final int REQUIRED_SIZE = 512; 

    int widthTemp = o.outWidth, heightTemp = o.outHeight; 
    int scale  = 1; 

    while (true) { 
     if (widthTemp < REQUIRED_SIZE && heightTemp < REQUIRED_SIZE) { 
      break; 
     } 

     widthTemp /= 2; 
     heightTemp /= 2; 
     scale  *= 2; 
    } 

    ExifInterface exifInterface = null; 

    try { 
     exifInterface = new ExifInterface(filePath); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); 

    Matrix matrix = new Matrix(); 

    switch(orientation) { 
     case ExifInterface.ORIENTATION_ROTATE_90 : 
      matrix.setRotate(90.0f); 
      break; 

     case ExifInterface.ORIENTATION_ROTATE_180: 
      matrix.setRotate(180.0f); 
      break; 

     case ExifInterface.ORIENTATION_ROTATE_270: 
      matrix.setRotate(270.0f); 
      break; 

    } 

    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize   = scale; 

    Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2); 

    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
} 
0
this solution work properly for me. 


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); 
    } 
関連する問題