示例#1
0
        public static Android.Graphics.Bitmap Rotate(Android.Graphics.Bitmap bitmap, int angle)
        {
            var mat = new Android.Graphics.Matrix();

            mat.PostRotate(angle);
            return(Android.Graphics.Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, mat, true));
        }
示例#2
0
 public Android.Graphics.Bitmap rotateMyBitmap(Android.Graphics.Bitmap bmp)
 {
     //*****旋转一下
     Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();
     matrix.PostRotate(270);
     Android.Graphics.Bitmap nbmp2 = Android.Graphics.Bitmap.CreateBitmap(bmp, 0, 0, bmp.Width, bmp.Height, matrix, true);
     bmp.Dispose();
     return(nbmp2);
 }
        private static Android.Graphics.Bitmap RotateImage(string filePath, int rotation)
        {
            var originalImage = Android.Graphics.BitmapFactory.DecodeFile(filePath);

            var matrix = new Android.Graphics.Matrix();

            matrix.PostRotate(rotation);
            var rotatedImage = Android.Graphics.Bitmap.CreateBitmap(originalImage, 0, 0, originalImage.Width, originalImage.Height, matrix, true);

            originalImage.Recycle();
            return(rotatedImage);
        }
        private void CorrectImageOrientation(string imagePath)
        {
            ExifInterface ei          = new ExifInterface(imagePath);
            int           orientation = ei.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Undefined);

            var  matrix           = new Android.Graphics.Matrix();
            bool rotationIsNeeded = true;

            switch (orientation)
            {
            case (int)Orientation.Rotate90:
                matrix.PostRotate(90);
                break;

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

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

            default:
                rotationIsNeeded = false;
                break;
            }
            if (rotationIsNeeded)
            {
                var sourceBitmap  = Android.Graphics.BitmapFactory.DecodeFile(imagePath);
                var rotatedBitmap = Android.Graphics.Bitmap.CreateBitmap(sourceBitmap, 0, 0, sourceBitmap.Width, sourceBitmap.Height, matrix, true);
                sourceBitmap.Dispose();
                App.FileHelper.DeleteFile(imagePath);
                var outStream = App.FileHelper.CreateFile(imagePath);
                rotatedBitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, outStream);
                rotatedBitmap.Dispose();
                outStream.Close();
                outStream.Dispose();
            }
        }
示例#5
0
        public void OnPictureTaken(byte[] data, Camera camera)
        {
            camera.StartPreview();
            Task.Run(() => {
                try
                {
                    var fileService = DependencyService.Get <IFileService>();

                    var fileName = "photo_" + DateTime.Now.ToString() + ".jpg";
                    fileName     = fileName.Replace("/", "_");

                    fileName = System.IO.Path.Combine(fileService.AppWorkPath, fileName);

                    //                   File.WriteAllBytes(fileName, data);

                    var rotate = 90;
                    if (rotate > 0)
                    {
                        var matrix = new Android.Graphics.Matrix();
                        matrix.PostRotate(rotate);

                        var bitmap        = Android.Graphics.BitmapFactory.DecodeByteArray(data, 0, data.Length); // MediaStore.Images.Media.GetBitmap(this.ContentResolver, Android.Net.Uri.Parse(uri));
                        var rotatedBitmap = Android.Graphics.Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true);
                        bitmap.Recycle();
                        bitmap = rotatedBitmap;

                        using (var finalStream = new MemoryStream())
                        {
                            bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, finalStream);
                            data = finalStream.GetBuffer();
                        }

                        File.WriteAllBytes(fileName, data);

                        bitmap.Recycle();
                        GC.Collect();
                    }

                    if (this.CameraPreviewCallback != null)
                    {
                        this.CameraPreviewCallback.AfterPictureTaken(fileName);
                    }
                }
                catch
                {
                }
            });
        }
示例#6
0
        /// <summary>
        /// Read the file into a Mat
        /// </summary>
        /// <param name="fileName">The file to read from</param>
        /// <param name="mat">The Mat to read the file into</param>
        /// <param name="loadType">The load type</param>
        /// <returns>True if successfully read file into Mat</returns>
        public bool ReadFile(String fileName, Mat mat, CvEnum.ImreadModes loadType)
        {
            try
            {
                int rotation = 0;
                Android.Media.ExifInterface exif = new Android.Media.ExifInterface(fileName);
                int orientation = exif.GetAttributeInt(Android.Media.ExifInterface.TagOrientation, int.MinValue);
                switch (orientation)
                {
                case (int)Android.Media.Orientation.Rotate270:
                    rotation = 270;
                    break;

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

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

                using (Android.Graphics.Bitmap bmp = Android.Graphics.BitmapFactory.DecodeFile(fileName))
                {
                    if (rotation == 0)
                    {
                        bmp.ToMat(mat);
                    }
                    else
                    {
                        Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();
                        matrix.PostRotate(rotation);
                        using (Android.Graphics.Bitmap rotated = Android.Graphics.Bitmap.CreateBitmap(bmp, 0, 0, bmp.Width, bmp.Height, matrix, true))
                        {
                            rotated.ToMat(mat);
                        }
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                //throw;
                return(false);
            }
        }
        public async void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
        {
            using (var bmp = await Android.Graphics.BitmapFactory.DecodeByteArrayAsync(data, 0, data.Length))
            {
                using (var resized = bmp.DefaultResize())
                {
                    var mat = new Android.Graphics.Matrix();

                    var info = new Android.Hardware.Camera.CameraInfo();
                    Android.Hardware.Camera.GetCameraInfo(currentCamera, info);

                    if (info.Facing == CameraFacing.Front)
                    {
                        mat.PostScale(-1, 1);
                    }

                    mat.PostRotate(90);
                    var correctBmp = Android.Graphics.Bitmap.CreateBitmap(resized, 0, 0, resized.Width, resized.Height, mat, true);
                    _captureComplete.SetResult(correctBmp);
                    _captureComplete = null;
                }
            }
        }
示例#8
0
        protected override void Draw(Canvas canvas, Rect bounds)
        {
            int   saveCount = canvas.Save();
            RectF arcBounds = mTempBounds;

            arcBounds.Set(bounds);

            mPaint.Color = new Color(mColor);

            //calculate fish clip bounds
            //clip the width of the fish need to increase mPathDottedLineSize * 1.2f
            RectF  fishRectF = new RectF(mFishHeadPos[0] - mFishWidth / 2.0f - mPathDottedLineSize * 1.2f, mFishHeadPos[1] - mFishHeight / 2.0f, mFishHeadPos[0] + mFishWidth / 2.0f + mPathDottedLineSize * 1.2f, mFishHeadPos[1] + mFishHeight / 2.0f);
            Matrix matrix    = new Matrix();

            matrix.PostRotate(mFishRotateDegrees, fishRectF.CenterX(), fishRectF.CenterY());
            matrix.MapRect(fishRectF);

            //draw river
            int riverSaveCount = canvas.Save();

            mPaint.SetStyle(Paint.Style.Stroke);
            canvas.ClipRect(fishRectF, Region.Op.Difference);
            canvas.DrawPath(CreateRiverPath(arcBounds), mPaint);
            canvas.RestoreToCount(riverSaveCount);

            //draw fish
            int fishSaveCount = canvas.Save();

            mPaint.SetStyle(Paint.Style.Fill);
            canvas.Rotate(mFishRotateDegrees, mFishHeadPos[0], mFishHeadPos[1]);
            canvas.ClipPath(CreateFishEyePath(mFishHeadPos[0], mFishHeadPos[1] - mFishHeight * 0.06f), Region.Op.Difference);
            canvas.DrawPath(CreateFishPath(mFishHeadPos[0], mFishHeadPos[1]), mPaint);
            canvas.RestoreToCount(fishSaveCount);

            canvas.RestoreToCount(saveCount);
        }
示例#9
0
 public static Android.Graphics.Bitmap rotateImage(Android.Graphics.Bitmap source, float angle)
 {
     Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();
     matrix.PostRotate(angle);
     return(Android.Graphics.Bitmap.CreateBitmap(source, 0, 0, source.Width, source.Height, matrix, true));
 }
        private static Android.Graphics.Bitmap RotateImage(string filePath, int rotation)
        {
            var originalImage = Android.Graphics.BitmapFactory.DecodeFile(filePath);

            var matrix = new Android.Graphics.Matrix();
            matrix.PostRotate(rotation);
            var rotatedImage = Android.Graphics.Bitmap.CreateBitmap(originalImage, 0, 0, originalImage.Width, originalImage.Height, matrix, true);
            originalImage.Recycle();
            return rotatedImage;
        }
示例#11
0
        /// <summary>
        /// Write picture data to stream. The picture will be rotated if necessary.
        /// </summary>
        public async Task WriteDataToStream(byte[] data, Stream writeStream)
        {
            var rotate = _displayOrientation;

            if (rotate == 0)
            {
                using (var ms = new MemoryStream(data))
                {
                    await ms.CopyToAsync(writeStream);
                }
            }
            else
            {
                using (var sourceBm = await Android.Graphics.BitmapFactory.DecodeByteArrayAsync(data, 0, data.Length))
                {
                    using (var m = new Android.Graphics.Matrix())
                    {
                        m.PostRotate(rotate);
                        using (var bm = Android.Graphics.Bitmap.CreateBitmap(sourceBm, 0, 0, sourceBm.Width, sourceBm.Height, m, true))
                        {
                            if (sourceBm != bm) { sourceBm.Recycle(); }
                            await bm.CompressAsync(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, writeStream);
                            bm.Recycle();
                        }
                    }
                }
            }
        }