2017-03-21 11 views
1

Wenn ich meine Anwendung auf meinem Android One-Telefon oder Emulator selbst teste, ist alles in Ordnung, aber mit einigen anderen Geräten gibt es ein Problem.Wie erkennt man die Kameraausrichtung in Android?

Problem ist, ich schicke grundsätzlich eine Kameraabsicht, bekomme die Daten von Absicht, nachdem Benutzer ein Foto macht, und ich setze die Pixel von ImageView mit allem, was ich von der Kamera bekomme. Bei einigen Geräten (meist Samsung) wird das Bild gedreht, es wird nicht so angezeigt, wie es aufgenommen wurde.

Meine App funktioniert nur im Hochformat, aber der Benutzer kann auch im Querformat fotografieren, wenn er das Telefon dreht, wenn er das Bild aufnimmt.

Gibt es eine Möglichkeit, den Standardwinkel zu erkennen, mit dem das Gerät die Bilder dreht, und so rotiere ich das Bitmap nach der Aufnahme des Bildes? Hier

ist der Code:

Senden Absicht:

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

Erfassung Absicht auf Fragment

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

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

File imageFile = new File(path, mFileName); 

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

Hilfsfunktionen:

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

Antwort

0

Wenn Sie wollen mak Wenn das Kamerabild in der gleichen Ausrichtung wie das Display angezeigt wird, können Sie den folgenden Code verwenden.

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

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

+0

Vielen Dank für die schnelle Antwort. Was soll ich als Argumente zum zweiten und dritten Parameter erstellen und übergeben? –

+0

Können Sie bitte den Code teilen, den Sie geschrieben haben? –

0

Bitte einen Blick -

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: Die Aktivität konnte nicht gestartet werden ComponentInfo java.lang.NullPointerException: Es wurde versucht, die virtuelle Methode 'void android.hardware.Camera.setDisplayOrientation (int)' für eine Nullobjekt-Referenz aufzurufen –

0

ich das Problem behoben eine ExifInterface verwenden. Aber es wäre immer noch großartig, wenn jemand etwas Licht auf das Erkennen des Standardwinkels werfen könnte, der auf Bilder angewendet wird, wenn er ein Bild aufnimmt. Hier

ist der Code:

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