示例#1
0
        private UIImage GetImageFromSampleBuffer(CMSampleBuffer sampleBuffer)
        {
            // Get a pixel buffer from the sample buffer
            using (var pixelBuffer = sampleBuffer.GetImageBuffer() as CVPixelBuffer)
            {
                // Lock the base address
                pixelBuffer.Lock(CVPixelBufferLock.None);

                // Prepare to decode buffer
                const CGBitmapFlags flags = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little;

                // Decode buffer - Create a new colorspace
                using (var cs = CGColorSpace.CreateDeviceRGB())
                {
                    // Create new context from buffer
                    using (var context = new CGBitmapContext(pixelBuffer.BaseAddress,
                                                             pixelBuffer.Width,
                                                             pixelBuffer.Height,
                                                             8,
                                                             pixelBuffer.BytesPerRow,
                                                             cs,
                                                             (CGImageAlphaInfo)flags))
                    {
                        // Get the image from the context
                        using (var cgImage = context.ToImage())
                        {
                            // Unlock and return image
                            pixelBuffer.Unlock(CVPixelBufferLock.None);
                            return(UIImage.FromImage(cgImage));
                        }
                    }
                }
            }
        }
示例#2
0
        public CGImage(int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow,
                       CGColorSpace colorSpace, CGBitmapFlags bitmapFlags, CGDataProvider provider,
                       nfloat [] decode, bool shouldInterpolate, CGColorRenderingIntent intent)
        {
            if (width < 0)
            {
                throw new ArgumentException("width");
            }
            if (height < 0)
            {
                throw new ArgumentException("height");
            }
            if (bitsPerPixel < 0)
            {
                throw new ArgumentException("bitsPerPixel");
            }
            if (bitsPerComponent < 0)
            {
                throw new ArgumentException("bitsPerComponent");
            }
            if (bytesPerRow < 0)
            {
                throw new ArgumentException("bytesPerRow");
            }

            handle = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow,
                                   colorSpace == null ? IntPtr.Zero : colorSpace.Handle, bitmapFlags, provider == null ? IntPtr.Zero : provider.Handle,
                                   decode,
                                   shouldInterpolate, intent);
        }
示例#3
0
 extern static IntPtr CGImageCreate(IntPtr size_t_width, IntPtr size_t_height, IntPtr size_t_bitsPerComponent,
                                    IntPtr size_t_bitsPerPixel, IntPtr size_t_bytesPerRow,
                                    IntPtr /* CGColorSpaceRef */ space,
                                    CGBitmapFlags bitmapInfo,
                                    IntPtr /* CGDataProviderRef */ provider,
                                    nfloat [] decode,
                                    bool shouldInterpolate,
                                    CGColorRenderingIntent intent);
示例#4
0
        partial void ScreenButton_down(UIButton sender)
        {
            if (!isScreen)
            {
                ScreenshotView.Hidden = false;
                ScreenBtn.SetTitle("Hide screen", UIControlState.Normal);
                isScreen = true;

                int    val = 1920 * 1080 * 4 + 8;              //ARGB 1920x1080 + 8(iosspecific)
                IntPtr b   = Marshal.AllocHGlobal(val);


                int val1 = -1;                 //return realsize after getvideoshot
                int val2 = -1;                 //return realsize after getvideoshot
                int val3 = 0;                  //return realsize after getvideoshot

                int rc = _mediaPlayer.GetVideoShot(b, ref val, ref val1, ref val2, ref val3);
                if (rc == -2)
                {
                    System.Console.WriteLine(String.Format("<binary> rc: {0} , not enough space allocated", rc));
                    Marshal.FreeHGlobal(b);
                    return;
                }
                else if (rc == -1)
                {
                    System.Console.WriteLine(String.Format("<binary> rc: {0} , error", rc));
                    Marshal.FreeHGlobal(b);
                    return;
                }


                CGDataProvider provider         = new CGDataProvider(b, val);
                int            bitsPerComponent = 8;
                int            bitsPerPixel     = 32;
                int            bytesPerRow      = val3;

                CGColorSpace           colorspaceref = CGColorSpace.CreateDeviceRGB();
                CGBitmapFlags          bitmapinfo    = CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.NoneSkipFirst;
                CGColorRenderingIntent renderintent  = CGColorRenderingIntent.Default;
                CGImage imageref = new CGImage(val1, val2, bitsPerComponent, bitsPerPixel, val3, colorspaceref, bitmapinfo, provider, null, true, renderintent);

                UIImage img = new UIImage(imageref);
                ScrenShotImageView.Image = img;
                Marshal.FreeHGlobal(b);
            }
            else
            {
                ScreenBtn.SetTitle("Screen", UIControlState.Normal);
                ScreenshotView.Hidden = true;
                isScreen = false;
            }
        }
示例#5
0
        public FastBitmap(int width, int height)
        {
            _width  = width;
            _height = height;

            _colorSpace = CGColorSpace.CreateDeviceRGB();
            _bpp        = 4;
            int           bytesPerRow      = _width * _bpp;
            int           bitmapByteCount  = bytesPerRow * _height;
            const int     bitsPerComponent = 8;
            CGBitmapFlags flags            = CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big;

            _bitmapData = new byte[bitmapByteCount];
            _handle     = GCHandle.Alloc(_bitmapData);

            _context = new CGBitmapContext(_bitmapData, _width, _height, bitsPerComponent, bytesPerRow, _colorSpace, flags);
        }
示例#6
0
 public CGBitmapContext(byte []?data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace?colorSpace, CGBitmapFlags bitmapInfo)
     : base(Create(data, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo, out var buffer), true)
 {
     this.buffer = buffer;
 }
示例#7
0
 static IntPtr Create(byte []?data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace?colorSpace, CGBitmapFlags bitmapInfo, out GCHandle buffer)
 {
     buffer = default(GCHandle);
     if (data != null)
     {
         buffer = GCHandle.Alloc(data, GCHandleType.Pinned);                  // This requires a pinned GCHandle, because unsafe code is scoped to the current block, and the address of the byte array will be used after this function returns.
     }
     return(CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorSpace.GetHandle(), (uint)bitmapInfo));
 }
示例#8
0
 public CGBitmapContext(IntPtr data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace?colorSpace, CGBitmapFlags bitmapInfo)
     : base(CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorSpace.GetHandle(), (uint)bitmapInfo), true)
 {
 }
示例#9
0
 public CGBitmapContext(byte [] data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGBitmapFlags bitmapInfo)
 {
     if (data != null)
     {
         buffer = GCHandle.Alloc(data, GCHandleType.Pinned);                  // This requires a pinned GCHandle, because unsafe code is scoped to the current block, and the address of the byte array will be used after this function returns.
     }
     Handle = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, GetHandle(colorSpace), (uint)bitmapInfo);
 }
示例#10
0
        public static String[] CalculatePValue(UIImage image, ref nfloat totP, ref nfloat avgP)
        {
            UIImage uiImagePic = image;


            CIContext ctx     = CIContext.FromOptions(null);
            CGImage   cgimage = uiImagePic.CGImage;

            nfloat powerValueAvg = 0.0f;
            nfloat powerValueTot = 0.0f;
            nint   numPixels     = 0;
            //compute values
            nint width  = cgimage.Width;
            nint height = cgimage.Height;

            nint bpr             = cgimage.BytesPerRow;
            nint bpp             = cgimage.BitsPerPixel;
            nint bpc             = cgimage.BitsPerComponent;
            nint bytes_per_pixel = bpp / bpc;

            CGBitmapFlags  info      = cgimage.BitmapInfo;
            CGDataProvider provider  = cgimage.DataProvider;
            NSData         data      = provider.CopyData();
            IntPtr         bytesintp = data.Bytes;

            byte[] bytes = new byte[data.Length];
            Marshal.Copy(bytesintp, bytes, 0, (int)data.Length);
            Console.WriteLine("Pixel Data:");
            for (nint row = 0; row < height; row++)
            {
                for (nint col = 0; col < width; col++)
                {
                    byte[] pixel = new byte[bytes_per_pixel];
                    for (int i = 0; i < bytes_per_pixel; i++)
                    {
                        pixel [i] = bytes [row * bpr + col * bytes_per_pixel + i];
                    }

                    //Console.Write("(");
                    for (nint x = 0; x < bytes_per_pixel; x++)
                    {
                        //pixel[0] is r
                        //pixel[1] is g
                        //pixel[2] is b
                        //pixel[3] is alpha
                        numPixels++;
                        nfloat curPower = pixel [0] * 0.299f + pixel [1] * 0.587f + pixel [2] * 0.0722f;
                        powerValueTot += curPower;
                        powerValueAvg  = powerValueTot / numPixels;

                        //	Console.Write(pixel[x]);
                        //	if( x < bytes_per_pixel - 1 )
                        //		Console.Write(",");
                    }

                    //Console.Write(")");
                    //if( col < width - 1 )
                    //	Console.Write(", ");
                }

                //Console.Write("\n");
            }
            totP = powerValueTot;
            avgP = powerValueAvg;
            string outputToName;

            outputToName = powerValueAvg.ToString("0.0000");
            string outputToName2 = powerValueTot.ToString("0.0000");

            string[] strToRet = new string[2];
            strToRet [0] = outputToName;
            strToRet [1] = outputToName2;
            return(strToRet);
        }
示例#11
0
 extern static /* CGImageRef */ IntPtr CGImageCreate(/* size_t */ nint width, /* size_t */ nint height,
                                                     /* size_t */ nint bitsPerComponent, /* size_t */ nint bitsPerPixel, /* size_t */ nint bytesPerRow,
                                                     /* CGColorSpaceRef */ IntPtr space, CGBitmapFlags bitmapInfo, /* CGDataProviderRef */ IntPtr provider,
                                                     /* CGFloat[] */ nfloat [] decode, [MarshalAs(UnmanagedType.I1)] bool shouldInterpolate, CGColorRenderingIntent intent);
示例#12
0
 public CGBitmapContext(byte [] data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGBitmapFlags bitmapInfo)
 {
     if (data != null)
         buffer = GCHandle.Alloc (data, GCHandleType.Pinned);
     Handle = CGBitmapContextCreate (data, width, height, bitsPerComponent, bytesPerRow, GetHandle (colorSpace), (uint) bitmapInfo);
 }
示例#13
0
 public CGBitmapContext(IntPtr data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGBitmapFlags bitmapInfo)
     : base(CGBitmapContextCreate (data, width, height, bitsPerComponent, bytesPerRow, GetHandle (colorSpace), (uint) bitmapInfo), true)
 {
 }
示例#14
0
        public void SetOriginalImage(UIImage originalImage, CGRect cropFrame)
        {
            LoadIndicator.StartAnimating();
            InvokeOnMainThread(() =>
            {
                CGImage imageRef = originalImage.CGImage;
                UIImageOrientation imageOrientation = originalImage.Orientation;

                if (imageRef == null)
                {
                    return;
                }


                var bytesPerRow          = 0;
                var width                = imageRef.Width;
                var height               = imageRef.Height;
                var bitsPerComponent     = imageRef.BitsPerComponent;
                CGColorSpace colorSpace  = CGColorSpace.CreateDeviceRGB();
                CGBitmapFlags bitmapInfo = imageRef.BitmapInfo;

                switch (imageOrientation)
                {
                case UIImageOrientation.RightMirrored:
                case UIImageOrientation.LeftMirrored:
                case UIImageOrientation.Right:
                case UIImageOrientation.Left:
                    width  = imageRef.Height;
                    height = imageRef.Width;
                    break;

                default:
                    break;
                }

                CGSize imageSize        = new CGSize(width, height);
                CGBitmapContext context = new CGBitmapContext(null,
                                                              width,
                                                              height,
                                                              bitsPerComponent,
                                                              bytesPerRow,
                                                              colorSpace,
                                                              bitmapInfo);

                colorSpace.Dispose();

                if (context == null)
                {
                    imageRef.Dispose();
                    return;
                }

                switch (imageOrientation)
                {
                case UIImageOrientation.RightMirrored:
                case UIImageOrientation.Right:
                    context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2);
                    context.RotateCTM(-((nfloat)Math.PI / 2));
                    context.TranslateCTM(-imageSize.Height / 2, -imageSize.Width / 2);
                    break;

                case UIImageOrientation.LeftMirrored:
                case UIImageOrientation.Left:
                    context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2);
                    context.RotateCTM((nfloat)(Math.PI / 2));
                    context.TranslateCTM(-imageSize.Height / 2, -imageSize.Width / 2);
                    break;

                case UIImageOrientation.Down:
                case UIImageOrientation.DownMirrored:
                    context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2);
                    context.RotateCTM((nfloat)Math.PI);
                    context.TranslateCTM(-imageSize.Width / 2, -imageSize.Height / 2);
                    break;

                default:
                    break;
                }

                context.InterpolationQuality = CGInterpolationQuality.High;
                context.SetBlendMode(CGBlendMode.Copy);
                context.DrawImage(new CGRect(0, 0, imageRef.Width, imageRef.Height), imageRef);

                CGImage contextImage = context.ToImage();

                context.Dispose();

                if (contextImage != null)
                {
                    _originalImage = UIImage.FromImage(contextImage, originalImage.CurrentScale, UIImageOrientation.Up);

                    contextImage.Dispose();
                }

                imageRef.Dispose();

                BeginInvokeOnMainThread(() =>
                {
                    CGSize convertedImageSize = new CGSize(_originalImage.Size.Width / _cropSizeRatio,
                                                           _originalImage.Size.Height / _cropSizeRatio);

                    ImageView.Alpha = 0;
                    ImageView.Image = _originalImage;

                    CGSize sampleImageSize = new CGSize(Math.Max(convertedImageSize.Width, ScrollView.Frame.Size.Width),
                                                        Math.Max(convertedImageSize.Height, ScrollView.Frame.Size.Height));

                    ScrollView.MinimumZoomScale = 1;
                    ScrollView.MaximumZoomScale = 1;
                    ScrollView.ZoomScale        = 1;
                    ImageView.Frame             = new CGRect(0, 0, convertedImageSize.Width, convertedImageSize.Height);

                    nfloat zoomScale = 1;

                    if (convertedImageSize.Width < convertedImageSize.Height)
                    {
                        zoomScale = (ScrollView.Frame.Size.Width / convertedImageSize.Width);
                    }
                    else
                    {
                        zoomScale = (ScrollView.Frame.Size.Height / convertedImageSize.Height);
                    }

                    ScrollView.ContentSize = sampleImageSize;

                    if (zoomScale < 1)
                    {
                        ScrollView.MinimumZoomScale = zoomScale;
                        ScrollView.MaximumZoomScale = 1;
                        ScrollView.ZoomScale        = zoomScale;
                    }
                    else
                    {
                        ScrollView.MinimumZoomScale = zoomScale;
                        ScrollView.MaximumZoomScale = zoomScale;
                        ScrollView.ZoomScale        = zoomScale;
                    }

                    ScrollView.ContentInset  = UIEdgeInsets.Zero;
                    ScrollView.ContentOffset = new CGPoint((ImageView.Frame.Size.Width - ScrollView.Frame.Size.Width) / 2, (ImageView.Frame.Size.Height - ScrollView.Frame.Size.Height) / 2);
                    if (cropFrame.Size.Width > 0 && cropFrame.Size.Height > 0)
                    {
                        nfloat scale        = UIScreen.MainScreen.Scale;
                        nfloat newZoomScale = (_targetSize.Width * scale) / cropFrame.Size.Width;

                        ScrollView.ZoomScale = newZoomScale;

                        nfloat heightAdjustment = (_targetSize.Height / _cropSizeRatio) - ScrollView.ContentSize.Height;
                        nfloat offsetY          = cropFrame.Y + (heightAdjustment * _cropSizeRatio * scale);

                        ScrollView.ContentOffset = new CGPoint(cropFrame.X / scale / _cropSizeRatio,
                                                               (offsetY / scale / _cropSizeRatio) - heightAdjustment);
                    }

                    ScrollView.SetNeedsLayout();

                    UIView.Animate(0.3,
                                   () =>
                    {
                        LoadIndicator.Alpha = 0;
                        ImageView.Alpha     = 1;
                    }, () =>
                    {
                        LoadIndicator.StopAnimating();
                    });
                });
            });
        }
示例#15
0
		public CGImage (int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow,
				CGColorSpace colorSpace, CGBitmapFlags bitmapFlags, CGDataProvider provider,
				nfloat [] decode, bool shouldInterpolate, CGColorRenderingIntent intent)
		{
			if (width < 0)
				throw new ArgumentException ("width");
			if (height < 0)
				throw new ArgumentException ("height");
			if (bitsPerPixel < 0)
				throw new ArgumentException ("bitsPerPixel");
			if (bitsPerComponent < 0)
				throw new ArgumentException ("bitsPerComponent");
			if (bytesPerRow < 0)
				throw new ArgumentException ("bytesPerRow");

			handle = CGImageCreate (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow,
						colorSpace == null ? IntPtr.Zero : colorSpace.Handle, bitmapFlags, provider == null ? IntPtr.Zero : provider.Handle,
						decode,
						shouldInterpolate, intent);
		}
示例#16
0
		extern static /* CGImageRef */ IntPtr CGImageCreate (/* size_t */ nint width, /* size_t */ nint height, 
			/* size_t */ nint bitsPerComponent, /* size_t */ nint bitsPerPixel, /* size_t */ nint bytesPerRow,
			/* CGColorSpaceRef */ IntPtr space, CGBitmapFlags bitmapInfo, /* CGDataProviderRef */ IntPtr provider,
			/* CGFloat[] */ nfloat [] decode, bool shouldInterpolate, CGColorRenderingIntent intent);
示例#17
0
 public CGBitmapContext(byte [] data, int width, int height, int bitsPerComponent, int bytesPerRow, CGColorSpace colorSpace, CGBitmapFlags bitmapInfo)
     : base(CGBitmapContextCreate(data, (UIntPtr)width, (UIntPtr)height, (UIntPtr)bitsPerComponent, (UIntPtr)bytesPerRow, colorSpace.handle, (uint)bitmapInfo), true)
 {
 }
示例#18
0
        CGImage NewCGImage(int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow, CGColorSpace colorSpace, CGBitmapFlags bitmapFlags, CGDataProvider provider, nfloat[] decode, bool shouldInterpolate, CGColorRenderingIntent intent)
        {
            var image = new CGImage(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapFlags, dataProvider, decode, shouldInterpolate, intent);

            if (image == null)
            {
                var e = new ArgumentException("Failed to create CGImage");
                e.Data["width"]             = width;
                e.Data["height"]            = height;
                e.Data["bitsPerComponent"]  = bitsPerComponent;
                e.Data["bitsPerPixel"]      = bitsPerPixel;
                e.Data["bytesPerRow"]       = bytesPerRow;
                e.Data["colorSpace"]        = colorSpace;
                e.Data["bitmapFlags"]       = bitmapFlags;
                e.Data["dataProvider"]      = dataProvider;
                e.Data["decode"]            = decode;
                e.Data["shouldInterpolate"] = shouldInterpolate;
                e.Data["intent"]            = intent;
            }
            return(image);
        }
示例#19
0
		extern static IntPtr CGImageCreate(int size_t_width, int size_t_height, int size_t_bitsPerComponent,
						   int size_t_bitsPerPixel, int size_t_bytesPerRow,
						   IntPtr /* CGColorSpaceRef */ space,
						   CGBitmapFlags bitmapInfo,
						   IntPtr /* CGDataProviderRef */ provider,
						   float [] decode,
						   bool shouldInterpolate,
						   CGColorRenderingIntent intent);
		public CGBitmapContext (byte [] data, int width, int height, int bitsPerComponent, int bytesPerRow, CGColorSpace colorSpace, CGBitmapFlags bitmapInfo)
			: base (CGBitmapContextCreate (data, (UIntPtr) width, (UIntPtr) height, (UIntPtr) bitsPerComponent, (UIntPtr) bytesPerRow, colorSpace.handle, (uint) bitmapInfo), true)
		{
		}
示例#21
0
 public CGBitmapContext(byte [] data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGBitmapFlags bitmapInfo)
 {
     if (data != null)
     {
         buffer = GCHandle.Alloc(data, GCHandleType.Pinned);
     }
     Handle = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, GetHandle(colorSpace), (uint)bitmapInfo);
 }