C# 地铁应用程序条形码扫描仪

C# 地铁应用程序条形码扫描仪,c#,windows-8,windows-runtime,microsoft-metro,C#,Windows 8,Windows Runtime,Microsoft Metro,我想开发一个用于扫描条形码的metro应用程序。是否存在用于条形码扫描的默认库?请向我提供指南和示例应用程序请在和上查看ZXing.Net。CodePlex的源代码已被删除 MainPage.xaml <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Canvas Width="640" Height="360"> <CaptureElement x:Na

我想开发一个用于扫描条形码的metro应用程序。是否存在用于条形码扫描的默认库?请向我提供指南和示例应用程序

请在和上查看ZXing.Net。CodePlex的源代码已被删除

MainPage.xaml

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
  <Canvas Width="640" Height="360">
     <CaptureElement x:Name="VideoCapture" Width="640" Height="360" />
     <Image x:Name="CaptureImage" Width="640" Height="360" Visibility="Collapsed" />
  </Canvas>
  <TextBlock x:Name="Error" VerticalAlignment="Bottom" FontSize="32" Width="640" TextAlignment="Center" Margin="363,0,363,37" />
  <TextBlock x:Name="ScanResult" VerticalAlignment="Bottom" TextAlignment="Center" FontSize="32" Width="640"/>
</Grid>

MainPage.xaml.cs

public sealed partial class MainPage : Page
{
  private readonly MediaCapture _mediaCapture = new MediaCapture();
  private Result _result;

  public MainPage()
  {
     InitializeComponent();
  }

  protected override async void OnNavigatedTo(NavigationEventArgs e)
  {
     try
     {
        var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
        if (cameras.Count < 1)
        {
           Error.Text = "No camera found, decoding static image";
           await DecodeStaticResource();
           return;
        }
        MediaCaptureInitializationSettings settings;
        if (cameras.Count == 1)
        {
           settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[0].Id }; // 0 => front, 1 => back
        }
        else
        {
           settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[1].Id }; // 0 => front, 1 => back
        }

        await _mediaCapture.InitializeAsync(settings);
        VideoCapture.Source = _mediaCapture;
        await _mediaCapture.StartPreviewAsync();

        while (_result == null)
        {
           var photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync("scan.jpg", CreationCollisionOption.GenerateUniqueName);
           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);
        }

        await _mediaCapture.StopPreviewAsync();
        VideoCapture.Visibility = Visibility.Collapsed;
        CaptureImage.Visibility = Visibility.Visible;
        ScanResult.Text = _result.Text;
     }
     catch (Exception ex)
     {
        Error.Text = ex.Message;
     }
  }

  private async System.Threading.Tasks.Task DecodeStaticResource()
  {
     var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\1.jpg");
     var stream = await file.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);
     CaptureImage.Source = writeableBmp;
     VideoCapture.Visibility = Visibility.Collapsed;
     CaptureImage.Visibility = Visibility.Visible;

     _result = ScanBitmap(writeableBmp);
     if (_result != null)
     {
        ScanResult.Text += _result.Text;
     }
     return;
  }

  private Result ScanBitmap(WriteableBitmap writeableBmp)
  {
     var barcodeReader = new BarcodeReader
     {
        TryHarder = true,
        AutoRotate = true
     };
     var result = barcodeReader.Decode(writeableBmp);

     if (result != null)
     {
        CaptureImage.Source = writeableBmp;
     }

     return result;
  }

  protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
  {
     await _mediaCapture.StopPreviewAsync();
  }
}
公共密封部分类主页面:第页
{
私有只读媒体捕获_MediaCapture=new MediaCapture();
私人结果(u Result);;
公共主页()
{
初始化组件();
}
受保护的覆盖异步无效OnNavigatedTo(NavigationEventArgs e)
{
尝试
{
var摄像机=等待设备信息.FindAllAsync(DeviceClass.VideoCapture);
如果(照相机计数<1)
{
错误。Text=“未找到摄像头,正在解码静态图像”;
等待解码静态资源();
返回;
}
MediaCaptureInitializationSettings;
如果(cameras.Count==1)
{
settings=new MediaCaptureInitializationSettings{VideoDeviceId=cameras[0].Id};//0=>前,1=>后
}
其他的
{
settings=new MediaCaptureInitializationSettings{VideoDeviceId=cameras[1].Id};//0=>front,1=>back
}
等待mediaCapture.InitializeAsync(设置);
VideoCapture.Source=\u mediaCapture;
等待_mediaCapture.startPreviewSync();
while(_result==null)
{
var photoStorageFile=await KnownFolders.PicturesLibrary.CreateFileAsync(“scan.jpg”,CreationCollisionOption.GenerateUniqueName);
wait_mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(),photoStorageFile);
var stream=wait photoStorageFile.OpenReadAsync();
//使用1,1初始化以获取图像的当前大小
var writeableBmp=新的WriteableBitmap(1,1);
writeableBmp.SetSource(流);
//并再次创建它,因为否则WB没有完全初始化和解码
//结果是一个指数崩盘
writeableBmp=新的WriteableBitmap(writeableBmp.PixelWidth,writeableBmp.PixelHeight);
stream.Seek(0);
writeableBmp.SetSource(流);
_结果=扫描位图(可写位图);
等待photoStorageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
等待_mediaCapture.stoppereviewasync();
VideoCapture.Visibility=Visibility.Collapsed;
CaptureImage.Visibility=可见性.Visibility;
ScanResult.Text=_result.Text;
}
捕获(例外情况除外)
{
错误。文本=例如消息;
}
}
专用异步System.Threading.Tasks.Task DecodeStaticResource()
{
var file=wait Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@“Assets\1.jpg”);
var stream=await file.OpenReadAsync();
//使用1,1初始化以获取图像的当前大小
var writeableBmp=新的WriteableBitmap(1,1);
writeableBmp.SetSource(流);
//并再次创建它,因为否则WB没有完全初始化和解码
//结果是一个指数崩盘
writeableBmp=新的WriteableBitmap(writeableBmp.PixelWidth,writeableBmp.PixelHeight);
stream.Seek(0);
writeableBmp.SetSource(流);
CaptureImage.Source=writeableBmp;
VideoCapture.Visibility=Visibility.Collapsed;
CaptureImage.Visibility=可见性.Visibility;
_结果=扫描位图(可写位图);
如果(_result!=null)
{
ScanResult.Text+=\u result.Text;
}
返回;
}
私有结果扫描位图(WriteableBitmap writeableBmp)
{
var barcodereder=新的条形码阅读器
{
TryHarder=true,
自动旋转=真
};
var结果=条形码阅读器.Decode(writeableBmp);
如果(结果!=null)
{
CaptureImage.Source=writeableBmp;
}
返回结果;
}
受保护的覆盖异步无效OnNavigatingFrom(NavigatingCancelEventArgs e)
{
等待_mediaCapture.stoppereviewasync();
}
}

Google result:(.net没有默认库,但它工作得很好)这是一个用于.net的条形码扫描仪库