Example usage for android.media ExifInterface getAttribute

List of usage examples for android.media ExifInterface getAttribute

Introduction

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

Prototype

public String getAttribute(String tag) 

Source Link

Document

Returns the value of the specified tag or null if there is no such tag in the image file.

Usage

From source file:Main.java

public static boolean hasGpsTag(String file) {
    try {/*from  ww w  . jav a  2s  . c o m*/
        ExifInterface exifInterface = new ExifInterface(file);
        // north and south
        if (exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF) == null)
            return false;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        return false;
    }
    return true;
}

From source file:Main.java

public static int getExifRotation(String imgPath) {
    try {/*  www.  ja  v a  2 s  .  co  m*/
        ExifInterface exif = new ExifInterface(imgPath);
        String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        if (!TextUtils.isEmpty(rotationAmount)) {
            int rotationParam = Integer.parseInt(rotationAmount);
            switch (rotationParam) {
            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;
            default:
                return 0;
            }
        } else {
            return 0;
        }
    } catch (Exception ex) {
        return 0;
    }
}

From source file:Main.java

public static Date getExifDate(File imgFile) throws IOException {
    ExifInterface imgFileExif = new ExifInterface(imgFile.getCanonicalPath());

    if (imgFileExif.getAttribute(ExifInterface.TAG_DATETIME) != null) {
        String imgDateTime = imgFileExif.getAttribute(ExifInterface.TAG_DATETIME);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", Locale.US);

        try {//  www  .  j a  v a2s. c  o  m
            return simpleDateFormat.parse(imgDateTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    return null;
}

From source file:Main.java

public static Bitmap rotateBitmapFromExif(String filePath, Bitmap bitmap) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from www . j  a v a 2 s. c om
    BitmapFactory.decodeFile(filePath, options);

    String orientString;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    } catch (IOException e) {
        e.printStackTrace();
        return bitmap;
    }

    int orientation = (orientString != null) ? Integer.parseInt(orientString)
            : ExifInterface.ORIENTATION_NORMAL;
    int rotationAngle = 0, outHeight = 0, outWidth = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotationAngle = 90;
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotationAngle = 180;
        outHeight = bitmap.getHeight();
        outWidth = bitmap.getWidth();
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotationAngle = 270;
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
        break;
    }

    if (rotationAngle == 0) {
        return bitmap;
    }

    Matrix matrix = new Matrix();
    matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, outWidth, outHeight, matrix, true);
    if (bitmap != rotateBitmap) {
        bitmap.recycle();
    }

    return rotateBitmap;
}

From source file:Main.java

public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null)
        return false;
    try {//w w  w. ja v  a  2 s .  c  o  m
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION,
                exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:Main.java

/**
 * Load the exif tags into the passed Bundle
 *
 * @param filepath// w w w.  j a v  a  2  s  .c  o m
 * @param out
 * @return true if exif tags are loaded correctly
 */
public static boolean loadAttributes(final String filepath, Bundle out) {
    ExifInterface e;
    try {
        e = new ExifInterface(filepath);
    } catch (IOException e1) {
        e1.printStackTrace();
        return false;
    }

    for (String tag : EXIF_TAGS) {
        out.putString(tag, e.getAttribute(tag));
    }
    return true;
}

From source file:Main.java

public static void copyExifData(File srcImgFile, File dstImgFile) throws IOException {
    ExifInterface srcExif = new ExifInterface(srcImgFile.getCanonicalPath());
    ExifInterface dstExif = new ExifInterface(dstImgFile.getCanonicalPath());

    int buildSDKVersion = Build.VERSION.SDK_INT;

    // From API 11
    if (buildSDKVersion >= Build.VERSION_CODES.HONEYCOMB) {
        if (srcExif.getAttribute(ExifInterface.TAG_APERTURE) != null) {
            dstExif.setAttribute(ExifInterface.TAG_APERTURE, srcExif.getAttribute(ExifInterface.TAG_APERTURE));
        }/*from  w  w  w .  j  a v a  2s  . com*/
        if (srcExif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME) != null) {
            dstExif.setAttribute(ExifInterface.TAG_EXPOSURE_TIME,
                    srcExif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME));
        }
        if (srcExif.getAttribute(ExifInterface.TAG_ISO) != null) {
            dstExif.setAttribute(ExifInterface.TAG_ISO, srcExif.getAttribute(ExifInterface.TAG_ISO));
        }
    }

    // From API 9
    if (buildSDKVersion >= Build.VERSION_CODES.GINGERBREAD) {
        if (srcExif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE) != null) {
            dstExif.setAttribute(ExifInterface.TAG_GPS_ALTITUDE,
                    srcExif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE));
        }
        if (srcExif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF) != null) {
            dstExif.setAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF,
                    srcExif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF));
        }
    }

    // From API 8
    if (buildSDKVersion >= Build.VERSION_CODES.FROYO) {
        if (srcExif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH) != null) {
            dstExif.setAttribute(ExifInterface.TAG_FOCAL_LENGTH,
                    srcExif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH));
        }
        if (srcExif.getAttribute(ExifInterface.TAG_GPS_DATESTAMP) != null) {
            dstExif.setAttribute(ExifInterface.TAG_GPS_DATESTAMP,
                    srcExif.getAttribute(ExifInterface.TAG_GPS_DATESTAMP));
        }
        if (srcExif.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD) != null) {
            dstExif.setAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD,
                    srcExif.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD));
        }
        if (srcExif.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP) != null) {
            dstExif.setAttribute(ExifInterface.TAG_GPS_TIMESTAMP,
                    srcExif.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP));
        }
    }

    if (srcExif.getAttribute(ExifInterface.TAG_DATETIME) != null) {
        dstExif.setAttribute(ExifInterface.TAG_DATETIME, srcExif.getAttribute(ExifInterface.TAG_DATETIME));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_FLASH) != null) {
        dstExif.setAttribute(ExifInterface.TAG_FLASH, srcExif.getAttribute(ExifInterface.TAG_FLASH));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) != null) {
        dstExif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,
                srcExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF) != null) {
        dstExif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF,
                srcExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE) != null) {
        dstExif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,
                srcExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF) != null) {
        dstExif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF,
                srcExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH) != null) {
        dstExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH,
                srcExif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH) != null) {
        dstExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH,
                srcExif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_MAKE) != null) {
        dstExif.setAttribute(ExifInterface.TAG_MAKE, srcExif.getAttribute(ExifInterface.TAG_MAKE));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_MODEL) != null) {
        dstExif.setAttribute(ExifInterface.TAG_MODEL, srcExif.getAttribute(ExifInterface.TAG_MODEL));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_ORIENTATION) != null) {
        dstExif.setAttribute(ExifInterface.TAG_ORIENTATION,
                srcExif.getAttribute(ExifInterface.TAG_ORIENTATION));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_WHITE_BALANCE) != null) {
        dstExif.setAttribute(ExifInterface.TAG_WHITE_BALANCE,
                srcExif.getAttribute(ExifInterface.TAG_WHITE_BALANCE));
    }

    dstExif.saveAttributes();
}

From source file:com.stfalcon.contentmanager.ContentManager.java

/**
 * Some devices return wrong rotated image so we can fix it by this method
 *//*  ww w.  j ava  2s .com*/
public static void fixImageRatation(Uri uri, Bitmap realImage) {
    File pictureFile = new File(uri.getPath());

    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        ExifInterface exif = new ExifInterface(pictureFile.toString());

        if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")) {
            realImage = rotate(realImage, 90);
        } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")) {
            realImage = rotate(realImage, 270);
        } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")) {
            realImage = rotate(realImage, 180);
        }

        boolean bo = realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);

        fos.close();

        Log.d("Info", bo + "");

    } catch (FileNotFoundException e) {
        Log.d("Info", "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d("TAG", "Error accessing file: " + e.getMessage());
    }
}

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 w  w.  j  a va  2s . c om

            } 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.nextgis.mobile.forms.CameraFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    sensorManager.unregisterListener(sensorListener);
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        //add angle
        float dfAngle = 0;
        try {/*from ww  w. j a  v a  2s  . c om*/
            ExifInterface exif = new ExifInterface(imgFile.getPath());
            String sDate = exif.getAttribute(ExifInterface.TAG_DATETIME);
            Date datetime = stringToDate(sDate, "yyyy:MM:dd HH:mm:ss");
            Log.d(TAG, "image date: " + datetime.toString());
            long testMilli = datetime.getTime();

            long nDif = 1000000;
            for (long n : mAngles.keySet()) {
                long nDifTmp = Math.abs(testMilli - n);
                if (nDifTmp < nDif) {
                    nDif = nDifTmp;
                    dfAngle = (Float) mAngles.get(n);
                }
            }
            Log.d(TAG, "image angle: " + dfAngle);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String listItem;
        if (data != null) {
            Uri outPath = data.getData();
            listItem = outPath.getLastPathSegment();
        } else {
            listItem = fileName;
        }
        HashMap<String, Object> hm = new HashMap<String, Object>();
        hm.put(IMG_NAME, listItem);
        String sAngle = CompassFragment.formatNumber(dfAngle, 0, 0) + CompassFragment.DEGREE_CHAR + " "
                + CompassFragment.getDirectionCode(dfAngle, getResources());
        hm.put(IMG_ROT, sAngle);

        listItems.add(hm);

        adapter.notifyDataSetChanged();

        InputPointActivity activity = (InputPointActivity) getActivity();
        if (activity == null)
            return;
        activity.AddImage(listItem, dfAngle);
    }
}