private void CreateTextures(CVPixelBuffer img) { var imageWidth = (int)img.Width; var imageHeight = (int)img.Height; var widthUV = (int)img.GetWidthOfPlane(1); var heightUV = (int)img.GetHeightOfPlane(1); this.sizeY = (int)img.GetBytesPerRowOfPlane(0) * imageHeight; this.sizeUV = (int)img.GetBytesPerRowOfPlane(1) * heightUV; this.cameraTextureY = new Texture2D() { Format = PixelFormat.R8, Usage = TextureUsage.Dynamic, CpuAccess = TextureCpuAccess.Write, Width = imageWidth, Height = imageHeight, Levels = 1 }; this.cameraTextureUV = new Texture2D() { Format = PixelFormat.R8G8, Usage = TextureUsage.Dynamic, CpuAccess = TextureCpuAccess.Write, Width = widthUV, Height = heightUV, Levels = 1, }; this.graphicsDevice.Textures.UploadTexture(this.cameraTextureY); this.graphicsDevice.Textures.UploadTexture(this.cameraTextureUV); this.yuvMaterial.LuminanceTexture = this.cameraTextureY; this.yuvMaterial.ChromaTexture = this.cameraTextureUV; }
public static CVPixelBuffer ToPixelBuffer(this UIImage self) { var width = self.Size.Width; var height = self.Size.Height; var attrs = new CVPixelBufferAttributes(); attrs.CGBitmapContextCompatibility = true; attrs.CGImageCompatibility = true; var resultPixelBuffer = new CVPixelBuffer((int)(width), (int)(height), CVPixelFormatType.CV32ARGB, attrs); resultPixelBuffer.Lock(CVPixelBufferLock.None); var pixelData = resultPixelBuffer.GetBaseAddress(0); var rgbColorSpace = CGColorSpace.CreateDeviceRGB(); var context = new CGBitmapContext(data: pixelData, width: (int)(width), height: (int)(height), bitsPerComponent: 8, bytesPerRow: resultPixelBuffer.GetBytesPerRowOfPlane(0), colorSpace: rgbColorSpace, bitmapInfo: CGImageAlphaInfo.NoneSkipFirst); context.TranslateCTM(tx: 0, ty: height); context.ScaleCTM(sx: 1.0f, sy: -1.0f); UIGraphics.PushContext(context); self.Draw(new CGRect(x: 0, y: 0, width: width, height: height)); UIGraphics.PopContext(); resultPixelBuffer.Unlock(CVPixelBufferLock.None); return(resultPixelBuffer); }