예제 #1
0
 private 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);
 }
		public static Bitmap ToRotated(Bitmap source, double degrees, bool ccw, bool resize)
		{
			if (ccw)
				degrees = 360d - degrees;

			Bitmap bitmap = Bitmap.CreateBitmap(source.Width, source.Height, Bitmap.Config.Argb8888);
			using (Canvas canvas = new Canvas(bitmap))
			using (Paint paint = new Paint())
			using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
			using (Matrix matrix = new Matrix())
			{
				paint.AntiAlias = true;
				paint.Dither = true;
				paint.FilterBitmap = true;

				float targetRotation = (float)degrees;
				float rotationPivotX = (float)source.Width / 2.0f;
				float rotationPivotY = (float)source.Height / 2.0f;
				float targetWidth = source.Width;
				float targetHeight = source.Height;

				if (resize)
				{
					double cosR = Math.Cos(DegreesToRadians(targetRotation));
					double sinR = Math.Sin(DegreesToRadians(targetRotation));

					// Recalculate dimensions after rotation around pivot point
					double x1T = rotationPivotX * (1.0 - cosR) + (rotationPivotY * sinR);
					double y1T = rotationPivotY * (1.0 - cosR) - (rotationPivotX * sinR);
					double x2T = x1T + (targetWidth * cosR);
					double y2T = y1T + (targetWidth * sinR);
					double x3T = x1T + (targetWidth * cosR) - (targetHeight * sinR);
					double y3T = y1T + (targetWidth * sinR) + (targetHeight * cosR);
					double x4T = x1T - (targetHeight * sinR);
					double y4T = y1T + (targetHeight * cosR);

					double maxX = Math.Max(x4T, Math.Max(x3T, Math.Max(x1T, x2T)));
					double minX = Math.Min(x4T, Math.Min(x3T, Math.Min(x1T, x2T)));
					double maxY = Math.Max(y4T, Math.Max(y3T, Math.Max(y1T, y2T)));
					double minY = Math.Min(y4T, Math.Min(y3T, Math.Min(y1T, y2T)));
					targetWidth = (int) Math.Floor(maxX - minX);
					targetHeight  = (int) Math.Floor(maxY - minY);

					float sx = (float)source.Width / targetWidth;
					float sy = (float)source.Height / targetHeight;

					matrix.SetScale(sx, sy, rotationPivotX, rotationPivotY);
				}

				matrix.PostRotate(targetRotation, rotationPivotX, rotationPivotY);
				canvas.DrawBitmap(source, matrix, paint);

				return bitmap;				
			}
		}
예제 #3
0
        public static Bitmap CreateBitmap(Texture2D image, Vector2 scale, float rotation, Vector2 origin)
        {
            Matrix matrix = new Matrix();

            matrix.PostScale(scale.X, scale.Y);
            matrix.PostRotate(rotation, origin.X, origin.Y);

            Bitmap source    = Texture2D2Bitmap(image);
            Bitmap newBitmap = Bitmap.CreateBitmap(source, 0, 0, source.Width, source.Height, matrix, true);

            return(newBitmap);
        }
예제 #4
0
        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))
                        {
                            //manually disposed sooner such that memory is released.
                            bmp.Dispose();
                            rotated.ToMat(mat);
                        }
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                //throw;
                return(false);
            }
        }
예제 #5
0
		public static Matrix parseTransformRotate(String pString) {
			var start = SVGConstants.ATTRIBUTE_TRANSFORM_VALUE_ROTATE.Length + 1;
			var svgNumberParserFloatResult = pString.Substring(start, pString.IndexOf(')') - start).ParseFloats ();
			SVGTransformParser.assertNumberParserResultNumberCountMinimum(svgNumberParserFloatResult, 1);

			float angle = svgNumberParserFloatResult[0];
			float cx = 0;
			float cy = 0;
			if (svgNumberParserFloatResult.Length > 2) {
				cx = svgNumberParserFloatResult[1];
				cy = svgNumberParserFloatResult[2];
			}
			Matrix matrix = new Matrix();
			matrix.PostTranslate(cx, cy);
			matrix.PostRotate(angle);
			matrix.PostTranslate(-cx, -cy);
			return matrix;
		}
예제 #6
0
        public Matrix GetRotateMatrix()
        {
            // By default this is an identity matrix.
            Matrix matrix = new Matrix();

            if (Rotation != 0)
            {
                // We want to do the rotation at origin, but since the bounding
                // rectangle will be changed after rotation, so the delta values
                // are based on old & new width/height respectively.
                int cx = Bitmap.Width / 2;
                int cy = Bitmap.Height / 2;
                matrix.PreTranslate(-cx, -cy);
                matrix.PostRotate(Rotation);
                matrix.PostTranslate(Width / 2, Height / 2);
            }

            return matrix;
        }
예제 #7
0
        public void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
        {
            camera.StopPreview();
            camera.Release();
            previewing = false;
            isPhotoTaken = true;

            photo = BitmapFactory.DecodeByteArray(data, 0, data.Length);
            Matrix matrix = new Matrix();
            matrix.PostRotate(90);
            photo = Bitmap.CreateBitmap(photo, 0, 0, photo.Width, photo.Height, matrix, true);

            surfaceView.Visibility = ViewStates.Invisible;
            acceptButton.Visibility = ViewStates.Visible;
            cancelButton.Visibility = ViewStates.Visible;
            imageView.Visibility = ViewStates.Visible;
            buttonLayout.SetBackgroundColor(Color.ParseColor("#80000000"));
            imageView.SetImageBitmap(photo);
        }
예제 #8
0
		public static Bitmap ToRotatedBitmap(this Bitmap sourceBitmap, int rotationDegrees)
		{
			if (rotationDegrees == 0)
				return sourceBitmap;

			int width = sourceBitmap.Width;
			int height = sourceBitmap.Height;

			if (rotationDegrees == 90 || rotationDegrees == 270) 
			{
				width = sourceBitmap.Height;
				height = sourceBitmap.Width;
			}

			Bitmap bitmap = Bitmap.CreateBitmap(width, height, sourceBitmap.GetConfig());
			using (Canvas canvas = new Canvas(bitmap))
			using (Paint paint = new Paint())
			using (BitmapShader shader = new BitmapShader(sourceBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
			using (Matrix matrix = new Matrix())
			{
				// paint.AntiAlias = true;
				// paint.Dither = true;
				// paint.FilterBitmap = true;

				matrix.PostRotate(rotationDegrees);
				canvas.DrawBitmap(sourceBitmap, matrix, paint);
			}

			if (sourceBitmap != null && sourceBitmap.Handle != IntPtr.Zero && !sourceBitmap.IsRecycled)
			{
				sourceBitmap.Recycle();
				sourceBitmap.Dispose();	
			}

			return bitmap;
		}
		private static Bitmap RotateImage(string filePath, int rotation)
		{
			Bitmap originalImage = BitmapFactory.DecodeFile(filePath);

			Matrix matrix = new Matrix();
			matrix.PostRotate(rotation);
			var rotatedImage = Bitmap.CreateBitmap(originalImage, 0, 0, originalImage.Width, originalImage.Height, matrix, true);
			originalImage.Recycle();
			return rotatedImage;
		}
		//Configures the neccesary matrix transformation to apply to the textureView
		public void configureTransform(int viewWidth, int viewHeight) 
		{
			if (null == Activity || null == previewSize || null == textureView) 
				return;

			int rotation = (int)Activity.WindowManager.DefaultDisplay.Rotation;
			var matrix = new Matrix ();
			var viewRect = new RectF (0, 0, viewWidth, viewHeight);
			var bufferRect = new RectF (0, 0, previewSize.Height, previewSize.Width);
			float centerX = viewRect.CenterX();
			float centerY = viewRect.CenterY();
			if ((int)SurfaceOrientation.Rotation90 == rotation || (int)SurfaceOrientation.Rotation270 == rotation) { 
				bufferRect.Offset ((centerX - bufferRect.CenterX()), (centerY - bufferRect.CenterY()));
				matrix.SetRectToRect (viewRect, bufferRect, Matrix.ScaleToFit.Fill);
				float scale = System.Math.Max (
					(float)viewHeight / previewSize.Height,
					(float)viewHeight / previewSize.Width);
				matrix.PostScale (scale, scale, centerX, centerY);
				matrix.PostRotate (90 * (rotation - 2), centerX, centerY);
			}
			textureView.SetTransform (matrix);
		}
예제 #11
0
        protected override void OnDraw(Canvas canvas)
        {
            coreX       = Width / 2;
            coreY       = Height / 2;
            roundRadius = (int)(Width / 2 * radiusDistance); //计算中心圆圈半径

            RectF rect = new RectF(0, 0, Width, Height);

            if (roundMenus != null && roundMenus.Count > 0)
            {
                float sweepAngle = 360 / roundMenus.Count; //每个弧形的角度
                deviationDegree = sweepAngle / 2;          //其实的偏移角度,如果4个扇形的时候是X形状,而非+,设为0试试就知道什么意思了
                for (int i = 0; i < roundMenus.Count; i++)
                {
                    RoundMenu roundMenu = roundMenus[i];
                    //填充
                    Paint paint = new Paint();
                    paint.AntiAlias = true;
                    if (onClickState == i)
                    {
                        //选中
                        paint.Color = new Color(roundMenu.selectSolidColor);
                    }
                    else
                    {
                        //未选中
                        paint.Color = new Color(roundMenu.solidColor);
                    }
                    canvas.DrawArc(rect, deviationDegree + (i * sweepAngle), sweepAngle, true, paint);

                    //画描边
                    paint             = new Paint();
                    paint.AntiAlias   = true;
                    paint.StrokeWidth = roundMenu.strokeSize;
                    paint.SetStyle(Paint.Style.Stroke);
                    paint.Color = new Color(roundMenu.strokeColor);
                    canvas.DrawArc(rect, deviationDegree + (i * sweepAngle), sweepAngle, roundMenu.useCenter, paint);

                    //画图案
                    Matrix matrix = new Matrix();
                    matrix.PostTranslate((float)((coreX + Width / 2 * roundMenu.iconDistance) - (roundMenu.icon.Width / 2)), coreY - (roundMenu.icon.Height / 2));
                    matrix.PostRotate(((i + 1) * sweepAngle), coreX, coreY);
                    canvas.DrawBitmap(roundMenu.icon, matrix, null);
                }
            }

            //画中心圆圈
            if (isCoreMenu)
            {
                //填充
                RectF rect1 = new RectF(coreX - roundRadius, coreY - roundRadius, coreX + roundRadius, coreY + roundRadius);
                Paint paint = new Paint();
                paint.AntiAlias   = true;
                paint.StrokeWidth = coreMenuStrokeSize;
                if (onClickState == -1)
                {
                    paint.Color = new Color(coreMenuSelectColor);
                }
                else
                {
                    paint.Color = new Color(coreMenuColor);
                }
                canvas.DrawArc(rect1, 0, 360, true, paint);

                //画描边
                paint             = new Paint();
                paint.AntiAlias   = true;
                paint.StrokeWidth = coreMenuStrokeSize;
                paint.SetStyle(Paint.Style.Stroke);
                paint.Color = new Color(coreMenuStrokeColor);
                canvas.DrawArc(rect1, 0, 360, true, paint);
                if (coreBitmap != null)
                {
                    //画中心圆圈的"OK"图标
                    canvas.DrawBitmap(coreBitmap, coreX - coreBitmap.Width / 2, coreY - coreBitmap.Height / 2, null); //在 0,0坐标开始画入src
                }
            }
        }
예제 #12
0
 /// Author:	Guy Spronck
 /// Date:	09-06-2015
 /// <summary>
 /// Rotates the bitmap.
 /// </summary>
 /// <returns>The bitmap.</returns>
 /// <param name="source">Source.</param>
 /// <param name="angle">Angle.</param>
 public Bitmap RotateBitmap(Bitmap source, float angle)
 {
     Matrix matrix = new Matrix();
     matrix.PostRotate (angle);
     return Bitmap.CreateBitmap(source, 0, 0, source.Width, source.Height, matrix, true);
 }
예제 #13
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if ((resultCode == Result.Ok) && (data != null))
            {
                if (requestCode == PickImageId)
                {
                    Uri uri = data.Data;
                    Bitmap tempBitmap = BitmapFactory.DecodeFile(GetPathToImage(uri));

                    //resize
                    int newHeight = ConvertDptoPx(60);
                    int newWidth = (int) (((float) tempBitmap.Width) / ((float) tempBitmap.Height) * newHeight);

                    //scale
                    float scaleWidth = ((float) newWidth) / tempBitmap.Width;
                    float scaleHeight = ((float) newHeight) / tempBitmap.Height;

                    Matrix matrix = new Matrix();
                    matrix.PostScale(scaleWidth, scaleHeight);

                    //rotate
                    ExifInterface exif = new ExifInterface(GetPathToImage(uri));
                    int orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, 1);
                    switch (orientation)
                    {
                        case 3:
                            matrix.PostRotate(180);
                            break;
                        case 6:
                            matrix.PostRotate(90);
                            break;
                        case 8:
                            matrix.PostRotate(270);
                            break;
                    }

                    _bitmap = Bitmap.CreateBitmap(tempBitmap, 0, 0, tempBitmap.Width, tempBitmap.Height, matrix, false);
                    tempBitmap.Recycle();
                    _imageView.SetImageBitmap(_bitmap);
                }

                else if (requestCode == PickContactsId)
                {
                    _participantsAdapter.Items.Clear();

                    string intentString = data.GetStringExtra("New participants");
                    if (intentString != string.Empty)
                    {
                        string[] contactStrings = intentString.Split('|');

                        for (int i = 0; i < contactStrings.Length; i++)
                        {
                            string[] contactAttr = contactStrings[i].Split(',');

                            _participantsAdapter.Items.Add(new ListItem(contactAttr[0], int.Parse(contactAttr[1]))); // 0 = naam, 1 = image
                        }
                    }

                    _participantsAdapter.NotifyDataSetChanged();
                    _participantsRecyclerView.LayoutParameters.Height = ConvertDptoPx(listItemHeight * _participantsAdapter.Items.Count);
                    _participantsRecyclerView.Invalidate ();
                    _scrollView.Invalidate();
                    _scrollView.RequestLayout();
                }
            }
        }
예제 #14
0
        protected virtual void OnCreate(Bundle bundle, RedLaserSettings settings)
        {
            base.OnCreate (bundle);

            this.settings = settings;

            StatusManager.Initialize(this);

            Log.Debug(TAG, "Starting capture activity");

            var window = Window;
            window.AddFlags(WindowManagerFlags.KeepScreenOn);
            this.mBarcodeScanLayout = new BarcodeScanLayout(this, this.settings.HoldStill, this.settings.AlignBarcode);
            SetContentView(this.mBarcodeScanLayout);

            var metrics = new DisplayMetrics();
            WindowManager.DefaultDisplay.GetMetrics(metrics);

            this.mWidth = metrics.WidthPixels;
            this.mHeight = metrics.HeightPixels;
            this.mDensity = metrics.Density;

            var logo = new ImageView(this);
            logo.SetScaleType(ImageView.ScaleType.Matrix);
            logo.SetImageResource(LogoResource);
            var matrix = new Matrix();
            matrix.PostRotate(270.0f);

            if(mWidth < mHeight) {
                var temp = mWidth;
                mWidth = mHeight;
                mHeight = temp;
            }

            var offset = 0;
            if(mDensity == 1.0f) {
                offset = 100;
            }

            matrix.PostTranslate((float)(mWidth / 2.0d + mWidth / 8 * mDensity) + offset, (float)(mHeight / 2.80 + 160.0f * mDensity));
            logo.ImageMatrix = matrix;

            CameraManager.Init(Application);
            viewfinderView = mBarcodeScanLayout.ViewFinderView;
            handler = null;
            hasSurface = false;
        }
        //@Override
        protected override void onLoadingDrawableSet(Drawable imageDrawable)
        {
            if (null != imageDrawable)
            {

                int dHeight = imageDrawable.IntrinsicHeight;
                int dWidth = imageDrawable.IntrinsicWidth;

                /**
                 * We need to set the width/height of the ImageView so that it is
                 * square with each side the size of the largest drawable dimension.
                 * This is so that it doesn't clip when rotated.
                 */
                ViewGroup.LayoutParams lp = mHeaderImage.LayoutParameters;
                lp.Width = lp.Height = Math.Max(dHeight, dWidth);
                mHeaderImage.RequestLayout();

                /**
                 * We now rotate the Drawable so that is at the correct rotation,
                 * and is centered.
                 */

                mHeaderImage.SetScaleType(ImageView.ScaleType.Matrix);
                Matrix matrix = new Matrix();
                matrix.PostTranslate((lp.Width - dWidth) / 2f, (lp.Height - dHeight) / 2f);
                matrix.PostRotate(getDrawableRotationAngle(), lp.Width / 2f, lp.Height / 2f);
                mHeaderImage.ImageMatrix = matrix;

            }
        }
		/// <summary>
		/// Configures the necessary transformation to mTextureView.
		/// This method should be called after the camera preciew size is determined in openCamera, and also the size of mTextureView is fixed
		/// </summary>
		/// <param name="viewWidth">The width of mTextureView</param>
		/// <param name="viewHeight">VThe height of mTextureView</param>
		private void ConfigureTransform(int viewWidth, int viewHeight)
		{
			Activity activity = Activity;
			if (mTextureView == null || mPreviewSize == null || activity == null) {
				return;
			}

			SurfaceOrientation rotation = activity.WindowManager.DefaultDisplay.Rotation;
			Matrix matrix = new Matrix ();
			RectF viewRect = new RectF (0, 0, viewWidth, viewHeight);
			RectF bufferRect = new RectF (0, 0, mPreviewSize.Width, mPreviewSize.Height);
			float centerX = viewRect.CenterX();
			float centerY = viewRect.CenterY();
			if (rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) {
				bufferRect.Offset (centerX - bufferRect.CenterX(), centerY - bufferRect.CenterY());
				matrix.SetRectToRect (viewRect, bufferRect, Matrix.ScaleToFit.Fill);
				float scale = Math.Max ((float)viewHeight / mPreviewSize.Height, (float)viewWidth / mPreviewSize.Width);
				matrix.PostScale (scale, scale, centerX, centerY);
				matrix.PostRotate (90 * ((int)rotation - 2), centerX, centerY);
			}
			mTextureView.SetTransform (matrix);
		}
		public void SetRotorPosAngle(float deg)
		{
			Console.WriteLine("posDegree = {0}", deg);

			if (deg >= 210 || deg <= 150)
			{
				if (deg > 180)
					deg = deg - 360;

				// Rotate handle
				Matrix matrix = new Matrix();
				m_RotorImaveView.SetScaleType(ImageView.ScaleType.Matrix);
				matrix.PostRotate((float)deg, m_Width / 2, m_Height / 2);
				m_RotorImaveView.ImageMatrix = matrix;
			}
		}
예제 #18
0
        public static ImageView RotateImage(ImageView image, RectangleF origImageFrame, RectangleF rotatedImageRect, double degAngle, bool disposeOriginalImage)
        {
            ImageView toReturn = image;
            float width = image.Drawable.Bounds.Width () / 2;
            float height = image.Drawable.Bounds.Height () / 2;
            double radAngle = degAngle * LOLConstants.DegToRad;
            Matrix matrix = new Matrix ();
            toReturn.SetScaleType (ImageView.ScaleType.Matrix);   //required
            matrix.PostRotate ((float)degAngle, width, height);
            toReturn.ImageMatrix = matrix;

            return toReturn;
        }
예제 #19
0
        Bitmap RotateDownsizeBitmap( string imageFile, float size )
        {
            // first see how big this image is.
            BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
            decodeOptions.InJustDecodeBounds = true;
            BitmapFactory.DecodeFile( imageFile, decodeOptions );

            // now limit its size to 'size'
            float scaleFactor = (float)decodeOptions.OutWidth / size;
            if ( scaleFactor > 1.00f )
            {
                decodeOptions.InSampleSize = (int)System.Math.Ceiling( scaleFactor );
            }
            else
            {
                scaleFactor = (float)decodeOptions.OutHeight / size;
                if ( scaleFactor > 1.00f )
                {
                    decodeOptions.InSampleSize = (int)System.Math.Ceiling( scaleFactor );
                }
            }

            decodeOptions.InJustDecodeBounds = false;

            // Open the bitmap
            Bitmap sourceBmp = BitmapFactory.DecodeFile( imageFile, decodeOptions );

            // does the image need to be rotated?
            Java.IO.File javaImageFile = new Java.IO.File( imageFile );

            int neededRotation = NeededRotation( javaImageFile );
            if ( neededRotation != 0 )
            {
                // create a rotation matrix to orient the picture "up"
                Matrix transform = new Matrix();
                transform.PostRotate( neededRotation );

                // re-create the bitmap rotated.
                Bitmap rotatedBmp = Bitmap.CreateBitmap( sourceBmp, 0, 0, sourceBmp.Width, sourceBmp.Height, transform, true );

                // release our refs to the bmps
                sourceBmp.Dispose( );
                sourceBmp = null;

                return rotatedBmp;
            }
            else
            {
                return sourceBmp;
            }
        }