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 getImageRotationAngle(Uri imageUri, ContentResolver contentResolver) throws IOException {
    int angle = 0;
    Cursor cursor = contentResolver.query(imageUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
            null, null, null);//w ww  . ja v  a  2 s  .  c o  m
    if (cursor != null) {
        if (cursor.getCount() == 1) {
            cursor.moveToFirst();
            angle = cursor.getInt(0);
        }
        cursor.close();
    } else {
        ExifInterface exif = new ExifInterface(imageUri.getPath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            angle = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            angle = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            angle = 90;
            break;
        default:
            break;
        }
    }
    return angle;
}

From source file:mobisocial.noteshere.util.UriImage.java

public static float rotationForImage(Context context, Uri uri) {
    if (uri.getScheme().equals("content")) {
        String[] projection = { Images.ImageColumns.ORIENTATION };
        Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        try {//  www. j  av  a 2 s.c  o m
            if (c.moveToFirst()) {
                return c.getInt(0);
            }
        } finally {
            c.close();
        }
    } else if (uri.getScheme().equals("file")) {
        try {
            ExifInterface exif = new ExifInterface(uri.getPath());
            int rotation = (int) exifOrientationToDegrees(
                    exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
            return rotation;
        } catch (IOException e) {
            Log.e(TAG, "Error checking exif", e);
        }
    }
    return 0f;
}

From source file:com.owncloud.android.services.AdvancedFileAlterationListener.java

public void onFileCreate(final File file, int delay) {
    if (file != null) {
        uploadMap.put(file.getAbsolutePath(), null);

        String mimetypeString = FileStorageUtils.getMimeTypeFromName(file.getAbsolutePath());
        Long lastModificationTime = file.lastModified();
        final Locale currentLocale = context.getResources().getConfiguration().locale;

        if ("image/jpeg".equalsIgnoreCase(mimetypeString) || "image/tiff".equalsIgnoreCase(mimetypeString)) {
            try {
                ExifInterface exifInterface = new ExifInterface(file.getAbsolutePath());
                String exifDate = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
                if (!TextUtils.isEmpty(exifDate)) {
                    ParsePosition pos = new ParsePosition(0);
                    SimpleDateFormat sFormatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", currentLocale);
                    sFormatter.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
                    Date dateTime = sFormatter.parse(exifDate, pos);
                    lastModificationTime = dateTime.getTime();
                }/*from   w  ww.j  ava2 s.c  o m*/

            } catch (IOException e) {
                Log_OC.d(TAG, "Failed to get the proper time " + e.getLocalizedMessage());
            }
        }

        final Long finalLastModificationTime = lastModificationTime;

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                PersistableBundleCompat bundle = new PersistableBundleCompat();
                bundle.putString(AutoUploadJob.LOCAL_PATH, file.getAbsolutePath());
                bundle.putString(AutoUploadJob.REMOTE_PATH,
                        FileStorageUtils.getInstantUploadFilePath(currentLocale, syncedFolder.getRemotePath(),
                                file.getName(), finalLastModificationTime, syncedFolder.getSubfolderByDate()));
                bundle.putString(AutoUploadJob.ACCOUNT, syncedFolder.getAccount());
                bundle.putInt(AutoUploadJob.UPLOAD_BEHAVIOUR, syncedFolder.getUploadAction());

                new JobRequest.Builder(AutoUploadJob.TAG).setExecutionWindow(30_000L, 80_000L)
                        .setRequiresCharging(syncedFolder.getChargingOnly())
                        .setRequiredNetworkType(syncedFolder.getWifiOnly() ? JobRequest.NetworkType.UNMETERED
                                : JobRequest.NetworkType.ANY)
                        .setExtras(bundle).setPersisted(false).setRequirementsEnforced(true)
                        .setUpdateCurrent(false).build().schedule();

                uploadMap.remove(file.getAbsolutePath());
            }
        };

        uploadMap.put(file.getAbsolutePath(), runnable);
        handler.postDelayed(runnable, delay);
    }
}

From source file:com.exzogeni.dk.graphics.Bitmaps.java

private static int getExifOrientation(String filePath) {
    try {//from   w  ww. jav  a  2 s.c  om
        return new ExifInterface(filePath).getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
    } catch (IOException e) {
        Logger.error(e);
    }
    return ExifInterface.ORIENTATION_UNDEFINED;
}

From source file:com.haru.ui.image.workers.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {/*from  w  ww  .j ava  2  s.c om*/
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (/* TODO: DEBUG */ true) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (/* TODO: DEBUG */ true) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (/* TODO: DEBUG */ true) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (/* TODO: DEBUG */ true) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}

From source file:com.cars.manager.utils.imageChooser.threads.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {// ww  w. j a  va 2  s. com
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (Config.DEBUG) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (Config.DEBUG) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (Config.DEBUG) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (Config.DEBUG) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}

From source file:at.wada811.imageslider.MainActivity.java

private int getOrientationFromExif(String filePath) {
    try {/*from ww w  .  j a  va 2 s .c om*/
        ExifInterface exifInterface = new ExifInterface(filePath);
        int orientationAttr = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        LogUtils.d("orientationAttr: " + orientationAttr);
        switch (orientationAttr) {
        case ExifInterface.ORIENTATION_NORMAL:
            return 0;
        case ExifInterface.ORIENTATION_ROTATE_90:
            return 90;
        case ExifInterface.ORIENTATION_ROTATE_180:
            return 180;
        case ExifInterface.ORIENTATION_ROTATE_270:
            return 270;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:com.dastardlylabs.ti.ocrdroid.OcrdroidModule.java

@Kroll.method
@SuppressWarnings("rawtypes")
public String ocr(HashMap _config) {
    assert (_config != null);
    String dataParentPath = getTessDataDirectory().getAbsolutePath(), //= (String) _config.get("dataParent"),
            imagePath = StorageHelper.stripFileUri((String) _config.get("image")),
            language = (String) _config.get("lang");

    try {/*from  w w w. j a  v a  2s.  c o  m*/
        if (!tessDataExists())
            unpackTessData();
    } catch (Exception e) {
        // catch failure and bubble up failure message and options
        // else continue.
    }

    Log.d(LCAT, "ocr called");
    Log.d(LCAT, "Setting parent directory for tessdata as DATAPATH".replace("DATAPATH",
            dataParentPath /*DATA_PATH*/));

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
    //bitmap.getConfig()

    try {
        ExifInterface exif = new ExifInterface(imagePath);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        Log.v(LCAT, "Orient: " + exifOrientation);

        int rotate = 0;
        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        }

        Log.v(LCAT, "Rotation: " + rotate);

        if (rotate != 0) {

            // Getting width & height of the given image.
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            // Setting pre rotate
            Matrix mtx = new Matrix();
            mtx.preRotate(rotate);

            // Rotating Bitmap
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
            // tesseract req. ARGB_8888
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        }

    } catch (IOException e) {
        Log.e(LCAT, "Rotate or coversion failed: " + e.toString());
    }

    //        ImageView iv = (ImageView) findViewById(R.id.image);
    //        iv.setImageBitmap(bitmap);
    //        iv.setVisibility(View.VISIBLE);

    Log.v(LCAT, "Before baseApi");

    TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.setDebug(true);
    baseApi.init(dataParentPath /*DATA_PATH*/, language);
    baseApi.setImage(bitmap);
    //baseApi.get
    String recognizedText = baseApi.getUTF8Text();
    baseApi.end();

    Log.v(LCAT, "OCR Result: " + recognizedText);

    // clean up and show
    if (language.equalsIgnoreCase("eng")) {
        recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " ");
    }

    return recognizedText.trim();
}

From source file:com.owncloud.android.utils.BitmapUtils.java

/**
 * Rotate bitmap according to EXIF orientation. 
 * Cf. http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/ 
 * @param bitmap Bitmap to be rotated//from   w  w  w  .  j a v  a  2s . c  om
 * @param storagePath Path to source file of bitmap. Needed for EXIF information.
 * @return correctly EXIF-rotated bitmap
 */
public static Bitmap rotateImage(final Bitmap bitmap, final String storagePath) {
    try {
        ExifInterface exifInterface = new ExifInterface(storagePath);
        final int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Matrix matrix = new Matrix();
        // 1: nothing to do

        switch (orientation) {
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.postScale(-1.0f, 1.0f);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.postScale(1.0f, -1.0f);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.postRotate(-90);
            matrix.postScale(1.0f, -1.0f);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.postRotate(90);
            matrix.postScale(1.0f, -1.0f);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(270);
            break;
        }

        // Rotate the bitmap
        final Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                matrix, true);
        if (resultBitmap != bitmap) {
            bitmap.recycle();
        }
        return resultBitmap;
    } catch (Exception exception) {
        Log_OC.e("BitmapUtil", "Could not rotate the image: " + storagePath);
        return bitmap;
    }
}

From source file:com.oxgcp.photoList.PhotolistModule.java

@Kroll.method
public KrollDict getExifData(String fileName) {
    try {/*ww  w . ja v a  2s  .c o  m*/
        ExifInterface exif = new ExifInterface(fileName);

        HashMap<String, String> tag = new HashMap<String, String>();

        tag.put("hasThumbnail", exif.hasThumbnail() ? "true" : null);
        tag.put("height", exif.getAttribute("ImageLength"));
        tag.put("width", exif.getAttribute("ImageWidth"));
        tag.put("alt", exif.getAttribute("GPSAltitude"));
        tag.put("altRef", exif.getAttribute("GPSAltitudeRef"));
        tag.put("lat", exif.getAttribute("GPSLatitude"));
        tag.put("latRef", exif.getAttribute("GPSLatitudeRef"));
        tag.put("lon", exif.getAttribute("GPSLongitude"));
        tag.put("lonRef", exif.getAttribute("GPSLongitudeRef"));
        tag.put("date", exif.getAttribute("GPSDateStamp"));
        tag.put("time", exif.getAttribute("GPSTimeStamp"));

        return new KrollDict(tag);//new JSONObject(tag).toString();
    } catch (Exception e) {
        return null;
    }
}