Example usage for android.media ExifInterface ExifInterface

List of usage examples for android.media ExifInterface ExifInterface

Introduction

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

Prototype

public ExifInterface(InputStream inputStream) throws IOException 

Source Link

Document

Reads Exif tags from the specified image input stream.

Usage

From source file:Main.java

private static int getRotation(String fileName) {
    try {//  w ww .  j  a  v a 2s . c o  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  w  w  w .  j  a  va  2 s.co  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 compressFileIfNeeded(String filePath) {
    File f = new File(filePath);

    Bitmap bitmap;// w  w w  .ja  v  a2  s.  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();
        }
    }
}

From source file:Main.java

public static int readPictureDegree(String path) {
    int degree = 0;
    try {/*from   w w  w . j  ava2  s .c  o m*/
        ExifInterface exifInterface = new ExifInterface(path);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            degree = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degree = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degree = 270;
            break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return degree;
}

From source file:com.angrystone.JpegExifReader.java

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    PluginResult.Status status = PluginResult.Status.OK;
    Integer result = 0;/*from   w  w  w.j  a va 2s  .  c  om*/

    if (action.equals("getWidth")) {
        String file = null;
        try {
            file = args.getString(0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            ExifInterface exif = new ExifInterface(file);
            result = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else if (action.equals("getLength")) {
        String file = null;
        try {
            file = args.getString(0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            ExifInterface exif = new ExifInterface(file);
            result = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        status = PluginResult.Status.INVALID_ACTION;
    }
    return new PluginResult(status, result);
}

From source file:Main.java

public static int getOrientation(String path) {
    try {//from w  w  w  .  j  a  va  2s .  c om
        ExifInterface exif = new ExifInterface(path);

        return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static String getImageOrientation(String filepath) {
    if (filepath == null) {
        Log.e(TAG, "GPrintCommon : getImageOrientation() : filepath is null!");
        return unknown;
    }/* w  ww. jav  a  2s .c o m*/

    int imageWidth, imageHeight;

    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (exif != null && exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0) > 0) {
        imageWidth = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 1);
        imageHeight = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 1);
    } else {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        Bitmap image = BitmapFactory.decodeFile(filepath, options);
        if (image == null) {
            Log.e(TAG, "GPrintCommon : getImageOrientation() : image is invalid. " + filepath);
            return unknown;
        }

        imageWidth = image.getWidth();
        imageHeight = image.getHeight();
    }

    if (imageWidth > imageHeight) {
        return landscape;
    }

    return portrait;
}

From source file:Main.java

private static Bitmap decodeExifBitmap(File file, Bitmap src) {
    try {/* ww w  .  j  a  v  a 2  s. c  o m*/
        // Try to load the bitmap as a bitmap file
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        if (orientation == ExifInterface.ORIENTATION_UNDEFINED
                || orientation == ExifInterface.ORIENTATION_NORMAL) {
            return src;
        }
        Matrix matrix = new Matrix();
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(90);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(180);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(270);
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            matrix.setScale(-1, 1);
            matrix.postTranslate(src.getWidth(), 0);
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            matrix.setScale(1, -1);
            matrix.postTranslate(0, src.getHeight());
        }
        // Rotate the bitmap
        return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
    } catch (IOException e) {
        // Ignore
    }
    return src;
}

From source file:Main.java

public static int getExifOrientation(String filepath) {
    int degree = 0;
    ExifInterface exif = null;/*from w w  w .  j  a v a2s .c o  m*/
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException ex) {
        Log.e(TAG, "cannot read exif", ex);
    }
    if (exif != null) {
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            // We only recognize a subset of orientation tag values.
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
            }

        }
    }
    return degree;
}

From source file:Main.java

public static int readPictureDegree(String filename) {
    short degree = 0;
    try {/*  w  w w.j  a v  a 2 s .  c om*/
        ExifInterface exifInterface = new ExifInterface(filename);
        int anInt = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        switch (anInt) {
        case ExifInterface.ORIENTATION_ROTATE_180:
            degree = 180;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
        case ExifInterface.ORIENTATION_TRANSPOSE:
        case ExifInterface.ORIENTATION_TRANSVERSE:
        default:
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            degree = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degree = 270;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return degree;
}