Example usage for android.media ExifInterface ORIENTATION_ROTATE_270

List of usage examples for android.media ExifInterface ORIENTATION_ROTATE_270

Introduction

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

Prototype

int ORIENTATION_ROTATE_270

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

Click Source Link

Usage

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   w  ww  . j a va2 s . c o  m*/
            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;
}