2011-12-01 7 views
22

私はカメラを使用するAndroidアプリケーションを作成しています。 Iが90にカメラディスプレイの向きを設定しています、私の活性が縦向きにある:Androidカメラの画像はキャプチャ後に回転する必要がありますか?

camera.setDisplayOrientation(90); 

Iが十分に配向プレビュー画像を取得していたが、得られた画像を-90度(反時計回り)に回転されますそして

exif.getAttribute(ExifInterface.TAG_ORIENTATION) 

戻り
をORIENTATION_NORMALそれが動作を期待されていますか?キャプチャ後に結果の画像を回転する必要がありますか?

デバイス - ネクサスS、API - 10

+0

同じことが私に起こっています。私はNexus S API 10にもいます。質問のおかげで。 – serkanozel

+0

ここに回答するhttp://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android –

+1

[ Android上の一部のデバイスでカメラの意図を使用するのはなぜですか?](https://stackoverflow.com/questions/14066038/why-does-an-image-captured-using-camera-intent-gets-rotated-on-some-devices -on-a) – bummi

答えて

13

問題は、カメラの向きであり、OEMは、標準に準拠していないため(画像をキャプチャされるように)完全に災害です。 HTCの携帯電話は事を一つの方法、サムスンの携帯電話は別の方法を行う、Nexusのラインはどのベンダー、私はどのハードウェアにもかかわらず、標準に従うと思うCM7ベースのROMを固執しているようだが、あなたはアイデアを得る。あなたは電話/ ROMに基づいて何をすべきかを決定する必要があります。ここでの議論を参照してください:Android camera unexplainable rotation on capture for some devices (not in EXIF)

21

この

try { 
     File f = new File(imagePath); 
     ExifInterface exif = new ExifInterface(f.getPath()); 
     int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

     int angle = 0; 

     if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
      angle = 90; 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { 
      angle = 180; 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
      angle = 270; 
     } 

     Matrix mat = new Matrix(); 
     mat.postRotate(angle); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 2; 

     Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), 
       null, options); 
     bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), 
       bmp.getHeight(), mat, true); 
     ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, 
       outstudentstreamOutputStream); 
     imageView.setImageBitmap(bitmap); 

    } catch (IOException e) { 
     Log.w("TAG", "-- Error in setting image"); 
    } catch (OutOfMemoryError oom) { 
     Log.w("TAG", "-- OOM Error in setting image"); 
    } 

を試してみてくださいそれは私が唯一のポートレートモード用アプリをコード化している

+2

この作業についての確認はありますか? – JoaoFilipeClementeMartins

+2

いいえ、私にとってはうまくいく! – AabidMulani

+2

あなたが結論に達したのは、これがすべてのデバイスでうまくいくかどうか不思議です。 – sudocoder

1
camera.setDisplayOrientation(90); 

動作します。

はカメラが90度まで回転するようになりますと、これは開発者のサイトに査読され、次のコードを使用するすべてのAndroidデバイスのための正しいプレビューを得るためにアンドロイド のすべてのデバイスに適していない可能性があります。あなたがあなたの活動を送信する必要が下

、cameraIdが戻っ0で、フロントカメラ用これは、カメラ

あなたは問題があるかもしれためsetDisplayOrientationを設定するために保存される方法である1

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; 
    //int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
     // do something for phones running an SDK before lollipop 
     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); 
} 

あります=キャプチャされたImage in Correct Orientationは、Android APIのすべてのデバイスをサポートするCamera APIのバグです。 あなたはすべてのデバイスに正しい値を与えることはありません

PLSノートEXIF値以下の手順を使用して克服することができ、これは我々が以前に使用したのと同じ概念を使用することで、あなたに

int CameraEyeValue = setPhotoOrientation(CameraActivity.this, cameraFront==true ? 1:0); // CameraID = 1 : front 0:back 

に役立つだろうDisplayOrientation

public int setPhotoOrientation(Activity activity, int cameraId) { 
    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; 
    // do something for phones running an SDK before lollipop 
    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; 
    } 

    return result; 
} 

だからあなたの最終PictureCallBack方法が

のようになります。
private PictureCallback getPictureCallback() { 
    PictureCallback picture = new PictureCallback() { 

     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      //make a new picture file 
      File pictureFile = getOutputMediaFile(); 

      if (pictureFile == null) { 
       return; 
      } 
      try { 
       //write the file 
       FileOutputStream fos = new FileOutputStream(pictureFile); 
       Bitmap bm=null; 

       // COnverting ByteArray to Bitmap - >Rotate and Convert back to Data 
       if (data != null) { 
        int screenWidth = getResources().getDisplayMetrics().widthPixels; 
        int screenHeight = getResources().getDisplayMetrics().heightPixels; 
        bm = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0); 

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
         // Notice that width and height are reversed 
         Bitmap scaled = Bitmap.createScaledBitmap(bm, screenHeight, screenWidth, true); 
         int w = scaled.getWidth(); 
         int h = scaled.getHeight(); 
         // Setting post rotate to 90 
         Matrix mtx = new Matrix(); 

         int CameraEyeValue = setPhotoOrientation(AndroidCameraExample.this, cameraFront==true ? 1:0); // CameraID = 1 : front 0:back 
         if(cameraFront) { // As Front camera is Mirrored so Fliping the Orientation 
          if (CameraEyeValue == 270) { 
           mtx.postRotate(90); 
          } else if (CameraEyeValue == 90) { 
           mtx.postRotate(270); 
          } 
         }else{ 
           mtx.postRotate(CameraEyeValue); // CameraEyeValue is default to Display Rotation 
         } 

         bm = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, true); 
        }else{// LANDSCAPE MODE 
         //No need to reverse width and height 
         Bitmap scaled = Bitmap.createScaledBitmap(bm, screenWidth, screenHeight, true); 
         bm=scaled; 
        } 
       } 
       // COnverting the Die photo to Bitmap 



       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       bm.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
       byte[] byteArray = stream.toByteArray(); 
       fos.write(byteArray); 
       //fos.write(data); 
       fos.close(); 

       Toast toast = Toast.makeText(myContext, "Picture saved: " + pictureFile.getName(), Toast.LENGTH_LONG); 
       toast.show(); 

      } catch (FileNotFoundException e) { 
      } catch (IOException e) { 
      } 

      //refresh camera to continue preview 
      mPreview.refreshCamera(mCamera); 
      mPreview.setCameraDisplayOrientation(CameraActivity.this,GlobalCameraId,mCamera); 
     } 
    }; 
    return picture; 
} 

フロントとバックカメラを使用したポートレートモードのみの動作画像は、すべてのアンドロイドデバイスで正しいポートレートのオリエンテーションでポートレートモードにのみ回転します。景観のために

あなたが参照としてこれを作成し、以下のブロックの変化

if(cameraFront) { // As Front camera is Mirrored so Fliping the Orientation 
     if (CameraEyeValue == 270) { 
      mtx.postRotate(90); //change Here 
      } else if (CameraEyeValue == 90) { 
      mtx.postRotate(270);//change Here 
      } 
     }else{ 
      mtx.postRotate(CameraEyeValue); // CameraEyeValue is default to Display Rotation //change Here 
     } 
+0

私はキャプチャ画像を常に肖像画にしたいが、ビットマップの回転角度を見つけることができなかった。 –

4

私はあなたのような同じ問題を抱えていたが、私はそれを修正しましたを作ることができます。

Camera.Parameters parameters = camera.getParameters(); 
parameters.setRotation(90); 
camera.setParameters(parameters); 

私はあなたがあまりにもこのコードを使用することを願って:
あなたは同じコードを使用する必要があります。

関連する問題