Search in sources :

Example 46 with ExifInterface

use of android.media.ExifInterface in project lzc_app_lib by httplzc.

the class FileUntil method setFilePictureDegree.

public static void setFilePictureDegree(File file, int degree) {
    try {
        ExifInterface exifInterface = new ExifInterface(file.getPath());
        int orientation = ExifInterface.ORIENTATION_NORMAL;
        switch(degree) {
            case 90:
                orientation = ExifInterface.ORIENTATION_ROTATE_90;
                break;
            case 180:
                orientation = ExifInterface.ORIENTATION_ROTATE_180;
                break;
            case 270:
                orientation = ExifInterface.ORIENTATION_ROTATE_270;
                break;
        }
        exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, orientation + "");
        exifInterface.saveAttributes();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ExifInterface(android.media.ExifInterface) IOException(java.io.IOException)

Example 47 with ExifInterface

use of android.media.ExifInterface in project androidquery by androidquery.

the class BitmapAjaxCallback method rotate.

private static Bitmap rotate(String path, Bitmap bm) {
    if (bm == null)
        return null;
    Bitmap result = bm;
    int ori = ExifInterface.ORIENTATION_NORMAL;
    try {
        ExifInterface ei = new ExifInterface(path);
        ori = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    } catch (Exception e) {
        // simply fallback to normal orientation
        AQUtility.debug(e);
    }
    if (ori > 0) {
        Matrix matrix = getRotateMatrix(ori);
        result = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        AQUtility.debug("before", bm.getWidth() + ":" + bm.getHeight());
        AQUtility.debug("after", result.getWidth() + ":" + result.getHeight());
        if (bm != result) {
            bm.recycle();
        }
    }
    return result;
}
Also used : Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) ExifInterface(android.media.ExifInterface) Paint(android.graphics.Paint) IOException(java.io.IOException)

Example 48 with ExifInterface

use of android.media.ExifInterface in project hypertrack-live-android by hypertrack.

the class ImageUtils method getScaledFile.

public static File getScaledFile(Context context, File file) {
    try {
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
        int rotationAngle = 0;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
            rotationAngle = 90;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
            rotationAngle = 180;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
            rotationAngle = 270;
        // BitmapFactory options to downsize the image
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        o.inSampleSize = 6;
        // factor of downsizing the image
        FileInputStream inputStream = new FileInputStream(file);
        // Bitmap selectedBitmap = null;
        BitmapFactory.decodeStream(inputStream, null, o);
        inputStream.close();
        // The new size we want to scale to
        final int REQUIRED_SIZE = 50;
        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE) {
            scale *= 2;
        }
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        inputStream = new FileInputStream(file);
        Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);
        inputStream.close();
        // File file2 = new File();
        // here i override the original image file
        FileOutputStream outputStream = new FileOutputStream(file);
        selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
        return getRotatedBitMap(file, rotationAngle);
    } catch (Exception e) {
        e.printStackTrace();
        CrashlyticsWrapper.log(e);
        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
    }
    return null;
}
Also used : Bitmap(android.graphics.Bitmap) ExifInterface(android.media.ExifInterface) FileOutputStream(java.io.FileOutputStream) BitmapFactory(android.graphics.BitmapFactory) FileInputStream(java.io.FileInputStream)

Example 49 with ExifInterface

use of android.media.ExifInterface in project ImagePickerUtil by himangipatel.

the class FilePickUtils method compressImage.

private String compressImage(String imageUri) {
    String filePath = getRealPathFromURI(imageUri);
    Bitmap scaledBitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    // by setting this field as true, the actual bitmap pixels are not loaded in the memory. Just the bounds are loaded. If
    // you try the use the bitmap here, you will get null.
    options.inJustDecodeBounds = true;
    Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
    /* if (bmp == null) {
            bmp = BitmapFactory.decodeFile(AppUtils.getWorkingDirectory() + "/" + "CROP" + imageUri.split("CROP")[imageUri.split("CROP").length - 1]);
        }
*/
    int actualHeight = options.outHeight;
    int actualWidth = options.outWidth;
    // max Height and width values of the compressed image is taken as 816x612
    float maxHeight = 616.0f;
    float maxWidth = 816.0f;
    float imgRatio = actualWidth / actualHeight;
    float maxRatio = maxWidth / maxHeight;
    // width and height values are set maintaining the aspect ratio of the image
    Log.d("IMAGE", "actualHeight=" + actualHeight + "actualWidth=" + actualWidth + "");
    if (actualHeight > maxHeight || actualWidth > maxWidth) {
        if (imgRatio < maxRatio) {
            imgRatio = maxHeight / actualHeight;
            actualWidth = (int) (imgRatio * actualWidth);
            actualHeight = (int) maxHeight;
        } else if (imgRatio > maxRatio) {
            imgRatio = maxWidth / actualWidth;
            actualHeight = (int) (imgRatio * actualHeight);
            actualWidth = (int) maxWidth;
        } else {
            actualHeight = (int) maxHeight;
            actualWidth = (int) maxWidth;
        }
    }
    // setting inSampleSize value allows to load a scaled down version of the original image
    options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
    // inJustDecodeBounds set to false to load the actual bitmap
    options.inJustDecodeBounds = false;
    // this options allow android to claim the bitmap memory if it runs low on memory
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inTempStorage = new byte[16 * 1024];
    try {
        // load the bitmap from its path
        bmp = BitmapFactory.decodeFile(filePath, options);
    } catch (OutOfMemoryError exception) {
        exception.printStackTrace();
    }
    try {
        scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.RGB_565);
    } catch (OutOfMemoryError exception) {
        exception.printStackTrace();
    }
    float ratioX = actualWidth / (float) options.outWidth;
    float ratioY = actualHeight / (float) options.outHeight;
    float middleX = actualWidth / 2.0f;
    float middleY = actualHeight / 2.0f;
    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
    ExifInterface exif;
    try {
        exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
        Matrix matrix = new Matrix();
        if (orientation == 6) {
            matrix.postRotate(90);
        } else if (orientation == 3) {
            matrix.postRotate(180);
        } else if (orientation == 8) {
            matrix.postRotate(270);
        }
        scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileOutputStream out = null;
    String filename = AppUtils.createImageFile(activity, "").getFile().getAbsolutePath();
    try {
        out = new FileOutputStream(filename);
        scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return filename;
}
Also used : Canvas(android.graphics.Canvas) ExifInterface(android.media.ExifInterface) FileNotFoundException(java.io.FileNotFoundException) Paint(android.graphics.Paint) IOException(java.io.IOException) Paint(android.graphics.Paint) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) FileOutputStream(java.io.FileOutputStream) BitmapFactory(android.graphics.BitmapFactory)

Example 50 with ExifInterface

use of android.media.ExifInterface in project cyborg-core by nu-art.

the class ImageUtilsModule method getImageOrientation.

public final int getImageOrientation(File imageFile) throws IOException {
    ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    switch(orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            return 270;
        case ExifInterface.ORIENTATION_ROTATE_180:
            return 180;
        case ExifInterface.ORIENTATION_ROTATE_90:
            return 90;
        default:
            return 0;
    }
}
Also used : ExifInterface(android.media.ExifInterface)

Aggregations

ExifInterface (android.media.ExifInterface)153 IOException (java.io.IOException)93 Paint (android.graphics.Paint)32 Bitmap (android.graphics.Bitmap)26 InputStream (java.io.InputStream)21 File (java.io.File)17 BitmapFactory (android.graphics.BitmapFactory)16 Matrix (android.graphics.Matrix)15 FileInputStream (java.io.FileInputStream)15 Point (android.graphics.Point)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ErrnoException (android.system.ErrnoException)10 BufferedInputStream (java.io.BufferedInputStream)10 FileDescriptor (java.io.FileDescriptor)10 FileNotFoundException (java.io.FileNotFoundException)10 SuppressLint (android.annotation.SuppressLint)9 FileOutputStream (java.io.FileOutputStream)8 Cursor (android.database.Cursor)7 AssetFileDescriptor (android.content.res.AssetFileDescriptor)6 Bundle (android.os.Bundle)5