Esempio n. 1
0
        public ExifData(string filename)
        {
            var exif = new ExifInterface(filename);

            _rawData = new Dictionary <string, object>
            {
                { ExifInterface.TagAperture, exif.GetAttributeDouble(ExifInterface.TagAperture, 1) },
                { ExifInterface.TagDatetime, exif.GetAttribute(ExifInterface.TagDatetime) },
                { ExifInterface.TagExposureTime, exif.GetAttribute(ExifInterface.TagExposureTime) },
                { ExifInterface.TagFlash, exif.GetAttribute(ExifInterface.TagFlash) },
                { ExifInterface.TagFocalLength, exif.GetAttributeDouble(ExifInterface.TagFocalLength, 0) },
                { ExifInterface.TagGpsAltitude, exif.GetAttribute(ExifInterface.TagGpsAltitude) },
                { ExifInterface.TagGpsAltitudeRef, exif.GetAttribute(ExifInterface.TagGpsAltitudeRef) },
                { ExifInterface.TagGpsDatestamp, exif.GetAttribute(ExifInterface.TagGpsDatestamp) },
                { ExifInterface.TagGpsLatitude, exif.GetAttribute(ExifInterface.TagGpsLatitude) },
                { ExifInterface.TagGpsLatitudeRef, exif.GetAttribute(ExifInterface.TagGpsLatitudeRef) },
                { ExifInterface.TagGpsLongitude, exif.GetAttribute(ExifInterface.TagGpsLongitude) },
                { ExifInterface.TagGpsLongitudeRef, exif.GetAttribute(ExifInterface.TagGpsLongitudeRef) },
                { ExifInterface.TagGpsProcessingMethod, exif.GetAttribute(ExifInterface.TagGpsProcessingMethod) },
                { ExifInterface.TagGpsTimestamp, exif.GetAttribute(ExifInterface.TagGpsTimestamp) },
                { ExifInterface.TagImageLength, exif.GetAttribute(ExifInterface.TagImageLength) },
                { ExifInterface.TagImageWidth, exif.GetAttribute(ExifInterface.TagImageWidth) },
                { ExifInterface.TagIso, exif.GetAttribute(ExifInterface.TagIso) },
                { ExifInterface.TagMake, exif.GetAttribute(ExifInterface.TagMake) },
                { ExifInterface.TagModel, exif.GetAttribute(ExifInterface.TagModel) },
                { ExifInterface.TagOrientation, exif.GetAttributeInt(ExifInterface.TagOrientation, -1) },
                { ExifInterface.TagWhiteBalance, exif.GetAttributeInt(ExifInterface.TagWhiteBalance, 0) },
            };
        }
        static int GetRotation(ExifInterface exif)
        {
            try
            {
                var orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, ExifInterface.OrientationNormal);

                switch (orientation)
                {
                case ExifInterface.OrientationRotate90:
                    return(90);

                case ExifInterface.OrientationRotate180:
                    return(180);

                case ExifInterface.OrientationRotate270:
                    return(270);

                default:
                    return(0);
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                throw ex;
#else
                return(0);
#endif
            }
        }
        private Matrix GetMatrix(string imageAbsoluePath)
        {
            ExifInterface ei          = new ExifInterface(imageAbsoluePath);
            int           orientation = ei.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Normal);
            int           rotate      = 0;

            switch (orientation)
            {
            case (int)Android.Media.Orientation.Rotate270:
                rotate = 270;
                break;

            case (int)Android.Media.Orientation.Rotate180:
                rotate = 180;
                break;

            case (int)Android.Media.Orientation.Rotate90:
                rotate = 90;
                break;
            }

            Matrix matrix = new Matrix();

            matrix.PreRotate(rotate);

            GC.Collect();

            return(matrix);
        }
Esempio n. 4
0
        private int GetRotation(string fileName)
        {
            var rotate = 0;

            if (!String.IsNullOrEmpty(fileName))
            {
                ExifInterface exif = new ExifInterface(fileName);
                //int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                var orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Normal);
                switch (orientation)
                {
                case (int)Android.Media.Orientation.Rotate270:
                    rotate = 270;
                    break;

                case (int)Android.Media.Orientation.Rotate180:
                    rotate = 180;
                    break;

                case (int)Android.Media.Orientation.Rotate90:
                    rotate = 90;
                    break;
                }
            }
            return(rotate);
        }
Esempio n. 5
0
        /// <summary>
        /// Получение Bitmap по пути до файла
        /// </summary>
        /// <param name="path">путь к файлу</param>
        /// <returns></returns>
        private Bitmap GetBitmap(string path)
        {
            byte[]        imageArray  = System.IO.File.ReadAllBytes(path);
            ExifInterface exif        = new ExifInterface(path);
            int           orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, 1);
            Matrix        matrix      = new Matrix();

            switch (orientation)
            {
            case 3:
                matrix.PostRotate(180);
                break;

            case 6:
                matrix.PostRotate(90);
                break;

            case 8:
                matrix.PostRotate(270);
                break;
            }

            Bitmap bitmap = BitmapFactory.DecodeByteArray(imageArray, 0, imageArray.Length);

            return(Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true));
        }
Esempio n. 6
0
        public static int CameraPhotoOrientation(Context context, Android.Net.Uri imageUri, String imagePath)
        {
            int rotate = 0;

            try
            {
                context.ContentResolver.NotifyChange(imageUri, null);
                Java.IO.File  imageFile = new Java.IO.File(imagePath);
                ExifInterface exif      = new ExifInterface(imageFile.AbsolutePath);
                //int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                var orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Normal);
                switch (orientation)
                {
                case (int)Android.Media.Orientation.Rotate270:
                    rotate = 270;
                    break;

                case (int)Android.Media.Orientation.Rotate180:
                    rotate = 180;
                    break;

                case (int)Android.Media.Orientation.Rotate90:
                    rotate = 90;
                    break;
                }
                System.Diagnostics.Debug.WriteLine("Orientation for file " + imagePath + " is " + rotate);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception during Camera orientations" + e.StackTrace + e.ToString());
            }
            return(rotate);
        }
Esempio n. 7
0
        int NeededRotation(Java.IO.File ff)
        {
            try
            {
                // extract the header info
                ExifInterface exif = new ExifInterface(ff.AbsolutePath);

                // determine how we should rotate the image
                int orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, 0);
                switch (orientation)
                {
                case 3: return(180);

                case 6: return(90);

                case 8: return(270);
                }

                return(0);
            }
            catch (Exception)
            {
            }

            return(0);
        }
Esempio n. 8
0
        public static int GetExifRotationDegrees(this string filePath)
        {
            int rotation     = 0;
            var exifInt      = new ExifInterface(filePath);
            int exifRotation = exifInt.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal);

            switch (exifRotation)
            {
            case (int)Orientation.Rotate270:
                rotation = 270;
                break;

            case (int)Orientation.Rotate180:
                rotation = 180;
                break;

            case (int)Orientation.Rotate90:
                rotation = 90;
                break;

            default:
                return(0);
            }

            return(rotation);
        }
Esempio n. 9
0
        public static int GetExifRotationDegrees(this Stream stream)
        {
            int rotation = 0;
            var exifInt  = new ExifInterface(stream);

            int exifRotation = exifInt.GetAttributeInt(ExifInterface.TagOrientation, ExifInterface.OrientationNormal);

            switch (exifRotation)
            {
            case ExifInterface.OrientationRotate270:
                rotation = 270;
                break;

            case ExifInterface.OrientationRotate180:
                rotation = 180;
                break;

            case ExifInterface.OrientationRotate90:
                rotation = 90;
                break;

            default:
                return(0);
            }

            return(rotation);
        }
Esempio n. 10
0
        static int GetRotation(ExifInterface exif)
        {
            if (exif == null)
            {
                return(0);
            }
            try
            {
                var orientation = (Orientation)exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal);

                switch (orientation)
                {
                case Orientation.Rotate90:
                    return(90);

                case Orientation.Rotate180:
                    return(180);

                case Orientation.Rotate270:
                    return(270);

                default:
                    return(0);
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                throw ex;
#else
                return(0);
#endif
            }
        }
Esempio n. 11
0
        public int GetRotation(ExifInterface exif)
        {
            try
            {
                var orientation = (Android.Media.Orientation)exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Normal);

                switch (orientation)
                {
                case Android.Media.Orientation.Rotate90:
                    return(90);

                case Android.Media.Orientation.Rotate180:
                    return(180);

                case Android.Media.Orientation.Rotate270:
                    return(270);

                default:
                    return(0);
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex);
                return(0);
            }
        }
Esempio n. 12
0
        public static int?GetRotation(string filePath)
        {
            try
            {
                ExifInterface ei          = new ExifInterface(filePath);
                int           orientation = Convert.ToInt16(ei.GetAttributeInt(ExifInterface.TagOrientation, (int)0));
                switch (orientation)
                {
                case 6:
                    return(90);

                case 3:
                    return(180);

                case 8:
                    return(270);

                default:
                    return(null);
                }
            }
            catch (Exception ex)
            {
                //  ex.report();
                return(null);
            }
        }
Esempio n. 13
0
        private static Bitmap FixOrientation(Bitmap bitmap, string fileName)
        {
            ExifInterface exif        = new ExifInterface(fileName);
            int           orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, 1);

            int rotationDegrees = 0;

            switch (orientation)
            {
            case 6:
                rotationDegrees = 90;
                break;

            case 3:
                rotationDegrees = 180;
                break;

            case 8:
                rotationDegrees = 270;
                break;
            }

            Matrix matrix = new Matrix();

            matrix.PostRotate(rotationDegrees);
            return(Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true));
        }
Esempio n. 14
0
        /// <summary>
        /// Load the image from the device, and resize it to the specified dimensions.
        /// </summary>
        /// <returns>The and resize bitmap.</returns>
        /// <param name="fileName">File name.</param>
        /// <param name="width">Width.</param>
        /// <param name="height">Height.</param>
        public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height)
        {
            // First we get the the dimensions of the file on disk
            BitmapFactory.Options options = new BitmapFactory.Options
            {
                // InBitmap = true,
                InJustDecodeBounds = true
            };
            BitmapFactory.DecodeFile(fileName, options);

            ExifInterface exif        = new ExifInterface(fileName);
            var           orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, 1);
            // Next we calculate the ratio that we need to resize the image by
            // in order to fit the requested dimensions.
            var outHeight    = options.OutHeight;
            var outWidth     = options.OutWidth;
            var inSampleSize = 1;

            if (outHeight > height || outWidth > width)
            {
                inSampleSize = outWidth > outHeight
                    ? outHeight / height
                    : outWidth / width;
            }

            // Now we will load the image and have BitmapFactory resize it for us.
            // options.InSampleSize = inSampleSize;
            options.InJustDecodeBounds = false;
            var resizedBitmap = BitmapFactory.DecodeFile(fileName, options);

            return(ExifRotateBitmap(fileName, resizedBitmap));

            // return resizedBitmap;
        }
Esempio n. 15
0
        static int GetRotation(string filePath)
        {
            try
            {
                using (var ei = new ExifInterface(filePath))
                {
                    var orientation = (Orientation)ei.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal);

                    switch (orientation)
                    {
                    case Orientation.Rotate90:
                        return(90);

                    case Orientation.Rotate180:
                        return(180);

                    case Orientation.Rotate270:
                        return(270);

                    default:
                        return(0);
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                throw ex;
#else
                return(0);
#endif
            }
        }
Esempio n. 16
0
        private int GetRotation(ExifInterface exif)
        {
            if (exif == null)
            {
                return(0);
            }

            try
            {
                var orientation = (Orientation)exif.GetAttributeInt(
                    ExifInterface.TagOrientation,
                    (int)Orientation.Normal);

                switch (orientation)
                {
                case Orientation.Rotate90:
                    return(90);

                case Orientation.Rotate180:
                    return(180);

                case Orientation.Rotate270:
                    return(270);

                default:
                    return(0);
                }
            }
            catch (Exception ex)
            {
                loggingService.Error(ex);

                return(0);
            }
        }
Esempio n. 17
0
        private static int GetRotation(string filePath)

        {
            using (var ei = new ExifInterface(filePath))

            {
                var orientation = (Android.Media.Orientation)ei.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Normal);



                switch (orientation)

                {
                case Android.Media.Orientation.Rotate90:

                    return(90);

                case Android.Media.Orientation.Rotate180:

                    return(180);

                case Android.Media.Orientation.Rotate270:

                    return(270);

                default:

                    return(0);
                }
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Changes the orientation.
        /// </summary>
        /// <returns>The orientation.</returns>
        /// <param name="mediafile">Mediafile.</param>
        /// <param name="bitmap">Bitmap.</param>
        public static Bitmap changeOrientation(ExifInterface exifInterface, Bitmap bitmap)
        {
            int orientation = exifInterface.GetAttributeInt(ExifInterface.TagOrientation, 1);
            var matrix      = new Matrix();

            switch (orientation)
            {
            case 2:
                matrix.SetScale(-1, 1);
                break;

            case 3:
                matrix.SetRotate(180);
                break;

            case 4:
                matrix.SetRotate(180);
                matrix.PostScale(-1, 1);
                break;

            case 5:
                matrix.SetRotate(90);
                matrix.PostScale(-1, 1);
                break;

            case 6:
                matrix.SetRotate(90);
                break;

            case 7:
                matrix.SetRotate(-90);
                matrix.PostScale(-1, 1);
                break;

            case 8:
                matrix.SetRotate(-90);
                break;

            default:
                return(bitmap);
            }

            try
            {
                Bitmap oriented = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true);
                bitmap.Recycle();
                return(oriented);
            }
            catch (OutOfMemoryError e)
            {
                e.PrintStackTrace();
                return(bitmap);
            }
            catch (System.Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
                return(bitmap);
            }
        }
        private static Bitmap ResizeImage(System.IO.Stream image, Bitmap bitmap, int width, int height)
        {
            var originalPixelWidth  = bitmap.Width;
            var originalPixelHeight = bitmap.Height;

            var widthRatio   = (double)height / originalPixelWidth;
            var heightRatio  = (double)width / originalPixelHeight;
            var aspectWidth  = width;
            var aspectHeight = height;

            if (originalPixelWidth > originalPixelHeight)
            {
                aspectWidth = (int)(heightRatio * originalPixelWidth);
            }
            else
            {
                aspectHeight = (int)(widthRatio * originalPixelHeight);
            }

            var scaledBitmap = Bitmap.CreateBitmap(aspectWidth, aspectHeight, Bitmap.Config.Argb8888);
            var ratioX       = aspectWidth / (float)bitmap.Width;
            var ratioY       = aspectHeight / (float)bitmap.Height;
            var middleX      = aspectWidth / 2.0f;
            var middleY      = aspectHeight / 2.0f;

            var scaleMatrix = new Matrix();

            scaleMatrix.SetScale(ratioX, ratioY, middleX, middleY);

            using var canvas = new Canvas(scaledBitmap)
                  {
                      Matrix = scaleMatrix
                  };
            canvas.DrawBitmap(bitmap, middleX - bitmap.Width / 2, middleY - bitmap.Height / 2, new Paint(PaintFlags.FilterBitmap));

            // check the rotation of the image and display it properly
            using var exif = new ExifInterface(image);
            var orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, 0);
            var matrix      = new Matrix();

            if (orientation == 6)
            {
                matrix.PostRotate(90);
            }
            else if (orientation == 3)
            {
                matrix.PostRotate(180);
            }
            else if (orientation == 8)
            {
                matrix.PostRotate(270);
            }

            scaledBitmap = Bitmap.CreateBitmap(scaledBitmap, 0, 0,
                                               scaledBitmap.Width, scaledBitmap.Height, matrix,
                                               true);

            return(scaledBitmap);
        }
Esempio n. 20
0
        public Photo(string filename)
        {
            Filename = filename;
            // Get and set picture orienation
            ExifInterface exif = new ExifInterface(Filename);

            mOrientation = exif.GetAttributeInt(ExifInterface.TagOrientation, (int)PhotoOrienation.Normal);
        }
Esempio n. 21
0
        /**
         * Sets a Bitmap and initializes the image rotation according to the EXIT data.
         * <p>
         * The EXIF can be retrieved by doing the following:
         * <code>ExifInterface exif = new ExifInterface(path);</code>
         *
         * @param bitmap the original bitmap to set; if null, this
         * @param exif the EXIF information about this bitmap; may be null
         */

        public void SetImageBitmap(Bitmap bitmap, ExifInterface exif)
        {
            try
            {
                if (bitmap == null)
                {
                    return;
                }

                if (exif == null)
                {
                    SetImageBitmap(bitmap);
                    return;
                }

                var matrix      = new Matrix();
                int orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, 1);
                int rotate      = -1;
                //TODO CHECK THIS FIX
                switch (orientation)
                {
                case (int)Orientation.Rotate270:
                    rotate = 270;
                    break;

                case (int)Orientation.Rotate180:
                    rotate = 180;
                    break;

                case (int)Orientation.Rotate90:
                    rotate = 90;
                    break;
                }

                if (rotate == -1)
                {
                    SetImageBitmap(bitmap);
                }
                else
                {
                    matrix.PostRotate(rotate);
                    Bitmap rotatedBitmap = Bitmap.CreateBitmap(bitmap,
                                                               0,
                                                               0,
                                                               bitmap.Width,
                                                               bitmap.Height,
                                                               matrix,
                                                               true);
                    SetImageBitmap(rotatedBitmap);
                    bitmap.Recycle();
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
Esempio n. 22
0
        public Tuple <string, byte[]> ProcessCapturedPhoto(string filePath, bool needXMirroring = false)
        {
            ExifInterface exif        = new ExifInterface(filePath);
            var           orientation = (Orientation)exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Undefined);

            Bitmap bitmap = BitmapFactory.DecodeFile(filePath);
            Matrix mtx    = new Matrix();

            switch (orientation)
            {
            case Orientation.Rotate180:
                mtx.PreRotate(180);
                break;

            case Orientation.Rotate90:     // portrait
                mtx.PreRotate(90);
                break;

            case Orientation.Rotate270:     // might need to flip horizontally too...
                mtx.PreRotate(270);
                break;

            default:
                mtx.PreRotate(90);
                break;

            case Orientation.Undefined:  // Nexus 7 landscape...
            case Orientation.Normal:     // landscape
            case Orientation.FlipHorizontal:
            case Orientation.FlipVertical:
            case Orientation.Transpose:
            case Orientation.Transverse:
                break;
            }

            if (needXMirroring)
            {
                mtx.PreScale(1, -1);
            }

            bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, mtx, false);
            mtx.Dispose();
            mtx = null;

            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            var compressionResult         = bitmap.Compress(Bitmap.CompressFormat.Jpeg, 80, stream);

            var id       = Guid.NewGuid().ToString("n");
            var filename = $"capture-{id}";

            var imageByteArray = stream.GetBuffer();

            var newFilePath = SavePictureAndThumbnail(imageByteArray, filename);

            return(new Tuple <string, byte[]>(newFilePath, imageByteArray));
        }
Esempio n. 23
0
        public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height, Context context)
        {
            // First we get the the dimensions of the file on disk
            BitmapFactory.Options options = new BitmapFactory.Options {
                InJustDecodeBounds = true
            };
            BitmapFactory.DecodeFile(fileName, options);

            // Next we calculate the ratio that we need to resize the image by
            // in order to fit the requested dimensions.
            int outHeight    = options.OutHeight;
            int outWidth     = options.OutWidth;
            int inSampleSize = 1;

            if (outHeight > height || outWidth > width)
            {
                inSampleSize = outWidth > outHeight
                                   ? outHeight / height
                                   : outWidth / width;
            }

            // Now we will load the image and have BitmapFactory resize it for us.
            options.InSampleSize       = inSampleSize;
            options.InJustDecodeBounds = false;
            Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options);


            //int orientation = ((BaseActivity)context).tempRotation;
            ExifInterface exif        = new ExifInterface(fileName);
            int           orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, 1);


            float angle = 0f;

            switch (orientation)
            {
            case 6:
                angle = 90f;
                break;

            case 3:
                angle = 180f;
                break;

            case 8:
                angle = 270f;
                break;
            }
            Matrix matrix = new Matrix();

            matrix.PostRotate(angle);
            resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, true);
            GC.Collect();
            return(resizedBitmap);
        }
Esempio n. 24
0
        internal static Bitmap GetPictureWithRotation(Bitmap inBitmap, ExifInterface exif)
        {
            var orientation = (Orientation)exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Undefined);

            Bitmap resultBitmap = inBitmap;
            Matrix mtx          = new Matrix();

            switch (orientation)
            {
            case Orientation.Undefined:     // Nexus 7 landscape...
                break;

            case Orientation.Normal:     // landscape
                break;

            case Orientation.FlipHorizontal:
                break;

            case Orientation.Rotate180:
                mtx.PreRotate(180);
                resultBitmap = Bitmap.CreateBitmap(resultBitmap, 0, 0, resultBitmap.Width, resultBitmap.Height, mtx, false);
                mtx.Dispose();
                mtx = null;
                break;

            case Orientation.FlipVertical:
                break;

            case Orientation.Transpose:
                break;

            case Orientation.Rotate90:     // portrait
                mtx.PreRotate(90);
                resultBitmap = Bitmap.CreateBitmap(resultBitmap, 0, 0, resultBitmap.Width, resultBitmap.Height, mtx, false);
                mtx.Dispose();
                mtx = null;
                break;

            case Orientation.Transverse:
                break;

            case Orientation.Rotate270:     // might need to flip horizontally too...
                mtx.PreRotate(270);
                resultBitmap = Bitmap.CreateBitmap(resultBitmap, 0, 0, resultBitmap.Width, resultBitmap.Height, mtx, false);
                mtx.Dispose();
                mtx = null;
                break;

            default:
                break;
            }

            return(resultBitmap);
        }
Esempio n. 25
0
        private static Bitmap ResizeImageFile(string filePath, int maxSize)
        {
            Bitmap bp             = BitmapFactory.DecodeFile(filePath);
            int    originalWidth  = bp.Width;
            int    originalHeight = bp.Height;

            ExifInterface exif        = new ExifInterface(filePath);
            int           orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Normal);
            int           angle       = 0;

            switch (orientation)
            {
            case (int)Android.Media.Orientation.Normal:
                break;

            case (int)Android.Media.Orientation.Rotate90:
                angle = 90;
                break;

            case (int)Android.Media.Orientation.Rotate180:
                angle = 180;
                break;

            case (int)Android.Media.Orientation.Rotate270:
                angle = 270;
                break;
            }

            float resizeScale = 1;

            if (originalWidth > maxSize || originalHeight > maxSize)
            {
                double ratio = (double)originalWidth / (double)originalHeight;

                if (ratio > 1)
                {
                    resizeScale = (float)maxSize / (float)originalWidth;
                }
                else
                {
                    resizeScale = (float)maxSize / (float)originalHeight;
                }
            }


            Matrix mat = new Matrix();

            mat.PostScale(resizeScale, resizeScale);
            mat.PostRotate((float)angle);
            Bitmap capBitmap = Bitmap.CreateBitmap(bp, 0, 0, originalWidth, originalHeight, mat, true);

            return(capBitmap);
        }
Esempio n. 26
0
        static public Bitmap LoadFrom(string realPath)
        {
            if (!File.Exists(realPath))
            {
                Insight.Track("ChadderImage: Invalid real path");
                return(null);
            }

            ExifInterface exif = new ExifInterface(realPath);
            var           a    = exif.GetAttributeInt(ExifInterface.TagOrientation, -1);

            return(SampledLoad(realPath, a));
        }
Esempio n. 27
0
        public static Bitmap GetAndRotateBitmap(string fileName)
        {
            Bitmap bitmap = BitmapFactory.DecodeFile(fileName);

            // Images are being saved in landscape, so rotate them back to portrait if they were taken in portrait
            // See https://forums.xamarin.com/discussion/5409/photo-being-saved-in-landscape-not-portrait
            // See http://developer.android.com/reference/android/media/ExifInterface.html
            using (Matrix mtx = new Matrix())
            {
                if (Android.OS.Build.Product.Contains("Emulator"))
                {
                    mtx.PreRotate(90);
                }
                else
                {
                    ExifInterface exif        = new ExifInterface(fileName);
                    var           orientation = (Orientation)exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal);

                    //TODO : handle FlipHorizontal, FlipVertical, Transpose and Transverse
                    switch (orientation)
                    {
                    case Orientation.Rotate90:
                        mtx.PreRotate(90);
                        break;

                    case Orientation.Rotate180:
                        mtx.PreRotate(180);
                        break;

                    case Orientation.Rotate270:
                        mtx.PreRotate(270);
                        break;

                    case Orientation.Normal:
                        // Normal, do nothing
                        break;

                    default:
                        break;
                    }
                }

                if (mtx != null)
                {
                    bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, mtx, false);
                }
            }

            return(bitmap);
        }
Esempio n. 28
0
        public Task <byte[]> NormalizeAsync(byte[] imageData, float quality)
        {
            return(Task <byte[]> .Factory.StartNew(delegate {
                int orient;
                using (MemoryStream ms = new MemoryStream(imageData)) {
                    ExifInterface exif = new ExifInterface(ms);
                    orient = exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Undefined);
                }
                using (Bitmap bmp = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length)) {
                    Matrix m = new Matrix();
                    switch (orient)
                    {
                    case (int)Orientation.Rotate90:
                        m.PostRotate(90);
                        break;

                    case (int)Orientation.Rotate180:
                        m.PostRotate(180);
                        break;

                    case (int)Orientation.Rotate270:
                        m.PostRotate(270);
                        break;

                    default:
                        m = null;
                        break;
                    }
                    byte[] retVal = null;
                    if (m != null)
                    {
                        using (Bitmap normBmp = Bitmap.CreateBitmap(bmp, 0, 0, bmp.Width, bmp.Height, m, true)) {
                            using (MemoryStream ms = new MemoryStream()) {
                                normBmp.Compress(Bitmap.CompressFormat.Jpeg, (int)(quality * 100), ms);
                                retVal = ms.ToArray();
                            }
                        }
                    }
                    else
                    {
                        using (MemoryStream ms = new MemoryStream()) {
                            bmp.Compress(Bitmap.CompressFormat.Jpeg, (int)(quality * 100), ms);
                            retVal = ms.ToArray();
                        }
                    }
                    return (retVal);
                }
            }));
        }
Esempio n. 29
0

        
Esempio n. 30
0
 private static bool TryGetOrientation(string url, out Orientation rez)
 {
     try
     {
         var ei = new ExifInterface(url);
         rez = (Orientation)ei.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal);
         return(true);
     }
     catch
     {
         //nothing to do
     }
     rez = Orientation.Normal;
     return(false);
 }