6

In meiner Anwendung muss ich Bild von der Kamera erfassen oder aus der Galerie importieren, zeigen Sie es in Bildansicht in Aktivität. Alles ist in Ordnung, ich bekomme Bild von beiden und kann es auf imageview ohne Ausnahme einstellen. Aber manchmal wird das Bild nicht richtig skaliert und wird vertikal gestreckt oder mit Ausrichtung verändert. Bitte hilf mir.Bild von der Kamera oder Galerie bei der Verwendung in Bildansicht seine Ausrichtung wird verändert, und manchmal vertikal gestreckt in Android

Hier ist mein Code Bild von den offiziellen Android-Dokumentation bezeichnet zu dekodieren:

public static Bitmap decodeSampledBitmapFromResource(File photoFile, int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    try { 
     BitmapFactory.decodeStream(new FileInputStream(photoFile), null, 
       options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 

     return BitmapFactory.decodeStream(new FileInputStream(photoFile), 
       null, options); 

    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return null; 
    } 
} 

public static int calculateInSampleSize(BitmapFactory.Options options, 
     int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     // Calculate ratios of height and width to requested height and 
     // width 
     final int heightRatio = Math.round((float) height 
       /(float) reqHeight); 
     final int widthRatio = Math.round((float) width/(float) reqWidth); 

     // Choose the smallest ratio as inSampleSize value, this will 
     // guarantee 
     // a final image with both dimensions larger than or equal to the 
     // requested height and width. 
     inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
    } 

    return inSampleSize; 
} 

Antwort

7

Die Bilder haben unterschiedliche Orientierungen, so dass es entsprechend die Ausrichtung dreht, wenn auf Imageview setzen. Sie können die Ausrichtung des Fotos anhand der Bildeigenschaften überprüfen. Um das Bild zu setzen in der richtigen Art und Weise Sie den folgenden Code verwenden können ...

 int rot=getCameraPhotoOrientation(this,Uri,picturePath); 
     if(rot!=0) 
     bitmap=new RotateOrientation().RotateOrientationCall(bitmap,rot); 

Die getCameraPhotoOrientation Methode: -

public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){ 
    int rotate = 0; 
    try { 
     context.getContentResolver().notifyChange(imageUri, null); 
     File imageFile = new File(imagePath); 
     ExifInterface exif = new ExifInterface(
       imageFile.getAbsolutePath()); 
     int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

     switch (orientation) { 
     case ExifInterface.ORIENTATION_ROTATE_270: 
      rotate = 270; 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_180: 
      rotate = 180; 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      rotate = 90; 
      break; 
     } 


     Log.d(TAG, "Exit orientation: " + orientation); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return rotate; 
} 

RotateOrientation Klasse hinzufügen Klasse drehen Orientierung nach.

public class RotateOrientation { 

Bitmap RotateOrientationCall(Bitmap src,float degree) 
     { 


     Matrix matrix=new Matrix(); 
     matrix.postRotate(degree); 
     Bitmap bmOut = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); 
     return bmOut; 

     } 
      } 
+0

Ich werde es versuchen, und lassen Sie wissen ... @ Abhishek – Prashant

+0

ok es und voteup überprüfen, ob es u hilfreich .. –

+0

Dank abhishek, es funktioniert fine..I've Code getestet meine App. – Prashant