internal static NativeRectangle ToNative(this PageRect rectangle) { #if __ANDROID__ || WINDOWS_PHONE_APP return(new NativeRectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height)); #elif __IOS__ return(NativeRectangle.Create((ushort)rectangle.X, (ushort)rectangle.Y, (ushort)rectangle.Width, (ushort)rectangle.Height)); #endif }
public override bool Equals(object obj) { if (!(obj is PageRect)) { return(false); } PageRect Rectangle = (PageRect)obj; return(Rectangle == this); }
public override PageRect OcrAnalyze(Stream stream) { var imageStream = new MemoryStream(); stream.CopyTo(imageStream); imageStream.Position = 0; stream.Position = 0; var createAsyncTask = BitmapDecoder.CreateAsync(imageStream.AsRandomAccessStream()).AsTask(); createAsyncTask.Wait(); var bitmapDecoder = createAsyncTask.Result; var getSoftwareBitmapAsyncTask = bitmapDecoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied).AsTask(); getSoftwareBitmapAsyncTask.Wait(); SoftwareBitmap softwareBitmap = getSoftwareBitmapAsyncTask.Result; var memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); stream.Position = 0; memoryStream.Position = 0; var client = new AmazonTextractClient(AWSAccessKeyId, AWSSecretKey, RegionEndpoint.USEast1); var detectDocumentTextTask = client.DetectDocumentTextAsync(new Amazon.Textract.Model.DetectDocumentTextRequest { Document = new Amazon.Textract.Model.Document { Bytes = memoryStream } }); detectDocumentTextTask.Wait(); var pageRect = new PageRect { Height = softwareBitmap.PixelHeight, Width = softwareBitmap.PixelWidth, LineTexts = detectDocumentTextTask.Result.Blocks.Where(block => block.BlockType == BlockType.LINE).Select(block => { return(new LineText { Text = block.Text, X = (int)(softwareBitmap.PixelWidth * block.Geometry.BoundingBox.Left), Y = (int)(softwareBitmap.PixelHeight * block.Geometry.BoundingBox.Top) }); }).ToList() }; return(pageRect); }
public override PageRect OcrAnalyze(Stream stream) { var imageStream = new MemoryStream(); stream.CopyTo(imageStream); imageStream.Position = 0; stream.Position = 0; var createAsyncTask = BitmapDecoder.CreateAsync(imageStream.AsRandomAccessStream()).AsTask(); createAsyncTask.Wait(); var bitmapDecoder = createAsyncTask.Result; var getSoftwareBitmapAsyncTask = bitmapDecoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied).AsTask(); getSoftwareBitmapAsyncTask.Wait(); SoftwareBitmap softwareBitmap = getSoftwareBitmapAsyncTask.Result; // Instantiates a client var client = ImageAnnotatorClient.Create(); // Load the image file into memory var image = Image.FromStream(stream); // Performs label detection on the image file var response = client.DetectText(image); var pageRect = new PageRect { Height = softwareBitmap.PixelHeight, Width = softwareBitmap.PixelWidth, LineTexts = response.Where(annotation => { return(string.IsNullOrEmpty(annotation.Locale)); }).Select(annotation => { return(new LineText { Text = annotation.Description, X = annotation.BoundingPoly.Vertices.First().X, Y = annotation.BoundingPoly.Vertices.First().Y, }); }).ToList() }; return(pageRect); }
internal async void Initialize() { var bandManager = BandClientManager.Instance; var pairedBands = await bandManager.GetBandsAsync(); try { if (pairedBands.Length < 1) { info.Text = "Could not connect"; return; } bandClient = await bandManager.ConnectAsync(pairedBands[0]); info.Text = "Connected to Band"; lastMove = DateTime.Now; // Create a Tile with a TextButton on it. BandTile myTile = new BandTile(myTileId) { Name = "My Teletouch", TileIcon = await LoadIcon("ms-appx:///Assets/BandTileIconLarge.png"), SmallIcon = await LoadIcon("ms-appx:///Assets/BandTileIconSmall.png") }; myTile.AdditionalIcons.Add(await LoadIcon("ms-appx:///Assets/left.png")); myTile.AdditionalIcons.Add(await LoadIcon("ms-appx:///Assets/center.png")); myTile.AdditionalIcons.Add(await LoadIcon("ms-appx:///Assets/right.png")); myTile.AdditionalIcons.Add(await LoadIcon("ms-appx:///Assets/switch.png")); myTile.AdditionalIcons.Add(await LoadIcon("ms-appx:///Assets/switch1.png")); myTile.AdditionalIcons.Add(await LoadIcon("ms-appx:///Assets/switch2.png")); // get the current theme from the Band BandTheme theme = await bandClient.PersonalizationManager.GetThemeAsync(); BandColor colorButton = theme.Base; BandColor colorButtonPressed = new BandColor(0xFF, 0xFF, 0xFF); PageRect rect1 = new PageRect(5, 0, 60, 110); PageRect rect2 = new PageRect(70, 0, 60, 110); PageRect rect3 = new PageRect(135, 0, 60, 110); PageRect rect4 = new PageRect(200, 0, 40, 35); PageRect rect5 = new PageRect(200, 37, 40, 35); PageRect rect6 = new PageRect(200, 75, 40, 35); FilledButton buttonLeft = new FilledButton() { ElementId = 1, Rect = rect1, BackgroundColor = colorButton }; FilledButton buttonCenter = new FilledButton() { ElementId = 2, Rect = rect2, BackgroundColor = colorButton }; FilledButton buttonRight = new FilledButton() { ElementId = 3, Rect = rect3, BackgroundColor = colorButton }; FilledButton buttonSwitch = new FilledButton() { ElementId = 4, Rect = rect4, BackgroundColor = colorButton }; FilledButton buttonLock = new FilledButton() { ElementId = 5, Rect = rect5, BackgroundColor = colorButton }; FilledButton buttonOnOff = new FilledButton() { ElementId = 6, Rect = rect6, BackgroundColor = colorButton }; Icon iconLeft = new Icon() { ElementId = 7, Rect = rect1, Color = colorButtonPressed, HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Center, VerticalAlignment = Microsoft.Band.Tiles.Pages.VerticalAlignment.Center }; Icon iconCenter = new Icon() { ElementId = 8, Rect = rect2, Color = colorButtonPressed, HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Center, VerticalAlignment = Microsoft.Band.Tiles.Pages.VerticalAlignment.Center }; Icon iconRight = new Icon() { ElementId = 9, Rect = rect3, Color = colorButtonPressed, HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Center, VerticalAlignment = Microsoft.Band.Tiles.Pages.VerticalAlignment.Center }; Icon iconSwitch = new Icon() { ElementId = 10, Rect = rect4, Color = colorButtonPressed, HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Center, VerticalAlignment = Microsoft.Band.Tiles.Pages.VerticalAlignment.Center }; Icon iconLock = new Icon() { ElementId = 11, Rect = rect5, Color = colorButtonPressed, HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Center, VerticalAlignment = Microsoft.Band.Tiles.Pages.VerticalAlignment.Center }; Icon iconOnOff = new Icon() { ElementId = 12, Rect = rect6, Color = colorButtonPressed, HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Center, VerticalAlignment = Microsoft.Band.Tiles.Pages.VerticalAlignment.Center }; FilledPanel panel = new FilledPanel(iconLeft, iconCenter, iconRight, iconSwitch, iconLock, iconOnOff, buttonLeft, buttonCenter, buttonRight, buttonSwitch, buttonLock, buttonOnOff) { Rect = new PageRect(0, 0, 240, 110), HorizontalAlignment = Microsoft.Band.Tiles.Pages.HorizontalAlignment.Center }; myTile.PageLayouts.Add(new PageLayout(panel)); // Remove the Tile from the Band, if present. An application won't need to do this everytime it runs. // But in case you modify this sample code and run it again, let's make sure to start fresh. //await bandClient.TileManager.RemoveTileAsync(myTileId); // Create the Tile on the Band. await bandClient.TileManager.AddTileAsync(myTile); await bandClient.TileManager.SetPagesAsync(myTileId, new PageData(pageId, 0, new FilledButtonData(1, colorButtonPressed), new FilledButtonData(2, colorButtonPressed), new FilledButtonData(3, colorButtonPressed), new FilledButtonData(4, colorButtonPressed), new FilledButtonData(5, colorButtonPressed), new FilledButtonData(6, colorButtonPressed), new IconData(7, 2), new IconData(8, 3), new IconData(9, 4), new IconData(10, 5), new IconData(11, 6), new IconData(12, 7))); // Subscribe to events bandClient.TileManager.TileOpened += EventHandler_TileOpened; bandClient.TileManager.TileClosed += EventHandler_TileClosed; bandClient.TileManager.TileButtonPressed += EventHandler_TileButtonPressed; bandClient.SensorManager.Gyroscope.ReadingChanged += Gyroscope_ReadingChanged; // Start listening for events await bandClient.TileManager.StartReadingsAsync(); } catch (BandException bandException) { Debug.WriteLine(bandException.Message); info.Text = "Could not connect"; } }
async partial void AddButtonPageClick(UIButton sender) { if (client != null && client.IsConnected) { Output("Creating tile..."); // remove an old tile try { var tiles = await client.TileManager.GetTilesTaskAsync(); if (tiles.Any(x => x.TileId.AsString() == tileId.AsString())) { // a tile exists, so remove it await client.TileManager.RemoveTileTaskAsync(tileId); Output("Removed tile!"); } } catch (BandException ex) { Output("Error: " + ex.Message); } // create the tile NSError operationError; var tileName = "iOS Sample"; var tileIcon = BandIcon.FromUIImage(UIImage.FromBundle("tile.png"), out operationError); var smallIcon = BandIcon.FromUIImage(UIImage.FromBundle("badge.png"), out operationError); var tile = BandTile.Create(tileId, tileName, tileIcon, smallIcon, out operationError); tile.BadgingEnabled = true; // create the button page var textBlock = new TextBlock(PageRect.Create(0, 0, 200, 40), TextBlockFont.Small); textBlock.ElementId = 10; textBlock.Baseline = 25; textBlock.HorizontalAlignment = HorizontalAlignment.Center; textBlock.BaselineAlignment = TextBlockBaselineAlignment.Relative; textBlock.AutoWidth = false; textBlock.Color = BandColor.FromUIColor(UIColor.Red, out operationError); textBlock.Margins = Margins.Create(5, 2, 5, 2); var button = new TextButton(PageRect.Create(0, 0, 200, 40)); button.ElementId = 11; button.HorizontalAlignment = HorizontalAlignment.Center; button.PressedColor = BandColor.FromUIColor(UIColor.Purple, out operationError); button.Margins = Margins.Create(5, 2, 5, 2); var flowPanel = new FlowPanel(PageRect.Create(15, 0, 260, 105)); flowPanel.AddElement(textBlock); flowPanel.AddElement(button); var pageLayout = new PageLayout(); pageLayout.Root = flowPanel; tile.PageLayouts.Add(pageLayout); // add the tile to the band try { Output("Adding tile..."); await client.TileManager.AddTileTaskAsync(tile); } catch (BandException ex) { Output("Error: " + ex.Message); } // set the page data try { Output("Creating page data..."); var pageValues = new PageElementData [] { TextBlockData.Create(textBlock.ElementId, "TextButton sample", out operationError), TextButtonData.Create(button.ElementId, "Press Me", out operationError) }; var page = PageData.Create(barcodePageId, 0, pageValues); await client.TileManager.SetPagesTaskAsync(new[] { page }, tileId); Output("Completed custom page!"); } catch (BandException ex) { Output("Error: " + ex.Message); } } else { Output("Band is not connected. Please wait...."); } }
public override TOutput Process(TInput input, ContentProcessorContext context) { input.ConvertBitmapType(typeof(PixelBitmapContent <Single>)); PixelBitmapContent <Single> bm = input.Mipmaps[0] as PixelBitmapContent <Single>; int w = bm.Width; int h = bm.Height; if (((w - 2) & (w - 1)) != 0 || ((h - 2) & (h - 1)) != 0 || w < Page.DataSizeWithoutPad || h < Page.DataSizeWithoutPad) { throw new Microsoft.Xna.Framework.Content.Pipeline.InvalidContentException( String.Format("PageSource texture must be power of 2 plus 1 in each direction, at least {0} pixels; got {1}x{2}.", Page.DataSizeWithPad, w, h), input.Identity); } int wt = (w - 1) / Page.DataSizeWithoutPad; int ht = (h - 1) / Page.DataSizeWithoutPad; int minx = -wt / 2; int minz = -ht / 2; int maxx = minx + wt; int maxz = minz + ht; TOutput ps = new TOutput(); float f = bm.GetPixel(0, 0); bool ok = false; unchecked { for (int z = minz; z < maxz; ++z) { for (int x = minx; x < maxx; ++x) { Page p = new Page(); PageAddress pa = new PageAddress(); pa.X = x; pa.Z = z; PageRect pr = PageAddress.RectFor(pa); p.SetAddress(pa, pr); ushort[] data = new ushort[Page.DataSizeWithPad * Page.DataSizeWithPad]; int[] count = new int[65536]; for (int v = 0; v < Page.DataSizeWithPad; ++v) { for (int u = 0; u < Page.DataSizeWithPad; ++u) { float q = bm.GetPixel(u + (x - minx) * Page.DataSizeWithoutPad, v + (z - minz) * Page.DataSizeWithoutPad); if (q != f) { ok = true; } ushort b; if (q < 0) { b = 0; } else if (q > 1) { b = 65535; } else { b = (ushort)(q * 65535); } count[b]++; data[v * Page.DataSizeWithPad + u] = b; } } int numBins = 0; for (int i = 0; i < 65536; ++i) { if (count[i] != 0) { ++numBins; } } p.SetData(data); ps.AddPage(p); } } } if (!ok) { throw new Microsoft.Xna.Framework.Content.Pipeline.InvalidContentException( String.Format("The input data has all the same value ({0}).", f), input.Identity); } TerrainShadeTag tst = new TerrainShadeTag(); tst.effect_ = context.BuildAsset <EffectContent, CompiledEffect>( new ExternalReference <EffectContent>(tsi_.terrainEffectName_), "EffectProcessor"); tst.splatControl_ = BuildControlTexture(context, tsi_.splatTextureName_); tst.base_ = BuildTexture(context, tsi_.baseSplatTextureName_); tst.r_ = BuildTexture(context, tsi_.rSplatTextureName_); tst.g_ = BuildTexture(context, tsi_.gSplatTextureName_); tst.b_ = BuildTexture(context, tsi_.bSplatTextureName_); tst.a_ = BuildTexture(context, tsi_.aSplatTextureName_); tst.nDetail_ = BuildTexture(context, tsi_.nDetailTextureName_); PixelBitmapContent <Color> normalPixels = new PixelBitmapContent <Color>(w - 1, h - 1); BuildNormalMap(bm, normalPixels); tst.normalMap_ = new Texture2DContent(); tst.normalMap_.Mipmaps = new MipmapChain(normalPixels); tst.normalMap_.GenerateMipmaps(true); NormalizeMipmaps(tst.normalMap_.Mipmaps); tsi_.tag_ = tst; ps.ShadeInfo = tsi_; return(ps); }
public override PageRect OcrAnalyze(Stream stream) { var imageStream = new MemoryStream(); stream.CopyTo(imageStream); imageStream.Position = 0; stream.Position = 0; var createAsyncTask = BitmapDecoder.CreateAsync(imageStream.AsRandomAccessStream()).AsTask(); createAsyncTask.Wait(); var bitmapDecoder = createAsyncTask.Result; var getSoftwareBitmapAsyncTask = bitmapDecoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied).AsTask(); getSoftwareBitmapAsyncTask.Wait(); SoftwareBitmap softwareBitmap = getSoftwareBitmapAsyncTask.Result; HttpClient client = new HttpClient(); // Request headers. client.DefaultRequestHeaders.Add( "Ocp-Apim-Subscription-Key", subscriptionKey); string url = uriBase; HttpResponseMessage response; byte[] byteData = GetStreamAsByteArray(stream); using (ByteArrayContent content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); var postAsyncTask = client.PostAsync(url, content); postAsyncTask.Wait(); response = postAsyncTask.Result; } string operationLocation; if (response.IsSuccessStatusCode) { operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault(); } else { // Display the JSON error data. var errorReadAsStringAsyncTask = response.Content.ReadAsStringAsync(); errorReadAsStringAsyncTask.Wait(); return(new PageRect { LineTexts = new List <LineText>() }); } var status = string.Empty; ApiResponse apiResponse = null; int retryCount = 0; do { if (retryCount++ > 0) { System.Threading.Thread.Sleep(1000); } var getAsyncTask = client.GetAsync(operationLocation); getAsyncTask.Wait(); response = getAsyncTask.Result; var readAsStringAsyncTask = response.Content.ReadAsStringAsync(); readAsStringAsyncTask.Wait(); var contentString = readAsStringAsyncTask.Result; apiResponse = JsonConvert.DeserializeObject <ApiResponse>(contentString); status = apiResponse.Status; }while (status != "succeeded"); var pageRect = new PageRect { Height = softwareBitmap.PixelHeight, Width = softwareBitmap.PixelWidth, LineTexts = apiResponse.AnalyzeResult.ReadResults.SelectMany(readResult => { return(readResult.Lines.Select(line => { return new LineText { Text = line.Text, Y = line.BoundingBox[1], X = line.BoundingBox[0] }; })); }).ToList() }; return(pageRect); }