簡體   English   中英

如何檢查是否使用Android相機以縱向或橫向拍攝圖像?

[英]How to check whether an image is captured in portrait mode or landscape mode using camera in android?

我正在創建一個打開照片庫的應用,通過從圖庫中選擇該照片,該照片將顯示在另一個活動中。 我的問題是,以人像模式拍攝的照片在顯示后會被旋轉。 但是我以橫向模式拍攝的照片將正確顯示。

因此,我必須檢查是否使用Android相機以縱向模式或橫向模式捕獲圖像,以便旋轉縱向捕獲的照片。 誰能幫我怎么做?

注意:肖像拍攝的圖像和風景拍攝的圖像的寬度和高度均相同。

您始終可以使用Matrix檢查圖像的旋轉並相應地旋轉它。

這段代碼放在onActivityResult->

    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inPurgeable = true;

        Bitmap cameraBitmap = BitmapFactory.decodeFile(filePath);//get file path from intent when you take iamge.
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);


        ExifInterface exif = new ExifInterface(filePath);
        float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  
        System.out.println(rotation);

        float rotationInDegrees = exifToDegrees(rotation);
        System.out.println(rotationInDegrees);

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

        Bitmap scaledBitmap = Bitmap.createBitmap(cameraBitmap);
        Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
        FileOutputStream fos=new FileOutputStream(filePath);
        rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();

OnActivityResult代碼在這里結束。

下面的此函數用於旋轉:-

    private static float exifToDegrees(float 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;    
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM