Exemplo n.º 1
0
        protected async System.Threading.Tasks.Task TakeSnapshotAsync <T>(T list, string fieldName) where T : ObservableCollection <ImageCapture>
        {
            try
            {
                CameraCaptureUI ccui        = new CameraCaptureUI();
                var             storagefile = await ccui.CaptureFileAsync(CameraCaptureUIMode.Photo);

                if (storagefile != null)
                {
                    var ms = await RenderDataStampOnSnap.RenderStaticTextToBitmap(storagefile);

                    var    msrandom = new MemoryRandomAccessStream(ms);
                    Byte[] bytes    = new Byte[ms.Length];
                    await ms.ReadAsync(bytes, 0, (int)ms.Length);

                    // StorageFile file = await KnownFolders.PicturesLibrary.CreateFileAsync("Image.png", Windows.Storage.CreationCollisionOption.GenerateUniqueName);

                    StorageFile file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(DateTime.Now.Ticks.ToString() + storagefile.Name, CreationCollisionOption.ReplaceExisting);

                    using (var strm = await file.OpenStreamForWriteAsync())
                    {
                        await strm.WriteAsync(bytes, 0, bytes.Length);

                        strm.Flush();
                    }

                    var ic = new ImageCapture
                    {
                        ImagePath        = file.Path,
                        ImageBinary      = Convert.ToBase64String(bytes),
                        CaseServiceRecId = ((BaseModel)this.Model).VehicleInsRecID,
                        FileName         = string.Format("{0}_{1}", fieldName, list.Count + 1)
                    };
                    list.Add(ic);

                    var imageTable = await SqliteHelper.Storage.LoadTableAsync <ImageCapture>();

                    var dbIC = imageTable.SingleOrDefault(x => x.CaseServiceRecId == ic.CaseServiceRecId && x.FileName == string.Format("{0}_{1}", fieldName, list.Count + 1));
                    if (dbIC == null)
                    {
                        await SqliteHelper.Storage.InsertSingleRecordAsync <ImageCapture>(ic);
                    }
                    else
                    {
                        dbIC.ImagePath   = ic.ImagePath;
                        dbIC.ImageBinary = ic.ImageBinary;
                        await SqliteHelper.Storage.UpdateSingleRecordAsync <ImageCapture>(dbIC);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 2
0
        public static async Task <BitmapImage> ConvertByteToImage(byte[] imageBytes)
        {
            if (imageBytes != null)
            {
                MemoryStream stream             = new MemoryStream(imageBytes);
                var          randomAccessStream = new MemoryRandomAccessStream(stream);
                BitmapImage  bitmapImage        = new BitmapImage();
                await bitmapImage.SetSourceAsync(randomAccessStream);

                return(bitmapImage);
            }
            else
            {
                return(Item.DefaultIcon);
            }
        }
Exemplo n.º 3
0
        public ImageMarkupPageViewModel()
        {
            this.Snapshots = new ObservableCollection <ImageCapture>();

            DoneCommand = DelegateCommand.FromAsyncHandler(async() =>
            {
                try
                {
                    var caseNumber = ApplicationData.Current.LocalSettings.Values["CaseNumber"].ToString();
                    foreach (var item in this.Snapshots)
                    {
                        var image = await StorageFile.GetFileFromPathAsync(item.ImagePath);
                        var curMarkupImageFile = await ApplicationData.Current.RoamingFolder.TryGetItemAsync("markupimage_" + caseNumber + this.Snapshots.IndexOf(item)) as StorageFile;
                        if (image != null && curMarkupImageFile != null)
                        {
                            var ms       = await RenderDataStampOnSnap.InterpolateImageMarkup(image, curMarkupImageFile);
                            var msrandom = new MemoryRandomAccessStream(ms);
                            Byte[] bytes = new Byte[ms.Length];
                            await ms.ReadAsync(bytes, 0, (int)ms.Length);
                            // StorageFile file = await KnownFolders.PicturesLibrary.CreateFileAsync("Image.png", Windows.Storage.CreationCollisionOption.GenerateUniqueName);

                            StorageFile file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(DateTime.Now.Ticks.ToString() + curMarkupImageFile.Name, CreationCollisionOption.ReplaceExisting);
                            using (var strm = await file.OpenStreamForWriteAsync())
                            {
                                await strm.WriteAsync(bytes, 0, bytes.Length);
                                strm.Flush();
                            }

                            await UpdateImageAsync(new ImageCapture
                            {
                                CaseServiceRecId = Convert.ToInt64(ApplicationData.Current.LocalSettings.Values["VehicleInsRecId"]),
                                ImageBinary      = Convert.ToBase64String(bytes),
                                ImagePath        = file.Path,
                                FileName         = item.FileName + "Marked"
                            });
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            });
        }
Exemplo n.º 4
0
        async public virtual System.Threading.Tasks.Task TakePictureAsync(ImageCapture param, string fieldName)
        {
            try
            {
                CameraCaptureUI cam = new CameraCaptureUI();

                var storagefile = await cam.CaptureFileAsync(CameraCaptureUIMode.Photo);

                if (storagefile != null)
                {
                    using (MemoryStream ms = await RenderDataStampOnSnap.RenderStaticTextToBitmap(storagefile))
                    {
                        using (var msrandom = new MemoryRandomAccessStream(ms))
                        {
                            Byte[] bytes = new Byte[ms.Length];
                            await ms.ReadAsync(bytes, 0, (int)ms.Length);

                            // StorageFile file = await KnownFolders.PicturesLibrary.CreateFileAsync("Image.png", Windows.Storage.CreationCollisionOption.GenerateUniqueName);

                            StorageFile file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(DateTime.Now.Ticks.ToString() + storagefile.Name, CreationCollisionOption.ReplaceExisting);

                            using (var strm = await file.OpenStreamForWriteAsync())
                            {
                                await strm.WriteAsync(bytes, 0, bytes.Length);

                                strm.Flush();
                            }

                            param.ImagePath        = file.Path;
                            param.ImageBinary      = Convert.ToBase64String(bytes);
                            param.CaseServiceRecId = ((BaseModel)this.Model).VehicleInsRecID;
                            param.FileName         = fieldName;

                            await UpdateImageAsync(param);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 5
0
 public static async Task Render(this WriteableBitmap wb, FrameworkElement fe)
 {
     var ms       = RenderToStream(fe);
     var msrandom = new MemoryRandomAccessStream(ms);
     await wb.SetSourceAsync(msrandom);
 }
Exemplo n.º 6
0
        private async void ReadAndSort(Windows.UI.Core.CoreDispatcher Dispatcher)
        {
            //Identify that it is ID3
            if (GetString(3).Equals("ID3"))
            {
                //Unwanted space between "ID3" tag and frame name
                for (int i = 0; i < 7; i++)
                    data.ReadByte();

                //Loop through the rest of the file, looking directly for specific frame names, reading their properties, and scrapping the rest.
                while (data.UnconsumedBufferLength > 3)
                {
                    //Use our Frame Name constant (4) to seek the identity of the tag (if it's one we care about).
                    //Otherwise, we loop through the bytes we don't care about.
                    String identity = GetString(ID3FrameNameLength);
                    identity = new ID3FrameDictionary().GetIdentity(identity);

                    //ID3 -- assuming no one took liberties extending this during encoding -- reserves 4 bytes following
                    //the frame name to tell decoders
                    byte[] byteCounter = new byte[4] { 0, 0, 0, 0 };
                    for (int i = 0; i < 4; i++)
                    {
                        byteCounter[i] = data.ReadByte();
                    }

                    //Since we're dealing with encoding and dealing primarily with Hex values, all values should be unsigned. 
                    //I don't actually know is there is padding between the frame name and the value count. If there is, this following operation doesn't hurt anything
                    //and is set in case the 4 bytes following are all possibly used. Accounted for by 64-bit integer.
                    UInt64 intCounter = (ulong)((byteCounter[0] << (8 * 3)) + (byteCounter[1] << (8 * 2)) + (byteCounter[2] << (8 * 1)) + (byteCounter[3] << (8 * 0)));
                    

                    //Padding of 00 00 between the 64-bit value count and the frame value. Read through the padding.
                    data.ReadByte();
                    data.ReadByte();

                    //Check if frame name is one we care about. (Album, Artist, Title, Cover)
                    if (identity != null)
                    {
                        //APIC (Album Picture) is the only frame that we could care about that can't be interpreted as a string.
                        if (identity.Equals("Image"))
                        {
                            //We get the byte array, but WinRT/Windows 8 makes the process of converting a byte array into a Random Access Stream difficult.
                            //A custom library was made for this that converts a Stream (MemoryStream) into IRandomAccessStream, so we can create a bitmap image.
                            MemoryStream ms = new MemoryStream(GetImage(intCounter));
                            MemoryRandomAccessStream randomAccessStream = new MemoryRandomAccessStream(ms);
                            
                            //Create new BitmapImage inside this thread, as it isn't possible to know that this variable will come out of the UI thread operation.
                            BitmapImage bt = null;

                            //Grab UI Thread to create a new BitmapImage instance, and to take care of operations involving the UI.
                            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High,
                             async () =>
                             {
                                 bt = new BitmapImage();
                                 try
                                 {
                                     await bt.SetSourceAsync(randomAccessStream);
                                     cover.Source = bt;
                                     cover.MaxWidth = bt.PixelWidth;
                                     cover.MaxHeight = bt.PixelHeight;
                                 }
                                 catch (Exception e)
                                 {
                                     //In case setting the source throws an exception due to corrupt extraction.
                                 }
                             });
                        }
                        else
                        {
                            //Convert the bytes following the frame value count into a readable string. It skips over characters that aren't used in the English Language.
                            String idValue = GetString(intCounter);
                            fileData[identity] = idValue;
                            Debug.WriteLine(idValue);
                        }
                    }
                    else
                    {
                        //Read over the bytes we don't care about. If there is some sort of wrong interpretation of the intCounter value, the loop is broken
                        //so there is no attempt of reading beyond the file's length.
                        if (intCounter > data.UnconsumedBufferLength)
                            break;
                        for (uint i = 0; i < intCounter; i++)
                            data.ReadByte();
                    }
                }
            }
            //We don't need this anymore. Let's dispose of it.
            data.Dispose();
        }