Example usage for android.media ExifInterface ORIENTATION_NORMAL

List of usage examples for android.media ExifInterface ORIENTATION_NORMAL

Introduction

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

Prototype

int ORIENTATION_NORMAL

To view the source code for android.media ExifInterface ORIENTATION_NORMAL.

Click Source Link

Usage

From source file:openscience.crowdsource.video.experiments.MainActivity.java

/**
 * Rotate an image if required./*  www.j  a  va2  s .  com*/
 *
 * @param selectedImagePath Image URI
 * @return The resulted Bitmap after manipulation
 */
private static int getImageDegree(String selectedImagePath) {

    ExifInterface ei = null;
    try {
        ei = new ExifInterface(selectedImagePath);
    } catch (IOException e) {
        AppLogger.logMessage("Error image could not be rotated " + e.getLocalizedMessage());
    }
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

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

From source file:com.almalence.opencam.SavingService.java

@TargetApi(21)
private void saveDNGPicture(int frameNum, long sessionID, OutputStream os, int width, int height,
        int orientation, boolean cameraMirrored) {
    DngCreator creator = new DngCreator(CameraController.getCameraCharacteristics(),
            PluginManager.getInstance().getFromRAWCaptureResults("captureResult" + frameNum + sessionID));
    byte[] frame = SwapHeap.SwapFromHeap(
            Integer.parseInt(getFromSharedMem("resultframe" + frameNum + Long.toString(sessionID))),
            Integer.parseInt(getFromSharedMem("resultframelen" + frameNum + Long.toString(sessionID))));

    ByteBuffer buff = ByteBuffer.allocateDirect(frame.length);
    buff.put(frame);//from ww w .j av a2s  .c o  m

    int exif_orientation = ExifInterface.ORIENTATION_NORMAL;
    switch ((orientation + 360) % 360) {
    default:
    case 0:
        exif_orientation = ExifInterface.ORIENTATION_NORMAL;
        break;
    case 90:
        exif_orientation = cameraMirrored ? ExifInterface.ORIENTATION_ROTATE_270
                : ExifInterface.ORIENTATION_ROTATE_90;
        break;
    case 180:
        exif_orientation = ExifInterface.ORIENTATION_ROTATE_180;
        break;
    case 270:
        exif_orientation = cameraMirrored ? ExifInterface.ORIENTATION_ROTATE_90
                : ExifInterface.ORIENTATION_ROTATE_270;
        break;
    }

    try {
        creator.setOrientation(exif_orientation);
        creator.writeByteBuffer(os, new Size(width, height), buff, 0);
    } catch (IOException e) {
        creator.close();
        e.printStackTrace();
        Log.e("Open Camera", "saveDNGPicture error: " + e.getMessage());
    }

    creator.close();
}

From source file:me.ccrama.redditslide.Views.SubsamplingScaleImageView.java

/**
 * Helper method for load tasks. Examines the EXIF info on the image file to determine the orientation.
 * This will only work for external files, not assets, resources or other URIs.
 *//*from   www  .ja  va2 s  .com*/
private int getExifOrientation(String sourceUri) {
    int exifOrientation = ORIENTATION_0;
    if (sourceUri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        try {
            final String[] columns = { MediaStore.Images.Media.ORIENTATION };
            final Cursor cursor = getContext().getContentResolver().query(Uri.parse(sourceUri), columns, null,
                    null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    int orientation = cursor.getInt(0);
                    if (VALID_ORIENTATIONS.contains(orientation) && orientation != ORIENTATION_USE_EXIF) {
                        exifOrientation = orientation;
                    } else {
                        Log.w(TAG, "Unsupported orientation: " + orientation);
                    }
                }
                cursor.close();
            }
        } catch (Exception e) {
            Log.w(TAG, "Could not get orientation of image from media store");
        }
    } else if (sourceUri.startsWith(ImageSource.FILE_SCHEME)
            && !sourceUri.startsWith(ImageSource.ASSET_SCHEME)) {
        try {
            ExifInterface exifInterface = new ExifInterface(
                    sourceUri.substring(ImageSource.FILE_SCHEME.length() - 1));
            int orientationAttr = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            if (orientationAttr == ExifInterface.ORIENTATION_NORMAL
                    || orientationAttr == ExifInterface.ORIENTATION_UNDEFINED) {
                exifOrientation = ORIENTATION_0;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_90) {
                exifOrientation = ORIENTATION_90;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_180) {
                exifOrientation = ORIENTATION_180;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_270) {
                exifOrientation = ORIENTATION_270;
            } else {
                Log.w(TAG, "Unsupported EXIF orientation: " + orientationAttr);
            }
        } catch (Exception e) {
            Log.w(TAG, "Could not get EXIF orientation of image");
        }
    }
    return exifOrientation;
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public Object createImage(String path) throws IOException {
    int IMAGE_MAX_SIZE = getDisplayHeight();
    if (exists(path)) {
        Bitmap b = null;/*from  w  w  w.ja  va2s  .c o  m*/
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            o.inPreferredConfig = Bitmap.Config.ARGB_8888;

            InputStream fis = createFileInputStream(path);
            BitmapFactory.decodeStream(fis, null, o);
            fis.close();

            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int) Math.pow(2, (int) Math.round(
                        Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inPreferredConfig = Bitmap.Config.ARGB_8888;

            if (sampleSizeOverride != -1) {
                o2.inSampleSize = sampleSizeOverride;
            } else {
                String sampleSize = Display.getInstance().getProperty("android.sampleSize", null);
                if (sampleSize != null) {
                    o2.inSampleSize = Integer.parseInt(sampleSize);
                } else {
                    o2.inSampleSize = scale;
                }
            }
            o2.inPurgeable = true;
            o2.inInputShareable = true;
            fis = createFileInputStream(path);
            b = BitmapFactory.decodeStream(fis, null, o2);
            fis.close();

            //fix rotation
            ExifInterface exif = new ExifInterface(path);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

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

            if (sampleSizeOverride < 0 && angle != 0) {
                Matrix mat = new Matrix();
                mat.postRotate(angle);
                Bitmap correctBmp = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), mat, true);
                b.recycle();
                b = correctBmp;
            }
        } catch (IOException e) {
        }
        return b;
    } else {
        InputStream in = this.getResourceAsStream(getClass(), path);
        if (in == null) {
            throw new IOException("Resource not found. " + path);
        }
        try {
            return this.createImage(in);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception ignored) {
                    ;
                }
            }
        }
    }
}

From source file:es.javocsoft.android.lib.toolbox.ToolBox.java

/**
 * Corrects the orientation of a Bitmap. Orientation, depending of the device
 * , is not correctly set in the EXIF data of the taken image when it is saved
 * into disk.//from   w w  w  . ja  va  2s .c  om
 * 
 * Explanation:
 *    Camera orientation is not working ok (as is when capturing an image) because 
 *  OEMs do not adhere to the standard. So, each company does this following their 
 *  own way.
 * 
 * @param imagePath   path to the file
 * @return
 */
public static Bitmap media_correctImageOrientation(String imagePath) {
    Bitmap res = null;

    try {
        File f = new File(imagePath);
        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 options = new BitmapFactory.Options();
        options.inSampleSize = 2;

        Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
        res = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);

    } catch (OutOfMemoryError e) {
        if (LOG_ENABLE)
            Log.e(TAG, "media_correctImageOrientation() [OutOfMemory!]: " + e.getMessage(), e);
    } catch (Exception e) {
        if (LOG_ENABLE)
            Log.e(TAG, "media_correctImageOrientation(): " + e.getMessage(), e);
    } catch (Throwable e) {
        if (LOG_ENABLE)
            Log.e(TAG, "media_correctImageOrientation(): " + e.getMessage(), e);
    }

    return res;
}

From source file:kr.wdream.ui.ChatActivity.java

@Override
public void onActivityResultFragment(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 0) {
            PhotoViewer.getInstance().setParentActivity(getParentActivity());
            final ArrayList<Object> arrayList = new ArrayList<>();
            int orientation = 0;
            try {
                ExifInterface ei = new ExifInterface(currentPicturePath);
                int exif = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                switch (exif) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    orientation = 90;//from  ww  w .  j a va  2s .co  m
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    orientation = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    orientation = 270;
                    break;
                }
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            arrayList.add(new MediaController.PhotoEntry(0, 0, 0, currentPicturePath, orientation, false));

            PhotoViewer.getInstance().openPhotoForSelect(arrayList, 0, 2,
                    new PhotoViewer.EmptyPhotoViewerProvider() {
                        @Override
                        public void sendButtonPressed(int index) {
                            sendMedia((MediaController.PhotoEntry) arrayList.get(0), false);
                        }
                    }, this);
            AndroidUtilities.addMediaToGallery(currentPicturePath);
            currentPicturePath = null;
        } else if (requestCode == 1) {
            if (data == null || data.getData() == null) {
                showAttachmentError();
                return;
            }
            Uri uri = data.getData();
            if (uri.toString().contains("video")) {
                String videoPath = null;
                try {
                    videoPath = AndroidUtilities.getPath(uri);
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
                if (videoPath == null) {
                    showAttachmentError();
                }
                if (Build.VERSION.SDK_INT >= 16) {
                    if (paused) {
                        startVideoEdit = videoPath;
                    } else {
                        openVideoEditor(videoPath, false, false);
                    }
                } else {
                    SendMessagesHelper.prepareSendingVideo(videoPath, 0, 0, 0, 0, null, dialog_id,
                            replyingMessageObject, null);
                }
            } else {
                SendMessagesHelper.prepareSendingPhoto(null, uri, dialog_id, replyingMessageObject, null, null);
            }
            showReplyPanel(false, null, null, null, false, true);
            DraftQuery.cleanDraft(dialog_id, true);
        } else if (requestCode == 2) {
            String videoPath = null;
            FileLog.d("tmessages", "pic path " + currentPicturePath);
            if (data != null && currentPicturePath != null) {
                if (new File(currentPicturePath).exists()) {
                    data = null;
                }
            }
            if (data != null) {
                Uri uri = data.getData();
                if (uri != null) {
                    FileLog.d("tmessages", "video record uri " + uri.toString());
                    videoPath = AndroidUtilities.getPath(uri);
                    FileLog.d("tmessages", "resolved path = " + videoPath);
                    if (!(new File(videoPath).exists())) {
                        videoPath = currentPicturePath;
                    }
                } else {
                    videoPath = currentPicturePath;
                }
                AndroidUtilities.addMediaToGallery(currentPicturePath);
                currentPicturePath = null;
            }
            if (videoPath == null && currentPicturePath != null) {
                File f = new File(currentPicturePath);
                if (f.exists()) {
                    videoPath = currentPicturePath;
                }
                currentPicturePath = null;
            }
            if (Build.VERSION.SDK_INT >= 16) {
                if (paused) {
                    startVideoEdit = videoPath;
                } else {
                    openVideoEditor(videoPath, false, false);
                }
            } else {
                SendMessagesHelper.prepareSendingVideo(videoPath, 0, 0, 0, 0, null, dialog_id,
                        replyingMessageObject, null);
                showReplyPanel(false, null, null, null, false, true);
                DraftQuery.cleanDraft(dialog_id, true);
            }
        } else if (requestCode == 21) {
            if (data == null || data.getData() == null) {
                showAttachmentError();
                return;
            }
            Uri uri = data.getData();

            String extractUriFrom = uri.toString();
            if (extractUriFrom.contains("com.google.android.apps.photos.contentprovider")) {
                try {
                    String firstExtraction = extractUriFrom.split("/1/")[1];
                    int index = firstExtraction.indexOf("/ACTUAL");
                    if (index != -1) {
                        firstExtraction = firstExtraction.substring(0, index);
                        String secondExtraction = URLDecoder.decode(firstExtraction, "UTF-8");
                        uri = Uri.parse(secondExtraction);
                    }
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
            }
            String tempPath = AndroidUtilities.getPath(uri);
            String originalPath = tempPath;
            if (tempPath == null) {
                originalPath = data.toString();
                tempPath = MediaController.copyFileToCache(data.getData(), "file");
            }
            if (tempPath == null) {
                showAttachmentError();
                return;
            }
            SendMessagesHelper.prepareSendingDocument(tempPath, originalPath, null, null, dialog_id,
                    replyingMessageObject);
            showReplyPanel(false, null, null, null, false, true);
            DraftQuery.cleanDraft(dialog_id, true);
        } else if (requestCode == 31) {
            if (data == null || data.getData() == null) {
                showAttachmentError();
                return;
            }
            Uri uri = data.getData();
            Cursor c = null;
            try {
                c = getParentActivity().getContentResolver().query(uri, new String[] {
                        ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER },
                        null, null, null);
                if (c != null) {
                    boolean sent = false;
                    while (c.moveToNext()) {
                        sent = true;
                        String name = c.getString(0);
                        String number = c.getString(1);
                        TLRPC.User user = new TLRPC.User();
                        user.first_name = name;
                        user.last_name = "";
                        user.phone = number;
                        SendMessagesHelper.getInstance().sendMessage(user, dialog_id, replyingMessageObject,
                                null, null);
                    }
                    if (sent) {
                        showReplyPanel(false, null, null, null, false, true);
                        DraftQuery.cleanDraft(dialog_id, true);
                    }
                }
            } finally {
                try {
                    if (c != null && !c.isClosed()) {
                        c.close();
                    }
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
            }
        }
    }
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public com.codename1.ui.util.ImageIO getImageIO() {
    if (imIO == null) {
        imIO = new com.codename1.ui.util.ImageIO() {
            @Override/*from ww  w  . j  av a 2  s . c om*/
            public Dimension getImageSize(String imageFilePath) throws IOException {
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                o.inPreferredConfig = Bitmap.Config.ARGB_8888;

                InputStream fis = createFileInputStream(imageFilePath);
                BitmapFactory.decodeStream(fis, null, o);
                fis.close();

                ExifInterface exif = new ExifInterface(imageFilePath);

                // if the image is in portrait mode
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);
                if (orientation == ExifInterface.ORIENTATION_ROTATE_90
                        || orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    return new Dimension(o.outHeight, o.outWidth);
                }
                return new Dimension(o.outWidth, o.outHeight);
            }

            private Dimension getImageSizeNoRotation(String imageFilePath) throws IOException {
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                o.inPreferredConfig = Bitmap.Config.ARGB_8888;

                InputStream fis = createFileInputStream(imageFilePath);
                BitmapFactory.decodeStream(fis, null, o);
                fis.close();

                return new Dimension(o.outWidth, o.outHeight);
            }

            @Override
            public void save(InputStream image, OutputStream response, String format, int width, int height,
                    float quality) throws IOException {
                Bitmap.CompressFormat f = Bitmap.CompressFormat.PNG;
                if (format == FORMAT_JPEG) {
                    f = Bitmap.CompressFormat.JPEG;
                }
                Image img = Image.createImage(image).scaled(width, height);
                Bitmap b = (Bitmap) img.getImage();
                b.compress(f, (int) (quality * 100), response);
            }

            @Override
            public String saveAndKeepAspect(String imageFilePath, String preferredOutputPath, String format,
                    int width, int height, float quality, boolean onlyDownscale, boolean scaleToFill)
                    throws IOException {
                ExifInterface exif = new ExifInterface(imageFilePath);
                Dimension d = getImageSizeNoRotation(imageFilePath);
                if (onlyDownscale) {
                    if (scaleToFill) {
                        if (d.getHeight() <= height || d.getWidth() <= width) {
                            return imageFilePath;
                        }
                    } else {
                        if (d.getHeight() <= height && d.getWidth() <= width) {
                            return imageFilePath;
                        }
                    }
                }

                float ratio = ((float) d.getWidth()) / ((float) d.getHeight());
                int heightBasedOnWidth = (int) (((float) width) / ratio);
                int widthBasedOnHeight = (int) (((float) height) * ratio);
                if (scaleToFill) {
                    if (heightBasedOnWidth >= width) {
                        height = heightBasedOnWidth;
                    } else {
                        width = widthBasedOnHeight;
                    }
                } else {
                    if (heightBasedOnWidth > width) {
                        width = widthBasedOnHeight;
                    } else {
                        height = heightBasedOnWidth;
                    }
                }
                sampleSizeOverride = Math.max(d.getWidth() / width, d.getHeight() / height);
                OutputStream im = FileSystemStorage.getInstance().openOutputStream(preferredOutputPath);
                Image i = Image.createImage(imageFilePath);
                Image newImage = i.scaled(width, height);
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

                int angle = 0;
                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    angle = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    angle = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    angle = 270;
                    break;
                }
                if (angle != 0) {
                    Matrix mat = new Matrix();
                    mat.postRotate(angle);
                    Bitmap b = (Bitmap) newImage.getImage();
                    Bitmap correctBmp = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), mat, true);
                    b.recycle();
                    newImage.dispose();
                    Image tmp = Image.createImage(correctBmp);
                    newImage = tmp;
                    save(tmp, im, format, quality);
                } else {
                    save(imageFilePath, im, format, width, height, quality);
                }
                sampleSizeOverride = -1;
                return preferredOutputPath;
            }

            @Override
            public void save(String imageFilePath, OutputStream response, String format, int width, int height,
                    float quality) throws IOException {
                Image i = Image.createImage(imageFilePath);
                Image newImage = i.scaled(width, height);
                save(newImage, response, format, quality);
                newImage.dispose();
                i.dispose();
            }

            @Override
            protected void saveImage(Image img, OutputStream response, String format, float quality)
                    throws IOException {
                Bitmap.CompressFormat f = Bitmap.CompressFormat.PNG;
                if (format == FORMAT_JPEG) {
                    f = Bitmap.CompressFormat.JPEG;
                }
                Bitmap b = (Bitmap) img.getImage();
                b.compress(f, (int) (quality * 100), response);
            }

            @Override
            public boolean isFormatSupported(String format) {
                return format == FORMAT_JPEG || format == FORMAT_PNG;
            }
        };
    }
    return imIO;
}