コード例 #1
0
        async partial void AddBarcodePageClick(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 barcode page
                var textBlock = new TextBlock(PageRect.Create(0, 0, 230, 40), TextBlockFont.Small);
                textBlock.ElementId           = 10;
                textBlock.Baseline            = 25;
                textBlock.HorizontalAlignment = HorizontalAlignment.Center;
                textBlock.BaselineAlignment   = TextBlockBaselineAlignment.Relative;
                textBlock.AutoWidth           = false;

                var barcode = new Barcode(PageRect.Create(0, 5, 230, 95), BarcodeType.Code39);
                barcode.ElementId = 11;

                var flowPanel = new FlowPanel(PageRect.Create(15, 0, 260, 105));
                flowPanel.AddElement(textBlock);
                flowPanel.AddElement(barcode);

                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, "Barcode value: A1 B", out operationError),
                        BarcodeData.Create(barcode.ElementId, BarcodeType.Code39, "A1 B", 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....");
            }
        }
コード例 #2
0
        /// <summary>
        ///     Poll the native API for barcode scanner results.
        /// </summary>
        /// <returns>
        ///     An array of BarcodeData that contains the results
        ///     that the scanner has collected since the last call to this function. This array
        ///     may be empty if there are no new results.
        /// </returns>
        private static List <BarcodeData> MLBarcodeScannerGetResults()
        {
            try
            {
                // get results from native api
                MLResult.Code resultCode = NativeBindings.MLBarcodeScannerGetResult(Instance.Handle, out NativeBindings.MLBarcodeScannerResultArray scannerResults);

                if (MLResult.IsOK(resultCode))
                {
                    var managedResults = new List <BarcodeData>((int)scannerResults.Count);

                    for (int i = 0; i < scannerResults.Count; i++)
                    {
                        // marshal native array into native structs
                        long address = scannerResults.Detections.ToInt64() + (Marshal.SizeOf <IntPtr>() * i);
                        NativeBindings.MLBarcodeScannerResult detectedResult = Marshal.PtrToStructure <NativeBindings.MLBarcodeScannerResult>(Marshal.ReadIntPtr(new IntPtr(address)));
                        MLPluginLog.Debug($"MLBarcodeScanner results found: {detectedResult}");

                        // create managed version of data
                        UnityEngine.Pose pose;
                        if (((BarcodeType)detectedResult.Type) == BarcodeType.QR)
                        {
                            if (!MagicLeapNativeBindings.UnityMagicLeap_TryGetPose(detectedResult.CoordinateFrameUID, out pose))
                            {
                                MLPluginLog.Error($"Barcode Scanner could not get pose data for coordinate frame id '{detectedResult.CoordinateFrameUID}'");
                                pose = Pose.identity;
                            }
                        }
                        else
                        {
                            pose = Pose.identity;
                        }

                        managedResults.Add
                        (
                            BarcodeData.Create
                            (
                                (BarcodeType)detectedResult.Type,
                                pose,
                                detectedResult.DecodedData.Data,
                                detectedResult.DecodedData.Size,
                                detectedResult.ReprojectionError
                            )
                        );
                    }

                    // release native memory so results can be polled again
                    if (MLResult.IsOK(NativeBindings.MLBarcodeScannerReleaseResult(scannerResults)))
                    {
                        return(managedResults);
                    }
                    else
                    {
                        MLPluginLog.Error($"MLBarcodeScanner.NativeBindings.MLBarcodeScannerReleaseResult failed when trying to release the results' memory. Reason: {MLResult.CodeToString(resultCode)}");
                        return(managedResults);
                    }
                }
                else
                {
                    MLPluginLog.Error($"MLBarcodeScanner.MLBarcodeScannerGetResult failed to obtain a result. Reason: {resultCode}");
                    return(default);