2017-02-13 96 views

回答

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

試試這個從後攝像機捕獲圖像旋轉。

+0

感謝兄弟的工作 –

+0

okk兄如果我的答案可以接受請投票。 – aj0822ArpitJoshi

2

您可以使用以下方法以實際方向顯示/保存圖像。

public static Bitmap setImage(String filePath, Bitmap bitmap){ 
    File curFile = new File(filePath); 
    Bitmap rotatedBitmap = null; 

    try { 
     ExifInterface exif = new ExifInterface(curFile.getPath()); 
     int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
     int rotationInDegrees = exifToDegrees(rotation); 
     Matrix matrix = new Matrix(); 
     if (rotation != 0.0f) {matrix.preRotate(rotationInDegrees);} 
     rotatedBitmap = Bitmap.createBitmap(bitmap,0,0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 


    }catch(IOException ex){ 
     ex.printStackTrace(); 
    } 
    return rotatedBitmap; 
} 

private static int exifToDegrees(int exifOrientation) { 
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } 
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } 
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } 
    return 0; 
} 
相關問題