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:com.daiv.android.twitter.ui.compose.Compose.java

private Bitmap getThumbnail(Uri uri) throws IOException {
    InputStream input = getContentResolver().openInputStream(uri);
    int reqWidth = 150;
    int reqHeight = 150;

    byte[] byteArr = new byte[0];
    byte[] buffer = new byte[1024];
    int len;//from  w w w .ja va  2 s  .  co  m
    int count = 0;

    try {
        while ((len = input.read(buffer)) > -1) {
            if (len != 0) {
                if (count + len > byteArr.length) {
                    byte[] newbuf = new byte[(count + len) * 2];
                    System.arraycopy(byteArr, 0, newbuf, 0, count);
                    byteArr = newbuf;
                }

                System.arraycopy(buffer, 0, byteArr, count, len);
                count += len;
            }
        }

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(byteArr, 0, count, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        Bitmap b = BitmapFactory.decodeByteArray(byteArr, 0, count, options);

        ExifInterface exif = new ExifInterface(IOUtils.getPath(uri, context));
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        b = ImageUtils.cropSquare(b);

        return rotateBitmap(b, orientation);

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

        return null;
    }
}

From source file:com.mk4droid.IMC_Activities.Fragment_NewIssueA.java

/**
 *        Check Image Orientation  //from  ww w  .java 2 s  .c o m
 */
public void CheckOrient() {

    BitmapFactory.Options options = new BitmapFactory.Options(); // Resize is needed otherwize outofmemory exception
    options.inSampleSize = 6;

    //------------- read tmp file ------------------ 
    Image_BMP = BitmapFactory.decodeFile(image_path_source_temp, options); // , options

    //---------------- find exif header --------
    ExifInterface exif;
    String exifOrientation = "0"; // 0 = exif not working
    try {
        exif = new ExifInterface(image_path_source_temp);
        exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    //---------------- Resize ---------------------
    if (exifOrientation.equals("0")) {
        if (Image_BMP.getWidth() < Image_BMP.getHeight() && Image_BMP.getWidth() > 400) {
            Image_BMP = Bitmap.createScaledBitmap(Image_BMP, 400, 640, true); // <- To sent
        } else if (Image_BMP.getWidth() > Image_BMP.getHeight() && Image_BMP.getWidth() > 640) {
            Image_BMP = Bitmap.createScaledBitmap(Image_BMP, 640, 400, true); // <- To sent
        }
    } else {

        if (exifOrientation.equals("1") && Image_BMP.getWidth() > 640) { // normal

            Image_BMP = Bitmap.createScaledBitmap(Image_BMP, 640, 400, true); // <- To sent

        } else if (exifOrientation.equals("6") && Image_BMP.getWidth() > 400) { // rotated 90 degrees

            // Rotate
            Matrix matrix = new Matrix();

            int bmwidth = Image_BMP.getWidth();
            int bmheight = Image_BMP.getHeight();

            matrix.postRotate(90);

            Image_BMP = Bitmap.createBitmap(Image_BMP, 0, 0, bmwidth, bmheight, matrix, true);

            Image_BMP = Bitmap.createScaledBitmap(Image_BMP, 400, 640, true); // <- To sent
        }
    }

    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

    //------------ now store as jpg over the temp jpg
    File imagef = new File(image_path_source_temp);
    try {
        Image_BMP.compress(Bitmap.CompressFormat.JPEG, 95, new FileOutputStream(imagef));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:eu.nubomedia.nubomedia_kurento_health_communicator_android.kc_and_communicator.util.FileUtils.java

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from w  ww . j a  v  a  2s . co  m
    BitmapFactory.decodeFile(path, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;

    Bitmap b = BitmapFactory.decodeFile(path, options);

    ExifInterface exif;
    try {
        exif = new ExifInterface(path);
    } catch (IOException e) {
        return null;
    }
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    Matrix matrix = new Matrix();
    if (orientation == 6)
        matrix.postRotate(90);
    else if (orientation == 3)
        matrix.postRotate(180);
    else if (orientation == 8)
        matrix.postRotate(270);

    return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
}

From source file:com.daiv.android.twitter.ui.compose.Compose.java

public Bitmap getBitmapToSend(Uri uri) throws IOException {
    InputStream input = getContentResolver().openInputStream(uri);
    int reqWidth = 750;
    int reqHeight = 750;

    byte[] byteArr = new byte[0];
    byte[] buffer = new byte[1024];
    int len;/*  w ww  .ja  v  a2 s  . com*/
    int count = 0;

    try {
        while ((len = input.read(buffer)) > -1) {
            if (len != 0) {
                if (count + len > byteArr.length) {
                    byte[] newbuf = new byte[(count + len) * 2];
                    System.arraycopy(byteArr, 0, newbuf, 0, count);
                    byteArr = newbuf;
                }

                System.arraycopy(buffer, 0, byteArr, count, len);
                count += len;
            }
        }

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(byteArr, 0, count, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        Bitmap b = BitmapFactory.decodeByteArray(byteArr, 0, count, options);

        ExifInterface exif = new ExifInterface(IOUtils.getPath(uri, context));
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        input.close();

        return rotateBitmap(b, orientation);

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

        return null;
    }
}

From source file:com.cloverstudio.spika.CameraCropActivity.java

protected void onPhotoTaken(String path) {

    String fileName = Uri.parse(path).getLastPathSegment();
    mFilePath = CameraCropActivity.this.getExternalCacheDir() + "/" + fileName;

    if (!path.equals(mFilePath)) {
        copy(new File(path), new File(mFilePath));
    }/*from  www.j a v a  2  s .  c om*/

    new AsyncTask<String, Void, byte[]>() {
        boolean loadingFailed = false;

        @Override
        protected byte[] doInBackground(String... params) {
            try {

                if (params == null)
                    return null;

                File f = new File(params[0]);
                ExifInterface exif = new ExifInterface(f.getPath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                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;
                }

                Matrix mat = new Matrix();
                mat.postRotate(angle);

                BitmapFactory.Options optionsMeta = new BitmapFactory.Options();
                optionsMeta.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(f.getAbsolutePath(), optionsMeta);

                BitmapFactory.Options options = new BitmapFactory.Options();

                options.inSampleSize = BitmapManagement.calculateInSampleSize(optionsMeta, 640, 640);
                options.inPurgeable = true;
                options.inInputShareable = true;
                mBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
                mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mat,
                        true);
                _scaleBitmap();
                return null;
            } catch (Exception ex) {
                loadingFailed = true;
                finish();
            }

            return null;
        }

        @Override
        protected void onPostExecute(byte[] result) {
            super.onPostExecute(result);

            if (null != mBitmap) {
                mImageView.setImageBitmap(mBitmap);
                mImageView.setScaleType(ScaleType.MATRIX);
                translateMatrix.setTranslate(-(mBitmap.getWidth() - crop_container_size) / 2f,
                        -(mBitmap.getHeight() - crop_container_size) / 2f);
                mImageView.setImageMatrix(translateMatrix);

                matrix = translateMatrix;

            }

        }
    }.execute(mFilePath);

}

From source file:com.nextgis.ngm_clink_monitoring.fragments.ObjectStatusFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    File tempPhotoFile = new File(mTempPhotoPath);

    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
        GISApplication app = (GISApplication) getActivity().getApplication();
        ContentResolver contentResolver = app.getContentResolver();
        String photoFileName = getPhotoFileName();

        try {//from w  ww .j a  v  a2s  .  co m
            BitmapUtil.writeLocationToExif(tempPhotoFile, app.getCurrentLocation(), 0);
        } catch (IOException e) {
            Log.d(TAG, e.getLocalizedMessage());
        }

        Uri allAttachesUri = Uri.parse("content://" + FoclSettingsConstantsUI.AUTHORITY + "/" + mObjectLayerName
                + "/" + mObjectId + "/attach");

        ContentValues values = new ContentValues();
        values.put(VectorLayer.ATTACH_DISPLAY_NAME, photoFileName);
        values.put(VectorLayer.ATTACH_MIME_TYPE, "image/jpeg");
        //values.put(VectorLayer.ATTACH_DESCRIPTION, photoFileName);

        Uri attachUri = null;
        try {
            attachUri = contentResolver.insert(allAttachesUri, values);

        } catch (Exception e) {
            Log.d(TAG, e.getLocalizedMessage());
        }

        if (null != attachUri) {

            try {
                int exifOrientation = BitmapUtil.getOrientationFromExif(tempPhotoFile);

                // resize and rotate
                Bitmap sourceBitmap = BitmapFactory.decodeFile(tempPhotoFile.getPath());
                Bitmap resizedBitmap = BitmapUtil.getResizedBitmap(sourceBitmap,
                        FoclConstants.PHOTO_MAX_SIZE_PX, FoclConstants.PHOTO_MAX_SIZE_PX);
                Bitmap rotatedBitmap = BitmapUtil.rotateBitmap(resizedBitmap, exifOrientation);

                // jpeg compress
                File tempAttachFile = File.createTempFile("attach", null, app.getCacheDir());
                OutputStream tempOutStream = new FileOutputStream(tempAttachFile);
                rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, FoclConstants.PHOTO_JPEG_COMPRESS_QUALITY,
                        tempOutStream);
                tempOutStream.close();

                int newHeight = rotatedBitmap.getHeight();
                int newWidth = rotatedBitmap.getWidth();

                rotatedBitmap.recycle();

                // write EXIF to new file
                BitmapUtil.copyExifData(tempPhotoFile, tempAttachFile);

                ExifInterface attachExif = new ExifInterface(tempAttachFile.getCanonicalPath());

                attachExif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + ExifInterface.ORIENTATION_NORMAL);
                attachExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, "" + newHeight);
                attachExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, "" + newWidth);

                attachExif.saveAttributes();

                // attach data from tempAttachFile
                OutputStream attachOutStream = contentResolver.openOutputStream(attachUri);
                FoclFileUtil.copy(new FileInputStream(tempAttachFile), attachOutStream);
                attachOutStream.close();

                tempAttachFile.delete();

            } catch (IOException e) {
                Toast.makeText(getActivity(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }

            Log.d(TAG, attachUri.toString());

        } else {
            Log.d(TAG, "insert attach failed");
        }

        try {
            if (app.isOriginalPhotoSaving()) {
                File origPhotoFile = new File(getDailyPhotoFolder(), photoFileName);

                if (!com.nextgis.maplib.util.FileUtil.move(tempPhotoFile, origPhotoFile)) {
                    Toast.makeText(getActivity(), "Save original photo failed", Toast.LENGTH_LONG).show();
                }

            } else {
                tempPhotoFile.delete();
            }

            setPhotoGalleryAdapter();
            setPhotoGalleryVisibility(true);

        } catch (IOException e) {
            Toast.makeText(getActivity(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    }

    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_CANCELED) {
        tempPhotoFile.delete();
    }
}

From source file:mobisocial.bento.ebento.ui.EditFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (DEBUG)/*from   ww w.  ja v a  2s  .c om*/
        Log.d(TAG, "requestCode: " + requestCode + ", resultCode: " + resultCode);
    if (resultCode == Activity.RESULT_OK) {
        try {
            File imageFile = null;

            if (requestCode == REQUEST_IMAGE_CAPTURE) {

                imageFile = JpgFileHelper.getTmpFile();

            } else if (requestCode == REQUEST_GALLERY) {
                Uri uri = data.getData();
                if (uri == null || uri.toString().length() == 0) {
                    return;
                }
                if (DEBUG)
                    Log.d(TAG, "data URI: " + uri.toString());

                ContentResolver cr = getActivity().getContentResolver();
                String[] columns = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
                Cursor c = cr.query(uri, columns, null, null, null);

                if (c != null && c.moveToFirst()) {
                    if (c.getString(0) != null) {
                        //regular processing for gallery files
                        imageFile = new File(c.getString(0));
                    } else {
                        final InputStream is = getActivity().getContentResolver().openInputStream(uri);
                        imageFile = JpgFileHelper.saveTmpFile(is);
                        is.close();
                    }
                } else if (uri.toString().startsWith("content://com.android.gallery3d.provider")) {
                    // motorola xoom doesn't work for contentresolver even if the image comes from picasa.
                    // So just adds the condition of startsWith...
                    // See in detail : http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/
                    uri = Uri.parse(
                            uri.toString().replace("com.android.gallery3d", "com.google.android.gallery3d"));
                    final InputStream is = getActivity().getContentResolver().openInputStream(uri);
                    imageFile = JpgFileHelper.saveTmpFile(is);
                    is.close();
                } else {
                    // http or https
                    HttpURLConnection http = null;
                    URL url = new URL(uri.toString());
                    http = (HttpURLConnection) url.openConnection();
                    http.setRequestMethod("GET");
                    http.connect();

                    final InputStream is = http.getInputStream();
                    imageFile = JpgFileHelper.saveTmpFile(is);
                    is.close();
                    if (http != null)
                        http.disconnect();
                }
            }

            if (imageFile.exists() && imageFile.length() > 0) {
                if (DEBUG)
                    Log.d(TAG, "imageFile exists=" + imageFile.exists() + " length=" + imageFile.length()
                            + " path=" + imageFile.getPath());

                float degrees = 0;
                try {
                    ExifInterface exif = new ExifInterface(imageFile.getPath());
                    switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL)) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        degrees = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        degrees = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        degrees = 270;
                        break;
                    default:
                        degrees = 0;
                        break;
                    }
                    Log.d(TAG, exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                } catch (IOException e) {
                    e.printStackTrace();
                }

                sImageOrg = BitmapHelper.getResizedBitmap(imageFile, BitmapHelper.MAX_IMAGE_WIDTH,
                        BitmapHelper.MAX_IMAGE_HEIGHT, degrees);
                mbImageChanged = true;

                // ImageView
                mImageView.setImageBitmap(
                        BitmapHelper.getResizedBitmap(sImageOrg, mTargetWidth, mTargetHeight, 0));
                mImageView.setVisibility(View.VISIBLE);
                mRootView.invalidate();

                imageFile.delete();
                JpgFileHelper.deleteTmpFile();
            }

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

From source file:org.openmrs.mobile.activities.addeditpatient.AddEditPatientFragment.java

private Bitmap getPortraitImage(String imagePath) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;//from w w  w . j  a v a2  s . c  om
    Bitmap photo = BitmapFactory.decodeFile(output.getPath(), options);
    float rotateAngle;
    try {
        ExifInterface exifInterface = new ExifInterface(imagePath);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotateAngle = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotateAngle = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotateAngle = 90;
            break;
        default:
            rotateAngle = 0;
            break;
        }
        return rotateImage(photo, rotateAngle);
    } catch (IOException e) {
        logger.e(e.getMessage());
        return photo;
    }
}

From source file:com.snappy.CameraCropActivity.java

protected void onPhotoTaken(final String path) {

    String fileName = Uri.parse(path).getLastPathSegment();
    mFilePath = CameraCropActivity.this.getExternalCacheDir() + "/" + fileName;

    if (!path.equals(mFilePath)) {
        copy(new File(path), new File(mFilePath));
    }/*w  w  w  . j  a v  a 2  s.c  o  m*/

    new AsyncTask<String, Void, byte[]>() {
        boolean loadingFailed = false;

        @Override
        protected byte[] doInBackground(String... params) {
            try {

                if (!path.equals(mFilePath)) {
                    copy(new File(path), new File(mFilePath));
                }

                if (params == null)
                    return null;

                File f = new File(params[0]);
                ExifInterface exif = new ExifInterface(f.getPath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                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;
                }

                Matrix mat = new Matrix();
                mat.postRotate(angle);

                BitmapFactory.Options optionsMeta = new BitmapFactory.Options();
                optionsMeta.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(f.getAbsolutePath(), optionsMeta);

                BitmapFactory.Options options = new BitmapFactory.Options();

                options.inSampleSize = BitmapManagement.calculateInSampleSize(optionsMeta, 640, 640);
                options.inPurgeable = true;
                options.inInputShareable = true;
                mBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
                mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mat,
                        true);
                _scaleBitmap();
                return null;
            } catch (Exception ex) {
                loadingFailed = true;
                finish();
            }

            return null;
        }

        @Override
        protected void onPostExecute(byte[] result) {
            super.onPostExecute(result);

            if (null != mBitmap) {
                mImageView.setImageBitmap(mBitmap);
                mImageView.setScaleType(ScaleType.MATRIX);
                translateMatrix.setTranslate(-(mBitmap.getWidth() - crop_container_size) / 2f,
                        -(mBitmap.getHeight() - crop_container_size) / 2f);
                mImageView.setImageMatrix(translateMatrix);

                matrix = translateMatrix;

            }

        }
    }.execute(mFilePath);

}

From source file:com.bai.android.ui.OtherActivity.java

private static int getOrientationFromFile(String imagePath) {
    int orientation = ExifInterface.ORIENTATION_NORMAL;
    int rotation;
    // get orientation of file
    try {/*w w w  . ja va2s .  c  o  m*/
        ExifInterface exif = new ExifInterface(imagePath);
        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // set the rotation from the file orientation
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotation = 90;
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotation = 270;
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotation = 180;
        break;
    default:
        rotation = ExifInterface.ORIENTATION_NORMAL;
        break;
    }
    return rotation;
}