none
WindowsPhone8.1 开发二维码扫描的问题 RRS feed

  • 常规讨论

  • 在WP8.1中解析二维码图片 我采用的是和winrt的方法 

    但是遇到了一个问题

    1.我采用 CapturePhotoToStorageFileAsync() 将图片保存到本地 在读取的方式 是不会出现问题

       
     try
                {
                    while (_result == null)
                    {
                        if (!IsBusy)
                        {
                            IsBusy = true;
                           
                            var photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync("qcode.jpg", CreationCollisionOption.ReplaceExisting);
                            await _mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoStorageFile);
    
                            var stream = await photoStorageFile.OpenReadAsync();
                            // initialize with 1,1 to get the current size of the image
                            var writeableBmp = new WriteableBitmap(1, 1);
                            writeableBmp.SetSource(stream);
                            // and create it again because otherwise the WB isn't fully initialized and decoding
                            // results in a IndexOutOfRange
                            writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);
                            stream.Seek(0);
                            writeableBmp.SetSource(stream);
                            _result = ScanBitmap(writeableBmp);
                            await photoStorageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
                        }
                        if (_result != null)
                        {
                            await _mediaCapture.StopPreviewAsync();
                            VideoCapture.Visibility = Visibility.Collapsed;
                            CaptureImage.Visibility = Visibility.Visible;
                            ScanResult.Text = _result.Text;
                            _timer.Stop();
                            myStoryboard.Stop();
                            imgScan.Visibility = Visibility.Collapsed;
                           
                        }  
                        IsBusy = false;
                     
                    }
                }
                catch (Exception ex)
                {
                   
                    IsBusy = false;
                    //Error.Text = ex.Message;
                }

    这种方法不会出现问题 

    2但是我如果采用CapturePhotoToStreamAsync()直接获取图片流在解析二维码图片就出现了问题

     
    try
                {
                    while (_result == null)
                    {
                       
                        IRandomAccessStream stream = new InMemoryRandomAccessStream();
                        await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreatePng(), stream);
                        await stream.FlushAsync();
                        stream.Seek(0);
                    
                        WriteableBitmap writeableBmp = new WriteableBitmap(640, 480);
                        writeableBmp.SetSource(stream);
                       if (writeableBmp != null)
                       {
                           _result = ScanBitmap(writeableBmp);
                           if (_result != null)
                           {
                               await _mediaCapture.StopPreviewAsync();
                               VideoCapture.Visibility = Visibility.Collapsed;
                               CaptureImage.Visibility = Visibility.Visible;
                               ScanResult.Text = _result.Text;
                               //_timer.Stop();
                               myStoryboard.Stop();
                               imgScan.Visibility = Visibility.Collapsed;
                           }
                       }
                    }
                }
                catch (Exception ex)
                {
     
               
                }

    我的详细代码地址:http://www.cnblogs.com/fxiaoquan/p/4030836.html

    请大家分析一下问什么?


    爱编程 爱生活

    2014年10月17日 9:45