Example #1
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);
                }
            }
        }
Example #2
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);
        }
        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);
        }
Example #4
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);
            }
        }
Example #5
0
        private bool IsValidExif(ExifInterface exif)
        {
            // if null, then not falid
            if (exif == null)
            {
                return(false);
            }

            try
            {
                // if has thumb, but is <= 0, then not valid
                if (exif.HasThumbnail && (exif.GetThumbnail()?.Length ?? 0) <= 0)
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine("Unable to get thumbnail exif: " + ex);
#endif
                return(false);
            }

            return(true);
        }
Example #6
0
        private void SaveDrawing()
        {
            if (Control.appearance.ToLower() == "annotate" || Control.appearance.ToLower() == "textannotate")
            {
                Bitmap Background     = ((BitmapDrawable)BackgroundImage.Drawable).Bitmap;
                Bitmap CombinedBitmap = OverlayCanvas(Background, dv.bm);

                using (var os = new FileStream(path, FileMode.Create))
                {
                    CombinedBitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, os);
                }

                //save any note that the user entered
                ExifInterface exifData = new ExifInterface(path);
                exifData.SetAttribute(ExifInterface.TagImageDescription, EditExif.Text);
                exifData.SaveAttributes();
            }
            else
            {
                using (var os = new FileStream(path, FileMode.Create))
                {
                    dv.bm.Compress(Bitmap.CompressFormat.Jpeg, 100, os);
                }
            }


            XForm.SetValue(Binding.nodeset, path);
            Finish();
        }
Example #7
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);
        }
Example #8
0
        void SetMissingMetadata(ExifInterface exif, Location location)
        {
            if (exif == null)
            {
                return;
            }

            var position = new float[6];

            if (!exif.GetLatLong(position) && location != null)
            {
                exif.SetAttribute(ExifInterface.TagGpsLatitude, CoordinateToRational(location.Latitude));
                exif.SetAttribute(ExifInterface.TagGpsLongitude, CoordinateToRational(location.Longitude));
                exif.SetAttribute(ExifInterface.TagGpsLatitudeRef, location.Latitude > 0 ? "N" : "S");
                exif.SetAttribute(ExifInterface.TagGpsLongitudeRef, location.Longitude > 0 ? "E" : "W");
            }
            if (string.IsNullOrEmpty(exif.GetAttribute(ExifInterface.TagDatetime)))
            {
                exif.SetAttribute(ExifInterface.TagDatetime, DateTime.Now.ToString("yyyy:MM:dd hh:mm:ss"));
            }
            if (string.IsNullOrEmpty(exif.GetAttribute(ExifInterface.TagMake)))
            {
                exif.SetAttribute(ExifInterface.TagMake, Build.Manufacturer);
            }
            if (string.IsNullOrEmpty(exif.GetAttribute(ExifInterface.TagModel)))
            {
                exif.SetAttribute(ExifInterface.TagModel, Build.Model);
            }
        }
Example #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);
        }
Example #10
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);
        }
        private async Task Save(byte[] bytes)
        {
            using (OutputStream output = new FileOutputStream(File))
            {
                output.Write(bytes);
            }

            global::Android.Graphics.Bitmap bitmap = global::Android.Graphics.BitmapFactory.DecodeFile(File.AbsolutePath);
            ExifInterface exif        = new ExifInterface(File.AbsolutePath);
            string        orientation = exif.GetAttribute(ExifInterface.TagOrientation);

            switch (orientation)
            {
            case "6":
                bitmap = Camera1Fragment.Rotate(bitmap, 90);
                break;

            case "8":
                bitmap = Camera1Fragment.Rotate(bitmap, 270);
                break;

            case "3":
                bitmap = Camera1Fragment.Rotate(bitmap, 180);
                break;
            }

            await AndroidUtils.WriteBitmapToFile(File.AbsolutePath, bitmap);
        }
Example #12
0
 private void ReadExifMetadata(string fileName)
 {
     ExifInterface exif  = new ExifInterface(fileName);
     string        model = exif.GetAttribute(ExifInterface.TagModel);
     string        desc  = exif.GetAttribute(ExifInterface.TagImageDescription);
     string        make  = exif.GetAttribute(ExifInterface.TagMake);
 }
Example #13
0
        public void GetExifMetadata(string fileName, string OrderId)
        {
            ExifInterface exif = new ExifInterface(fileName);

            string exifMake  = exif.GetAttribute(ExifInterface.TagMake);
            string exifModel = exif.GetAttribute(ExifInterface.TagModel);

            if (string.IsNullOrEmpty(exifMake))
            {
                exifMake = string.Empty;
            }
            if (string.IsNullOrEmpty(exifModel))
            {
                exifModel = string.Empty;
            }


            exifMake = exifMake + " " + exifModel;
            string exifExtraData = GetLocationForExif(exif);
            string tmp           = GetPhoneInformation();
            string exifUserData  = tmp + "BatchID=" + OrderId + ",";

            exifModel = exifUserData + exifExtraData;

            exif.SetAttribute(ExifInterface.TagMake, exifModel);
            exif.SetAttribute(ExifInterface.TagModel, exifModel);
            exif.SetAttribute(ExifInterface.TagImageDescription, exifModel);

            exif.SaveAttributes();

            //ReadExifMetadata(fileName);
        }
Example #14
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) },
            };
        }
Example #15
0
        private Bitmap LoadAndResizeBitmap()
        {
            if (Path.StartsWith("http"))
            {
                //var webImage = GetImageBitmapFromUrl(Path);
                //return Bitmap.CreateScaledBitmap(webImage, _screenWidth, _screenHeight, false);
                return(GetImageBitmapFromUrl(Path));
            }

            // First we get the the dimensions of the file on disk
            BitmapFactory.Options options = new BitmapFactory.Options {
                InJustDecodeBounds = true
            };
            BitmapFactory.DecodeFile(Path, 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 > _screenHeight || outWidth > _screenWidth)
            {
                inSampleSize = outWidth > outHeight
                               ? outHeight / _screenHeight
                               : outWidth / _screenWidth;
            }

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

            // Images are being saved in landscape, so rotate them back to portrait if they were taken in portrait
            Matrix        mtx         = new Matrix();
            ExifInterface exif        = new ExifInterface(Path);
            string        orientation = exif.GetAttribute(ExifInterface.TagOrientation);

            switch (orientation)
            {
            case "6":     // portrait
                mtx.PreRotate(90);
                resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false);
                mtx.Dispose();
                mtx = null;
                break;

            case "1":     // landscape
                break;

            default:
                mtx.PreRotate(90);
                resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false);
                mtx.Dispose();
                mtx = null;
                break;
            }

            return(resizedBitmap);
        }
Example #16
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));
        }
        async Task FixOrientationAndResize(PickMediaOptions options, MediaFile media)
        {
            var originalMetadata = new ExifInterface(media.Path);

            if (options.RotateImage)
            {
                await FixOrientationAndResizeAsync(media.Path, options, originalMetadata);
            }
            else
            {
                await ResizeAsync(media.Path, options, originalMetadata);
            }

            if (options.SaveMetaData && IsValidExif(originalMetadata))
            {
                try
                {
                    originalMetadata?.SaveAttributes();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Unable to save exif {ex}");
                }
            }

            originalMetadata?.Dispose();
        }
Example #18
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
            }
        }
Example #19
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);
        }
Example #20
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);
            }
        }
        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
            }
        }
Example #22
0
 public static void Copy(this ExifInterface dest, ExifInterface source)
 {
     foreach (var tagName in tagNames)
     {
         dest.SetAttribute(tagName, source.GetAttribute(tagName));
     }
 }
Example #23
0
        public static void LocationToEXIF(string filePath, global::Android.Locations.Location loc)
        {
            try
            {
                ExifInterface ef = new ExifInterface(filePath);
                ef.SetAttribute(ExifInterface.TagGpsLatitude, Helpers.DecToDMS(loc.Latitude));
                ef.SetAttribute(ExifInterface.TagGpsLongitude, Helpers.DecToDMS(loc.Longitude));

                if (loc.Latitude > 0)
                {
                    ef.SetAttribute(ExifInterface.TagGpsLatitudeRef, "N");
                }
                else
                {
                    ef.SetAttribute(ExifInterface.TagGpsLatitudeRef, "S");
                }

                if (loc.Longitude > 0)
                {
                    ef.SetAttribute(ExifInterface.TagGpsLongitudeRef, "E");
                }
                else
                {
                    ef.SetAttribute(ExifInterface.TagGpsLongitudeRef, "W");
                }

                ef.SaveAttributes();
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Example #24
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
            }
        }
Example #25
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;
        }
Example #26
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));
        }
Example #27
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);
            }
        }
Example #28
0
        public static void CopyExif(string source, string destination, Dictionary <string, string> replace)
        {
            var sourceExif      = new ExifInterface(source);
            var destinationExif = new ExifInterface(destination);

            CopyExif(sourceExif, destinationExif, replace);
        }
        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);
        }
Example #30
0
        // シャッターを切った時のコールバック
        public void OnPictureTaken(byte[] data, AndroidCamera camera)
        {
            try {
                var SaveDir = new Java.IO.File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDcim), "Camera");
                if (!SaveDir.Exists())
                {
                    SaveDir.Mkdir();
                }


                // 非同期で画像の回転・保存・アルバムへの登録
                Task.Run(async() => {
                    // 保存ディレクトリに入ってるファイル数をカウント
                    var Files = SaveDir.List();
                    int count = 0;
                    foreach (var tmp in Files)
                    {
                        count++;
                    }

                    Matrix matrix = new Matrix();                       // 回転用の行列
                    matrix.SetRotate(90 - DetectScreenOrientation());
                    Bitmap original = BitmapFactory.DecodeByteArray(data, 0, data.Length);
                    Bitmap rotated  = Bitmap.CreateBitmap(original, 0, 0, original.Width, original.Height, matrix, true);

                    var FileName = new Java.IO.File(SaveDir, "DCIM_" + (count + 1) + ".jpg");


                    // ファイルをストレージに保存
                    FileStream stream = new FileStream(FileName.ToString(), FileMode.CreateNew);
                    await rotated.CompressAsync(Bitmap.CompressFormat.Jpeg, 90, stream);
                    stream.Close();

                    Android.Media.ExifInterface Exif = new ExifInterface(FileName.ToString());
                    Exif.SetAttribute(ExifInterface.TagGpsLatitude, Latitude);
                    Exif.SetAttribute(ExifInterface.TagGpsLongitude, Longitude);
                    Exif.SetAttribute(ExifInterface.TagGpsLatitudeRef, "N");
                    Exif.SetAttribute(ExifInterface.TagGpsLongitudeRef, "E");
                    Exif.SaveAttributes();


                    // 保存したファイルをアルバムに登録
                    string[] FilePath = { Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDcim) + "/Camera/" + "DCIM_" + (count + 1) + ".jpg" };
                    string[] mimeType = { "image/jpeg" };
                    MediaScannerConnection.ScanFile(ApplicationContext, FilePath, mimeType, null);
                    RunOnUiThread(() => {
                        Toast.MakeText(ApplicationContext, "保存しました\n" + FileName, ToastLength.Short).Show();
                    });
                    original.Recycle();
                    rotated.Recycle();

                    isTakeEnabled = false;
                });

                m_Camera.StartPreview();
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }