coomar2841 / image-chooser-library

An Easy Image/Video Chooser Library for your Android Apps
646 stars 193 forks source link

Rotate images #11

Closed rogersala closed 10 years ago

rogersala commented 10 years ago

Hi,

In Galaxy S4, for example, the image appears rotate -90º. I think you should control about rotations, because some devices returns rotated images.

Congrats!

coomar2841 commented 10 years ago

Would look into that when I get hold of an S4. Thanks for creating the issue.

stiggybr0 commented 10 years ago

Same issue on my galaxy s3. The camera takes images in landscape mode no matter what orientation you are holding the phone, so images are rotated 90 if you try to take a portrait picture. This code might help

public static Bitmap checkifImageRotated(File file) {
        ExifInterface exif;
        try {
            exif = new ExifInterface(file.getAbsolutePath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            int rotate = 0;
            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;
            }
            Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(file), null, null);
            if (rotate != 0) {
                Matrix matrix = new Matrix();
                matrix.setRotate(rotate);
                bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);
                return bmp;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
stiggybr0 commented 10 years ago

Check out this link - it looks like it is just samsung devices. http://stackoverflow.com/questions/13245556/exif-orientation-tag-value-always-0-for-image-taken-with-portrait-camera-app-and

The code I posted above is essentially the same thing. I am always getting an orientation of 0, though. Trying to figure out how you are changing the exif data... Looks like in ChosenMedia?

stiggybr0 commented 10 years ago

I was able to fix the rotate issue on my Galaxy S3 by updating my compressAndSaveImage function in MediaProcessorThread.java to the following. This rotates the thumbnails and finished my requirements, but as the original image is never compressed, it doesn't rotate the original. For those of you that need to rotate the original as well, a similar approach with find the exif orientation would be necessary elsewhere as well. Best of luck!


private String compressAndSaveImage(String fileImage, int scale)
            throws Exception {
        try {
            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???");
        }
    }
coomar2841 commented 10 years ago

Thanks for the code. Could you send a pull request, and I can merge it?

coomar2841 commented 10 years ago

Thanks @stiggybr0 I would be closing this issue after some more testing.

coomar2841 commented 10 years ago

Seems to be working ok. Hence closing the issue.