コード例 #1
0
ファイル: InkingInApp.cs プロジェクト: mpalka/Libra
        private async Task <InkStrokeContainer> LoadInkFile(StorageFile inkFile, int pageNumber)
        {
            InkStrokeContainer inkStrokeContainer = new InkStrokeContainer();

            try
            {
                using (var inkStream = await inkFile.OpenSequentialReadAsync())
                {
                    await inkStrokeContainer.LoadAsync(inkStream);
                }
                AppEventSource.Log.Debug("InkingInApp: Inking for page " + pageNumber.ToString() + " loaded.");
            }
            catch (Exception e)
            {
                string errorMsg = "Error when loading inking for page " + pageNumber.ToString() + "\n Exception: " + e.Message;
                AppEventSource.Log.Error("In App Inking: " + errorMsg);
                int userResponse = await App.NotifyUserWithOptions(errorMsg, new string[] { "Remove Inking", "Ignore" });

                switch (userResponse)
                {
                case 0:     // Delete inking file
                    await inkFile.DeleteAsync();

                    AppEventSource.Log.Error("InkingInApp: File deleted.");
                    break;

                default: break;
                }
            }
            return(inkStrokeContainer);
        }
コード例 #2
0
        private async void BtnOpen_Click(object sender, RoutedEventArgs e)
        {
            InkStrokeContainer container = new InkStrokeContainer();

            Windows.Storage.Pickers.FileOpenPicker openPicker = new Windows.Storage.Pickers.FileOpenPicker();
            openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
            openPicker.FileTypeFilter.Add(".gif");

            Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();

            if (file != null)
            {
                IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

                using (var inputStream = stream.GetInputStreamAt(0))
                {
                    await container.LoadAsync(inputStream);
                }
                stream.Dispose();
                _inkStrokes.Clear();

                _inkStrokes.Add(container);
                DrawingCanvas.Invalidate();
            }


            else
            {
            }
        }
コード例 #3
0
        public async Task <bool> LoadInkFileAsync(StorageFile file)
        {
            try
            {
                if (file == null)
                {
                    return(false);
                }

                ClearStrokesSelection();
                ClearStrokes();

                using (var stream = await file.OpenSequentialReadAsync())
                {
                    await _strokeContainer.LoadAsync(stream);
                }

                LoadInkFileEvent?.Invoke(this, EventArgs.Empty);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
コード例 #4
0
        private static async void AddStrokeCountToRecords(List <InkSample> records)
        {
            foreach (InkSample record in records)
            {
                string hexInput = record.ISF;
                try
                {
                    InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
                    using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0)))
                    {
                        writer.WriteBytes(HexStringToByteArray(hexInput));
                        await writer.StoreAsync();

                        var iSC = new InkStrokeContainer();

                        await iSC.LoadAsync(stream);


                        record.StrokeCount = iSC.GetStrokes().Count;
                    }
                    stream.Dispose();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error parsing ISF!");
                }
            }
        }
コード例 #5
0
        private async void OnOpenProjectClick(object sender, RoutedEventArgs e)
        {
            TemplateChoice templateChoice = TemplateChoice.None;
            //Let the user pick a project folder to open
            FolderPicker folderPicker = new FolderPicker();

            folderPicker.FileTypeFilter.Add("*");

            StorageFolder folder = await folderPicker.PickSingleFolderAsync();

            if (folder != null)
            {
                // Get all the files in the project folder and loop through them
                IReadOnlyList <StorageFile> files = await folder.GetFilesAsync();

                foreach (var f in files)
                {
                    // For each file that matches the files we are looking for, do operations
                    if (f.Name.Equals("metadata.txt")) // Templates
                    {
                        string text = await FileIO.ReadTextAsync(f);

                        templateChoice = (TemplateChoice)Enum.Parse(typeof(TemplateChoice), text);
                    }
                    else if (f.Name.Equals("components.txt")) // Shapes
                    {
                        // read file load shapes
                        string text = await FileIO.ReadTextAsync(f);

                        string[] xmlComponents = text.Split('\n');

                        foreach (string component in xmlComponents)
                        {
                            if (component.Length > 0)
                            {
                                components.Add(Serializer.Deserialize <CanvasComponent>(component));
                            }
                        }
                    }
                    else if (f != null && f.FileType.Equals(".gif")) // .gif are strokes
                    {
                        // Open a file stream for reading.
                        IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);

                        // Read from file.
                        using (var inputStream = stream.GetInputStreamAt(0))
                        {
                            var container = new InkStrokeContainer();
                            await container.LoadAsync(inputStream);

                            //Add the strokes stored in the files
                            strokes.Add(container);
                        }
                        stream.Dispose();
                    }
                }
                this.Frame.Navigate(typeof(MainCanvas), new MainCanvasParams(strokes, folder, templateChoice, components));
            }
        }
コード例 #6
0
        public async Task ReadInkAsync(InkStrokeContainer strokeContainer)
        {
            strokeContainer.Clear();

            using (Stream stream = await _folder.OpenStreamForReadAsync(_name + Serializer.InkFileExtension))
            {
                await strokeContainer.LoadAsync(stream.AsInputStream());
            }
        }
コード例 #7
0
        private async Task LoadInkAsync(StorageFolder coloringDirectory)
        {
            InkStrokeContainer = new InkStrokeContainer();
            var fileName = ColoringName + Tools.GetResourceString("FileType/inkFileType");
            var file     = await Tools.GetFileAsync(coloringDirectory, fileName);

            if (file != null)
            {
                using (var stream = await file.OpenSequentialReadAsync())
                {
                    await InkStrokeContainer.LoadAsync(stream);
                }
            }
        }
コード例 #8
0
        private async Task loadContentAsync()
        {
            if (_pdfDocument != null)
            {
                ContentBitmaps.Clear();
                PageIndex = default(uint);
                PageCount = _pdfDocument.PageCount;
                var overlays = await LoadOverlayAsync();

                for (uint idx = 0; idx < PageCount; idx++)
                {
                    using (var page = _pdfDocument.GetPage(idx))
                    {
                        var bitmapImage = new BitmapImage();
                        using (var bmpStream = new InMemoryRandomAccessStream())
                        {
                            await page.RenderToStreamAsync(bmpStream);

                            await bitmapImage.SetSourceAsync(bmpStream);
                        }

                        var inkStrokes = new InkStrokeContainer();
                        if (overlays.ContainsKey(idx))
                        {
                            using (var inkStream = new InMemoryRandomAccessStream())
                            {
                                await inkStream.WriteAsync(overlays[idx].AsBuffer());

                                await inkStrokes.LoadAsync(inkStream.GetInputStreamAt(0));
                            }
                        }

                        ContentBitmaps.Add(
                            new SimpleFlipPage
                        {
                            Index      = idx,
                            Image      = bitmapImage,
                            InkStrokes = inkStrokes
                        }
                            );
                    }
                }
            }
        }
コード例 #9
0
        public async void ReadXml(XmlReader reader)
        {
            reader.Read();
            Title       = reader.ReadElementContentAsString("Title", "");
            Description = reader.ReadElementContentAsString("Description", "");
            string s = reader.ReadElementContentAsString("Status", "");

            Status = (JobStatus)Enum.Parse(typeof(JobStatus), s);

            InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
            DataWriter dw = new DataWriter(ms.GetOutputStreamAt(0));

            byte[] tempBytes = new byte[1024];
            int    bytesRead = 0;

            do
            {
                bytesRead = reader.ReadElementContentAsBinHex(tempBytes, 0, 1024);
                if (bytesRead > 0)
                {
                    dw.WriteBytes(tempBytes);
                }
            } while (bytesRead == 1024);

            await dw.StoreAsync();

            if (ms.Size > 0)
            {
                InkStrokeContainer inkCont = new InkStrokeContainer();
                await inkCont.LoadAsync(ms);

                Strokes = inkCont.GetStrokes().ToList();
            }

            reader.Skip();
        }
コード例 #10
0
        public async void ReadXml(XmlReader reader)
        {
            reader.Read();
            Title       = reader.ReadElementContentAsString("Title", "");
            Description = reader.ReadElementContentAsString("Description", "");
            string s = reader.ReadElementContentAsString("Status", "");

            Status = (JobStatus)Enum.Parse(typeof(JobStatus), s);

            if (!reader.IsEmptyElement)
            {
                InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
                DataWriter dw = new DataWriter(ms.GetOutputStreamAt(0));
                byte[]     tempBytes;
                int        bytesRead  = 0;
                int        totalBytes = 0;
                do
                {
                    tempBytes = new byte[1024];
                    bytesRead = reader.ReadElementContentAsBinHex(tempBytes, 0, 1024);
                    if (bytesRead > 0)
                    {
                        dw.WriteBytes(tempBytes);
                        totalBytes += bytesRead;
                    }
                } while (bytesRead == 1024);

                await dw.StoreAsync();

                if (totalBytes > 1)
                {
                    InkStrokeContainer inkCont = new InkStrokeContainer();
                    await inkCont.LoadAsync(ms);

                    Strokes = inkCont.GetStrokes().ToList();
                }
                reader.ReadEndElement();
            }
            else
            {
                reader.Read();
            }

            if (!reader.IsEmptyElement)
            {
                InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
                DataWriter dw = new DataWriter(ms.GetOutputStreamAt(0));
                byte[]     tempBytes;
                int        bytesRead      = 0;
                int        totalBytesRead = 0;
                do
                {
                    tempBytes = new byte[1024];
                    bytesRead = reader.ReadContentAsBinHex(tempBytes, 0, 1024);
                    if (bytesRead > 0)
                    {
                        dw.WriteBytes(tempBytes);
                        totalBytesRead += bytesRead;
                    }
                } while (bytesRead == 1024);

                await dw.StoreAsync();

                if (totalBytesRead > 1)
                {
                    //load bytes as image
                    byte[] bytes = new byte[ms.Size];
                    //var dataWriter = new DataWriter(ms);
                    var dataReader = new DataReader(ms.GetInputStreamAt(0));

                    await dataReader.LoadAsync((uint)ms.Size);

                    dataReader.ReadBytes(bytes);
                    //TODO: this should change based on the resolution you store the photos at
                    Photo = new SoftwareBitmap(BitmapPixelFormat.Bgra8, 640, 360);
                    Photo.CopyFromBuffer(bytes.AsBuffer());
                }
                reader.ReadEndElement();
            }
            else
            {
                reader.Read();
            }


            reader.Skip();
        }
コード例 #11
0
        /// <summary>
        /// Load the family and their notes from local storage
        /// </summary>
        /// <returns>Null if there was no model to load, otherwise, the deserialized model</returns>
        private async Task <Model> LoadModelAsync()
        {
            Model model = null;

            InkStrokeContainer combinedStrokes     = new InkStrokeContainer(); // To avoid managing individual files for every InkCanvas, we will combine all ink stroke information into one container
            List <int>         InkStrokesPerCanvas = new List <int>();

            try
            {
                StorageFile modelDataFile = await ApplicationData.Current.LocalFolder.GetFileAsync(NOTES_MODEL_FILE);

                using (IRandomAccessStream randomAccessStream = await modelDataFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    // Load the model which contains the people and the note collection
                    try
                    {
                        DataContractJsonSerializer modelSerializer = new DataContractJsonSerializer(typeof(Model));
                        model = (Model)modelSerializer.ReadObject(randomAccessStream.AsStream());
                    }
                    catch (System.Runtime.Serialization.SerializationException)
                    {
                        System.Diagnostics.Debug.Assert(false, "Failed to load serialized model");
                        return(null);
                    }
                }

                // For each sticky note, load the number of inkstrokes it contains
                StorageFile inkDataFile = await ApplicationData.Current.LocalFolder.GetFileAsync(NOTES_INK_FILE);

                using (IInputStream inkStream = await inkDataFile.OpenSequentialReadAsync())
                {
                    bool       combinedStrokesExist = false;
                    DataReader reader = new DataReader(inkStream);
                    foreach (StickyNote n in model.StickyNotes)
                    {
                        await reader.LoadAsync(sizeof(int)); // You need to buffer the data before you can read from a DataReader.

                        int numberOfInkStrokes = reader.ReadInt32();
                        InkStrokesPerCanvas.Add(numberOfInkStrokes);
                        combinedStrokesExist |= numberOfInkStrokes > 0;
                    }

                    // Load the ink data
                    if (combinedStrokesExist)
                    {
                        await combinedStrokes.LoadAsync(inkStream);
                    }
                } // using inkStream
            }     // try
            catch (FileNotFoundException)
            {
                // No data to load. We'll start with a fresh model
                return(null);
            }

            // Factor out the inkstrokes from the big container into each note
            int allStrokesIndex = 0, noteIndex = 0;
            IReadOnlyList <InkStroke> allStrokes = combinedStrokes.GetStrokes();

            foreach (StickyNote n in model.StickyNotes)
            {
                // InkStrokeContainers can't be serialized using the default xml/json serialization.
                // So create a new one and fill it up from the data we restored
                n.Ink = new InkStrokeContainer();
                // pull out the ink strokes that belong to this note
                for (int i = 0; i < InkStrokesPerCanvas[noteIndex]; i++)
                {
                    n.Ink.AddStroke(allStrokes[allStrokesIndex++].Clone());
                }
                ++noteIndex;
            }

            return(model);
        }
コード例 #12
0
        public async Task <MainCanvasParams> OpenProject(List <InkStrokeContainer> currentStrokes, StorageFolder currentFolder, ProjectMetaData metaData, List <CanvasComponent> components)
        {
            List <InkStrokeContainer> newStrokes     = new List <InkStrokeContainer>();
            TemplateChoice            templateChoice = TemplateChoice.None;
            //Let the user pick a project folder to open
            FolderPicker folderPicker = new FolderPicker();

            folderPicker.FileTypeFilter.Add("*");

            StorageFolder newFolder = await folderPicker.PickSingleFolderAsync();

            if (newFolder != null)
            {
                IReadOnlyList <StorageFile> files = await newFolder.GetFilesAsync();

                foreach (var f in files)
                {
                    if (f.Name.Equals("metadata.txt"))
                    {
                        string text = await FileIO.ReadTextAsync(f);

                        templateChoice = (TemplateChoice)Enum.Parse(typeof(TemplateChoice), text);
                    }
                    else if (f.Name.Equals("components.txt"))
                    {
                        // read file load shapes
                        string text = await FileIO.ReadTextAsync(f);

                        string[] xmlComponents = text.Split('\n');

                        foreach (string component in xmlComponents)
                        {
                            if (component.Length > 0)
                            {
                                components.Add(Serializer.Deserialize <CanvasComponent>(component));
                            }
                        }
                    }
                    else if (f != null && f.FileType.Equals(".gif"))
                    {
                        // Open a file stream for reading.
                        IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);

                        // Read from file.
                        using (var inputStream = stream.GetInputStreamAt(0))
                        {
                            var container = new InkStrokeContainer();
                            await container.LoadAsync(inputStream);

                            //Add the strokes stored in the files
                            newStrokes.Add(container);
                        }
                        stream.Dispose();
                    }
                }
                var result = await ConfirmSave(currentStrokes, currentFolder, metaData, components);

                if (result != ContentDialogResult.None)
                {
                    return(new MainCanvasParams(newStrokes, newFolder, templateChoice, components));
                }
            }
            return(null);
        }