wojkxlq 发表于 2015-5-22 13:25:54

Windows phone 8 二维码生成与扫描

  1. 二维码的生成
  二维码生成用到了一个第三方的插件(zxing.wp8.0)

  根据指定的信息,生成对应的二维码。
  代码很简单:
  
  bool falg=tbk.Text==""?false:true;
            if (falg==false)
            {
                MessageBox.Show("message lose, can't produce!");
                return;
            }
            EncodingOptions options;//包含一些编码、大小等的设置
            BarcodeWriter write = null;//用来生成二维码,对应的BarcodeReader用来解码
            options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8",
                Width = 300,
                Height = 300,
                Margin = 3
            };
            write = new BarcodeWriter();
            write.Format = BarcodeFormat.QR_CODE;
            write.Options = options;
            
            WriteableBitmap bitmap = write.Write(tbk.Text.Trim());
            imgCode.Source = bitmap;
  
  下面看下二维码的扫描(同样用的一个第三方的插件 Silverlight_ZXing_Core)
  直接上代码
  protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)      
  {            
  _reader = new QRCodeReader();               
  _photoCamera = new PhotoCamera();               
  _photoCamera.Initialized += new EventHandler(cam_Initialized);               _videoBrush.SetSource(_photoCamera);               
  BarCodeRectInitial();               
  base.OnNavigatedTo(e);            
  }
  
  //释放资源
  protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
      {
            if (_photoCamera != null)
            {
                _timer.Stop();
                _photoCamera.CancelFocus();
                _photoCamera.Dispose();
            }
            
            base.OnNavigatingFrom(e);
      }
  
  //初始化
  void cam_Initialized(object sender, CameraOperationCompletedEventArgs e)
{
            int width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);
            int height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);
            _luminance = new PhotoCameraLuminanceSource(width, height);
            
            Dispatcher.BeginInvoke(() =>
            {
                _previewTransform.Rotation = _photoCamera.Orientation;
                _timer.Start();
            });
            _photoCamera.FlashMode = FlashMode.Auto;
            _photoCamera.Focus();
}
  
  
  
  public void SetStillPicture()      
  {            
  int width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);         
  int height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);            
  int[] PreviewBuffer = new int;            
  _photoCamera.GetPreviewBufferArgb32(PreviewBuffer);
  WriteableBitmap wb = new WriteableBitmap(width, height);            
  PreviewBuffer.CopyTo(wb.Pixels, 0);
  MemoryStream ms = new MemoryStream();            
  wb.SaveJpeg(ms, wb.PixelWidth, wb.PixelHeight, 0, 80);            
  ms.Seek(0, SeekOrigin.Begin);
  BitmapImage bi = new BitmapImage();            
  bi.SetSource(ms);            
  ImageBrush still = new ImageBrush();            
  still.ImageSource = bi;            
  frame.Fill = still;            
  still.RelativeTransform = new CompositeTransform()               { CenterX = 0.5, CenterY = 0.5, Rotation = _photoCamera.Orientation };         
  }
  
  
  private void ScanPreviewBuffer()         
  {              
  try            
  {               
  _photoCamera.GetPreviewBufferY(_luminance.PreviewBufferY);               
  var binarizer = new HybridBinarizer(_luminance);               
  var binBitmap = new BinaryBitmap(binarizer);               
  Result result = _reader.decode(binBitmap);               
  if (result != null)               
  {                     
  _timer.Stop();                  
  SetStillPicture();                  
  BarCodeRectSuccess();                  
  Dispatcher.BeginInvoke(() =>                  
  {                        
  //读取成功,结果存放在result.Text                        
  NavigationService.Navigate(new Uri("/ScanResult.xaml?result=" + result.Text, UriKind.Relative));
  });               
  }               
  else               
  {                  
  _photoCamera.Focus();               
  }            
  }            
  catch            
  {             }         
  }
页: [1]
查看完整版本: Windows phone 8 二维码生成与扫描