示例#1
0
        public static Android.Graphics.Bitmap rotateBitmap(Android.Graphics.Bitmap bitmap, int orientation)
        {
            Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();
            switch (orientation)
            {
            case (int)Android.Media.Orientation.Normal:
                return(bitmap);

            case (int)Android.Media.Orientation.FlipHorizontal:
                matrix.SetScale(-1, 1);
                break;

            case (int)Android.Media.Orientation.Rotate180:
                matrix.SetRotate(180);
                break;

            case (int)Android.Media.Orientation.FlipVertical:
                matrix.SetRotate(180);
                matrix.PostScale(-1, 1);
                break;

            case (int)Android.Media.Orientation.Transpose:
                matrix.SetRotate(90);
                matrix.PostScale(-1, 1);
                break;

            case (int)Android.Media.Orientation.Rotate90:
                matrix.SetRotate(90);
                break;

            case (int)Android.Media.Orientation.Transverse:
                matrix.SetRotate(-90);
                matrix.PostScale(-1, 1);
                break;

            case (int)Android.Media.Orientation.Rotate270:
                matrix.SetRotate(-90);
                break;

            default:
                return(bitmap);
            }
            try
            {
                Android.Graphics.Bitmap bmRotated = Android.Graphics.Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true);
                bitmap.Recycle();
                return(bmRotated);
            }
            catch (Java.Lang.OutOfMemoryError e)
            {
                e.PrintStackTrace();
                return(null);
            }
        }
        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;
                }
            }
        }
示例#3
0
            public override void OnResourceReady(Java.Lang.Object p0, Com.Bumptech.Glide.Request.Transition.ITransition p1)
            {
                float density    = currentContext.Resources.DisplayMetrics.Density;
                float MIN_WIDTH  = 60 * density;
                float MAX_WIDTH  = 200 * density;
                float MIN_HEIGHT = 60 * density;
                float MAX_HEIGHT = 200 * density;

                var resource    = p0 as Android.Graphics.Bitmap;
                int imageWidth  = resource.Width;
                int imageHeight = resource.Height;

                // 裁剪 bitmap
                float width, height;

                if (imageWidth > imageHeight)
                {
                    if (imageWidth > MAX_WIDTH)
                    {
                        float temp = MAX_WIDTH / imageWidth * imageHeight;
                        height = temp > MIN_HEIGHT ? temp : MIN_HEIGHT;
                        width  = MAX_WIDTH;
                    }
                    else if (imageWidth < MIN_WIDTH)
                    {
                        float temp = MIN_WIDTH / imageWidth * imageHeight;
                        height = temp < MAX_HEIGHT ? temp : MAX_HEIGHT;
                        width  = MIN_WIDTH;
                    }
                    else
                    {
                        float ratio = imageWidth / imageHeight;
                        if (ratio > 3)
                        {
                            ratio = 3;
                        }
                        height = imageHeight * ratio;
                        width  = imageWidth;
                    }
                }
                else
                {
                    if (imageHeight > MAX_HEIGHT)
                    {
                        float temp = MAX_HEIGHT / imageHeight * imageWidth;
                        width  = temp > MIN_WIDTH ? temp : MIN_WIDTH;
                        height = MAX_HEIGHT;
                    }
                    else if (imageHeight < MIN_HEIGHT)
                    {
                        float temp = MIN_HEIGHT / imageHeight * imageWidth;
                        width  = temp < MAX_WIDTH ? temp : MAX_WIDTH;
                        height = MIN_HEIGHT;
                    }
                    else
                    {
                        float ratio = imageHeight / imageWidth;
                        if (ratio > 3)
                        {
                            ratio = 3;
                        }
                        width  = imageWidth * ratio;
                        height = imageHeight;
                    }
                }

                var layoutParms = imageView.LayoutParameters;

                layoutParms.Width          = (int)width;
                layoutParms.Height         = (int)height;
                imageView.LayoutParameters = layoutParms;
                var   matrix      = new Android.Graphics.Matrix();
                float scaleWidth  = width / imageWidth;
                float scaleHeight = height / imageHeight;

                matrix.PostScale(scaleWidth, scaleHeight);
                imageView.SetImageBitmap(Android.Graphics.Bitmap.CreateBitmap(resource, 0, 0, imageWidth, imageHeight, matrix, true));
            }
示例#4
0
            public override void Draw(Android.Graphics.Canvas canvas)
            {
                if (Control == null) return;

                Control.CreateGraphicBuffers();

                if (origMatrix == null)
                {
                    origMatrix = new Android.Graphics.Matrix();
                    origMatrix.Set (canvas.Matrix);
                }
                if (hMatrix == null)
                {
                    hMatrix = new Android.Graphics.Matrix();
                    hMatrix.Set(origMatrix);
                    hMatrix.PostTranslate(-Control.offBmp.Width, 0);
                    hMatrix.PostScale(-1, 1);
                }
                if (vMatrix == null)
                {
                    vMatrix = new Android.Graphics.Matrix();
                    vMatrix.Set(origMatrix);
                    vMatrix.PostTranslate(0, -Control.offBmp.Height);
                    vMatrix.PostScale(1, -1);
                }

                var ctime = System.Environment.TickCount;
                var realupdate = false;
                Fleux.UIElements.Canvas.drawtime = 0;
                /**/
                if (Control.offNeedExtraDraw && Control.offUpdated)
                lock(Control.offBmp){
                    Control.offNeedExtraDraw = false;
                    Control.offUpdated = false;

                    Control.offBmpDraw = true;
                    Control.Draw(new PaintEventArgs(Control.offGr, new Rectangle(0,0, Control.offBmp.Width, Control.offBmp.Height)));
                    Control.offBmpDraw = false;

                    updcnt++;
                    realupdate = true;
                }
                //*/
                lock(Control.offBmp)
                {
                    if (Fleux.Core.FleuxApplication.HorizontalMirror)
                    {
                        canvas.Matrix = hMatrix;
                    }else if (Fleux.Core.FleuxApplication.VerticalMirror)
                    {
                        canvas.Matrix = vMatrix;
                    }else
                        canvas.Matrix = origMatrix;

                    // Thats for FastGraphics.cs
                    Control.offGr.Flush();

                    canvas.DrawBitmap(Control.offBmp.ABitmap, 0,0, paint);
                    Control.offUpdated = false;
                    updcntflush++;
                }
                if (PerfData)
                {
                    ctime = System.Environment.TickCount - ctime;
                    //if (realupdate)
                    {
                        totime += ctime;
                    }
                    var cavg = totime / (updcnt+1);

                    var cpaint = new Android.Graphics.Paint();
                    cpaint.Color = new Android.Graphics.Color(0xA0, 0xFF, 0xFF, 0xFF);
                    cpaint.SetStyle(Android.Graphics.Paint.Style.Fill);
                    canvas.DrawRect(0,0, 250, 20, cpaint);
                    canvas.DrawText(""+updcnt+":"+updcntflush+":"+updcntinval+" ctime: "+ctime+"cavg:"+cavg+" canv: "+Fleux.UIElements.Canvas.drawtime, 0,20, spaint);
                    cpaint.Dispose();
                    if (updcnt > 100)
                    {
                        totime = 0;
                        updcnt = 0;
                    }
                }
            }