Example usage for android.media ExifInterface TAG_ORIENTATION

List of usage examples for android.media ExifInterface TAG_ORIENTATION

Introduction

In this page you can find the example usage for android.media ExifInterface TAG_ORIENTATION.

Prototype

String TAG_ORIENTATION

To view the source code for android.media ExifInterface TAG_ORIENTATION.

Click Source Link

Document

Type is int.

Usage

From source file:Main.java

/**
 * To avoid problems with rotated videos retrieved from camera
 * @param bitmap/*from  w  w w  .j  a v  a 2 s .  c  om*/
 * @param filePath
 * @return
 */
public static Bitmap rotateImage(Bitmap bitmap, String filePath) {
    Bitmap resultBitmap = bitmap;

    try {
        ExifInterface exifInterface = new ExifInterface(filePath);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Matrix matrix = new Matrix();

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(ExifInterface.ORIENTATION_ROTATE_90);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(ExifInterface.ORIENTATION_ROTATE_180);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(ExifInterface.ORIENTATION_ROTATE_270);
        }

        // Rotate the bitmap
        resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } catch (Exception exception) {
        Log.d("AndroidTouchGallery", "Could not rotate the image");
    }
    return resultBitmap;
}

From source file:Main.java

public static void fixBitmapRotationExif(String filePath, Activity activityForScreenOrientation) {
    try {//from  w w  w. ja v a2  s .c o  m
        ExifInterface exif = new ExifInterface(filePath);

        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        if (exifOrientation == ExifInterface.ORIENTATION_UNDEFINED
                && Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).contains("htc"))
            return;

        boolean flippedHorizontally = false, flippedVertically = false;

        int angle = 0;

        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle += 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle += 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle += 270;
        } else if (exifOrientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            flippedHorizontally = true;
        } else if (exifOrientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            flippedVertically = true;
        } else if (exifOrientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            angle += 90;
            flippedVertically = true;
        } else if (exifOrientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            angle -= 90;
            flippedVertically = true;
        }

        int orientation;

        if (activityForScreenOrientation != null) {
            orientation = getScreenOrientation(activityForScreenOrientation);
            if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                angle += 90;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                angle += 180;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                angle += 270;
            }
        }

        orientation = 0;
        angle = angle % 360;

        if (angle == -90 && flippedVertically && !flippedHorizontally) {
            orientation = ExifInterface.ORIENTATION_TRANSVERSE;
        } else if (angle == -270 && flippedVertically && !flippedHorizontally) {
            orientation = ExifInterface.ORIENTATION_TRANSPOSE;
        } else if (angle == -90 && !flippedVertically && flippedHorizontally) {
            orientation = ExifInterface.ORIENTATION_TRANSPOSE;
        } else if (angle == -270 && !flippedVertically && flippedHorizontally) {
            orientation = ExifInterface.ORIENTATION_TRANSVERSE;
        } else {
            while (angle < 0) {
                angle += 360;
            }
            switch (angle) {
            case 0:
                if (flippedHorizontally) {
                    orientation = ExifInterface.ORIENTATION_FLIP_HORIZONTAL;
                } else if (flippedVertically) {
                    orientation = ExifInterface.ORIENTATION_FLIP_VERTICAL;
                }
                break;
            case 90:
                orientation = ExifInterface.ORIENTATION_ROTATE_90;
                break;
            case 180:
                orientation = ExifInterface.ORIENTATION_ROTATE_180;
                break;
            case 270:
                orientation = ExifInterface.ORIENTATION_ROTATE_270;
                break;
            }
        }

        if (orientation != exifOrientation) {
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, ((Integer) orientation).toString());
            exif.saveAttributes();
        }
    } catch (IOException e) {
        Log.w("TAG", "-- Error in setting image");
    }
}

From source file:Main.java

public static int getCameraPhotoOrientation(Uri imageUri) {
    int rotate = 0;
    try {//from w  w w.  j  a  v  a2  s .c o m
        File imageFile = new File(imageUri.getPath());
        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;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

From source file:Main.java

public static Bitmap downSampleBitmap(Uri uri, Activity act, Boolean needRotate) {
    DisplayMetrics displaymetrics = new DisplayMetrics();
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    Resources r = act.getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics()); // 50: magic num
    int targetWidth = displaymetrics.heightPixels;
    int targetHeight = displaymetrics.widthPixels - px;

    Bitmap resizedBitmap = decodeSampledBitmap(uri, targetWidth, targetHeight, act);
    Bitmap returnBitmap = null;/*w  w  w  .  jav  a2  s  . com*/
    ExifInterface exif;
    try {
        float degree = 0;
        exif = new ExifInterface(uri.toString());
        int orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        if (resizedBitmap != null && needRotate) {
            degree = getDegree(orient);
            if (degree != 0) {
                returnBitmap = createRotatedBitmap(resizedBitmap, degree);
            }
            returnBitmap = returnBitmap == null ? resizedBitmap : returnBitmap;
        }
    } catch (IOException e) {
        Log.v(TAG, "not found file at downsample");
        e.printStackTrace();
    }
    return returnBitmap;
}

From source file:Main.java

private static int getPhotoRotationDegree(String imagePath) {
    int rotate = 0;
    try {/*w  w  w.j av  a  2  s.c  o m*/
        File imageFile = new File(imagePath);
        ExifInterface exifDataReader = new ExifInterface(imageFile.getAbsolutePath());
        int orientation = exifDataReader.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;
        }
    } catch (IOException e) {
        Log.e("CATROID", "Could not find file to initialize ExifInterface.", e);
    }
    return rotate;
}

From source file:Main.java

public static Bitmap getBitmapByFixingRotationForFile(String filePath, Bitmap sourceBitmap,
        Activity activityForScreenOrientation, boolean freeSourceBitmap) {
    try {/*from   w  w  w .  j  a v  a2  s.c o m*/
        ExifInterface exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        if (orientation == ExifInterface.ORIENTATION_UNDEFINED
                && Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).contains("htc"))
            return null;

        boolean flippedHorizontally = false, flippedVertically = false;

        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;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            flippedHorizontally = true;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            angle += 90;
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            angle -= 90;
            flippedVertically = true;
        }

        if (activityForScreenOrientation != null) {
            orientation = getScreenOrientation(activityForScreenOrientation);
            if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                angle += 90;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                angle += 180;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                angle += 270;
            }
        }

        Bitmap bmp = sourceBitmap;
        if (bmp == null) {
            bmp = BitmapFactory.decodeFile(filePath, null);
        }
        if (angle != 0) {
            Matrix mat = new Matrix();
            mat.postRotate(angle);

            if (flippedHorizontally) {
                mat.postScale(-1.f, 1.f);
            }
            if (flippedVertically) {
                mat.postScale(1.f, -1.f);
            }

            Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
            if (freeSourceBitmap || bmp != sourceBitmap) {
                bmp.recycle();
            }
            bmp = rotated;
        }

        return bmp;

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

    return null;
}

From source file:Main.java

private static int getRotation(String fileName) {
    try {//from  w w  w.java2  s.co  m
        File imageCaptureFile = new File(fileName);
        ExifInterface exif = new ExifInterface(imageCaptureFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
            return 90;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
            return 180;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
            return 270;
    } catch (IOException e) {
        Log.e(TAG, "Failed to read rotation from file: " + fileName, e);
    }
    return 0;
}

From source file:Main.java

public static int getImageDegrees(String pathName) {
    int degrees = 0;
    try {//from   ww  w.j  a v a  2  s.c o m
        ExifInterface exifInterface = new ExifInterface(pathName);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            degrees = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degrees = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degrees = 270;
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return degrees;
}

From source file:Main.java

public static void removeExif(String path) {
    if (!TextUtils.isEmpty(path)) {
        return;//from w w  w .  java2s.co m
    }
    ExifInterface exifInterface;
    try {
        exifInterface = new ExifInterface(path);
    } catch (IOException ignore) {
        return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        exifInterface.setAttribute(ExifInterface.TAG_ARTIST, "");
        exifInterface.setAttribute(ExifInterface.TAG_RESOLUTION_UNIT, "0");
        exifInterface.setAttribute(ExifInterface.TAG_DATETIME_ORIGINAL, "");
        exifInterface.setAttribute(ExifInterface.TAG_MAKER_NOTE, "0");
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        exifInterface.setAttribute(ExifInterface.TAG_DATETIME_DIGITIZED, "");
    }
    exifInterface.setAttribute(ExifInterface.TAG_MAKE, "");
    exifInterface.setAttribute(ExifInterface.TAG_MODEL, "");
    exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, "0");

    exifInterface.setAttribute(ExifInterface.TAG_DATETIME, "");
    exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE, "");
    exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, "");

    exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE, "");

}

From source file:Main.java

public static void compressFileIfNeeded(String filePath) {
    File f = new File(filePath);

    Bitmap bitmap;/*from  ww w . j av a  2s .c o  m*/
    bitmap = BitmapFactory.decodeFile(filePath);
    int MAX_IMAGE_SIZE = 1000 * 1024;
    int streamLength = (int) f.length();
    if (streamLength > MAX_IMAGE_SIZE) {
        int compressQuality = 105;
        ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
        while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
            try {
                bmpStream.flush();//to avoid out of memory error
                bmpStream.reset();
            } catch (IOException e) {
                e.printStackTrace();
            }
            compressQuality -= 5;
            bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
            byte[] bmpPicByteArray = bmpStream.toByteArray();
            streamLength = bmpPicByteArray.length;
            Log.d("test upload", "Quality: " + compressQuality);
            Log.d("test upload", "Size: " + streamLength);
        }

        FileOutputStream fo;

        try {
            f.delete();
            f = new File(filePath);
            fo = new FileOutputStream(f);
            fo.write(bmpStream.toByteArray());
            fo.flush();
            fo.close();
            ExifInterface exif = new ExifInterface(f.getAbsolutePath());
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, "6");
            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}