2017-04-18 11 views
2

Androidのカメラアプリを作成しています。これは、camera APIを使用したカスタムカメラです。
カメラから写真を撮るたびに、いつでもランドスケープモードで保存します。
私はこの方法を使用してカメラの向きを設定しています。Orientate Image Androidのカメラから撮影しました

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

画像の保存を担当するコードです。

PictureCallback jpegCallback = new PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) { 
     File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); 
     if (pictureFile == null){ 
      Log.d(TAG, "Error creating media file, check storage permissions: "); 
      return; 
     } 

     try { 
      FileOutputStream fos = new FileOutputStream(pictureFile); 
      fos.write(data); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      Log.d(TAG, "File not found: " + e.getMessage()); 
     } catch (IOException e) { 
      Log.d(TAG, "Error accessing file: " + e.getMessage()); 
     } 
    } 
    }; 

このコードでは、画像は正常に保存されますが、常にランドスケープモードになります。私が見逃しているのは、私はアンドロイドで新しいです。プレビューに表示されているように画像を保存したい

アップデート2私はこの方法でコードを使用していますが、これは動作していない

 PictureCallback jpegCallback = new PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) { 

     Camera.CameraInfo info = new Camera.CameraInfo(); 

     int rotationAngle = getCorrectCameraOrientation (MainActivity.this, info); 
    Toast.makeText(MainActivity.this, "Angle "+ rotationAngle, Toast.LENGTH_SHORT).show(); 
     File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); 

     try { 
      writeFile (data, pictureFile); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     processImage (pictureFile, rotationAngle, 1); 
    } 
    }; 

(回答によると)まだ同じ問題の画像は、風景モードでは常にあります。

+0

下記の私の回答をご覧ください http://stackoverflow.com/questions/43447987/android-camera-provide-upside-down-data-on-back-camera-in-some-devices/43449660#43449660 – Pehlaj

+0

なぜupdate2のコードが変更されますか?あなたはまだデータ配列を保存しています。カメラの情報や方向は何もしないでください。 – greenapps

答えて

1

下に定義されたメソッド(WriteFile関数、processImageとgetCorrectCameraOrientation)を追加し、全ての

まず試し

計算カメラの向きonPictureTakenの写真をキャプチャし、更新した後(ステップ3の後) **

@Override 
public void onPictureTaken (final byte[] data, final Camera camera) { 

    int rotationAngle = getCorrectCameraOrientation (this, info); 

3.ファイルを作成し、写真の角度Uを更新歌う回転角度

File file = new File (folder, fileName); 

try { 
    file.createNewFile(); 
} 
catch (IOException e) { 
    e.printStackTrace(); 
} 

writeFile (data, file); 
processImage (file, rotationAngle, compressRatio); 

のWriteFile

public static void writeFile (byte[] data, File file) throws IOException { 

    BufferedOutputStream bos = null; 

    try { 
     FileOutputStream fos = new FileOutputStream (file); 
     bos = new BufferedOutputStream (fos); 
     bos.write (data); 
    } 
    finally { 
     if (bos != null) { 
      try { 
       bos.flush(); 
       bos.close(); 
      } 
      catch (Exception e) { 
      } 
     } 
    } 
} 

processImage

public static void processImage (File file, int rotationAngle, int compressionRatio) { 

    BufferedOutputStream bos = null; 

    try { 

     Bitmap bmp = BitmapFactory.decodeFile (file.getPath()); 

     Matrix matrix = new Matrix(); 
     matrix.postRotate (rotationAngle); 

     bmp = Bitmap.createBitmap (bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 

     FileOutputStream fos = new FileOutputStream (file); 
     bmp.compress (Bitmap.CompressFormat.PNG, compressionRatio, fos); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    catch (OutOfMemoryError t) { 
     t.printStackTrace(); 
    } 
    catch (Throwable t) { 
     t.printStackTrace(); 
    } 
    finally { 
     if (bos != null) { 
      try { 
       bos.flush(); 
       bos.close(); 
      } 
      catch (Exception e) { 
      } 
     } 
    } 
} 

getCorrectCameraOrientation

public static int getCorrectCameraOrientation (Activity activity, Camera.CameraInfo info) { 

    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 
    int degrees = 0; 

    if (hasValidRotation (rotation)) { 
     degrees = rotation * 90; 
    } 

    int result; 
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
     result = (info.orientation + degrees) % 360; 
     result = (360 - result) % 360; 
    } 
    else { 
     result = (info.orientation - degrees + 360) % 360; 
    } 

    return result; 
} 
+0

私はあなたの答えに応じて私の質問を更新するが、これはまだ同じ問題を動作していません。私は 'processImage'がどのように働いているのか分かりません。最初にファイルを保存してから画像を処理していますか? –

+0

はい、バイトデータをローカルファイルに保存してから、保存したファイルに回転角度を適用します。ローカルに保存されたイメージファイルを使用します。 – Pehlaj

+0

このコードは複数の端末で完全にテストされており、Googleのアプリはプレイストアで公開されています。あなたは異なるデバイスで異なる向きの問題に直面しますが、これはあなたの問題を解決します – Pehlaj

関連する問題