QRScanner.xaml.cs CampusAppWP8::Pages::Dev::QRScanner CampusAppWP8::Pages::Dev Microsoft::Devices ZXing //----------------------------------------------------------------------- //<copyrightfile="QRScanner.xaml.cs"company="BTU/IIT"> //TheMITLicense(MIT).Copyright(c)2013BTU/IIT. //</copyright> //<author>fiedlchr</author> //<date>15.10.2013</date> //<summary>Implementstheqrscanner.xamlclass</summary> //<remarks> //ThisClassusestheZXinglibrary,whichisundertheApacheLicense2.0. // //Therefor,see: //http://zxingnet.codeplex.com/license //http://www.apache.org/licenses/LICENSE-2.0 //</remarks> //----------------------------------------------------------------------- namespaceCampusAppWP8.Pages.Dev { usingSystem; usingSystem.Threading; usingSystem.Windows; usingSystem.Windows.Media.Imaging; usingCampusAppWP8.Model.Setting; usingCampusAppWP8.Resources; usingCampusAppWP8.Utility; usingCampusAppWP8.Utility.Lui.MessageBoxes; usingMicrosoft.Devices; usingMicrosoft.Phone.Controls; usingZXing; usingSystem.Windows.Navigation; publicpartialclassQRScanner:PhoneApplicationPage { #regionMember privatePhotoCameracam=null; privateThreadcaptureThread=null; privatevolatileboolcaptureThreadExit=false; privateIBarcodeReaderbarcodeReader=null; privateWriteableBitmapbit=null; privateboolisInAutofocus=false; privatestringresultAppStoreKey; #endregion #regionConstructor publicQRScanner() { this.InitializeComponent(); this.barcodeReader=newBarcodeReader(); } #endregion #regionMethod #regionprotected protectedoverridevoidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgse) { if(PhotoCamera.IsCameraTypeSupported(CameraType.Primary)==true) { if(NavigationContext.QueryString.ContainsKey(Constants.ParamQRResultKey)) { this.resultAppStoreKey=NavigationContext.QueryString[Constants.ParamQRResultKey]; } if(e.NavigationMode==NavigationMode.Back) { this.captureThreadExit=false; } this.isInAutofocus=true; this.cam=newPhotoCamera(CameraType.Primary); this.cam.Initialized+=newEventHandler<CameraOperationCompletedEventArgs>(this.Cam_Initialized); this.cam.AutoFocusCompleted+=newEventHandler<CameraOperationCompletedEventArgs>(this.Cam_AutoFocusCompl); this.bit=newWriteableBitmap((int)this.cam.PreviewResolution.Width,(int)this.cam.PreviewResolution.Height); this.camViewBrush.SetSource(this.cam); } else { this.Dispatcher.BeginInvoke(delegate { MessageBox.Show(AppResources.PrimCamNotSupported); }); } } protectedoverridevoidOnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgse) { if(this.cam!=null) { this.captureThreadExit=true; if(this.captureThread!=null) { this.captureThread.Join(); } this.cam.Dispose(); this.bit=null; } } protectedoverridevoidOnOrientationChanged(OrientationChangedEventArgse) { base.OnOrientationChanged(e); switch(e.Orientation) { casePageOrientation.Landscape: this.camViewBrushTransform.Rotation=0; break; casePageOrientation.LandscapeLeft: this.camViewBrushTransform.Rotation=0; break; casePageOrientation.LandscapeRight: this.camViewBrushTransform.Rotation=180; break; casePageOrientation.Portrait: this.camViewBrushTransform.Rotation=90; break; casePageOrientation.PortraitUp: this.camViewBrushTransform.Rotation=90; break; casePageOrientation.PortraitDown: this.camViewBrushTransform.Rotation=270; break; } } #endregion #regionprivate privatevoidCam_Initialized(objectsender,Microsoft.Devices.CameraOperationCompletedEventArgse) { if(e.Succeeded) { if(this.cam!=null&&!this.captureThreadExit) { this.cam.Focus(); this.captureThreadExit=false; this.captureThread=newThread(this.CaptureThreadFunc); this.captureThread.Start(); } } } privatevoidCam_AutoFocusCompl(objectsender,CameraOperationCompletedEventArgse) { } privatevoidCamView_Tap(objectsender,System.Windows.Input.GestureEventArgse) { if(this.cam!=null&&this.isInAutofocus==false) { if(this.cam.IsFocusAtPointSupported==true) { Pointpos=e.GetPosition(this.camView); this.cam.FocusAtPoint( pos.Y/this.camView.ActualHeight, 1.0-(pos.X/this.camView.ActualWidth)); } elseif(this.cam.IsFocusSupported==true) { this.cam.Focus(); } } } privatevoidOnCaptureImage(int[]img,intwidth,intheight) { Array.Copy(img,this.bit.Pixels,img.Length); varresult=this.barcodeReader.Decode(this.bit); if(result!=null) { Dispatcher.BeginInvoke(delegate { if(!Wp8StringManager.IsBTUQRTag(result.Text)) { MessageBoxes.ShowMainModelErrorMessageBox(AppResources.BTUTag_WrongTag); return; } if(this.resultAppStoreKey!=null) { if(NavigationService.CanGoBack) { App.SaveToIsolatedStorage(this.resultAppStoreKey,result.Text); NavigationService.GoBack(); } else { this.GoToCampusMappage(result.Text); } } else { if(AppSettings.BTUTagDefaultHandler.CampusMap==Settings.AppSetting.TagDefaultHandler) { this.GoToCampusMappage(result.Text); } else { MessageBoxResultmsgResult=MessageBoxes.ShowBtuTagInfoOkCancelMessageBox(result.Text); if(msgResult.Equals(MessageBoxResult.OK)) { stringurlString=Constants.PathBTUTag_Info; Uriurl=newUri(urlStringasstring,UriKind.Relative); this.NavigationService.Navigate(url); } } } }); } else { this.cam.Focus(); } } privatevoidGoToCampusMappage(stringtagContent) { stringpid=Wp8StringManager.FilterPlaceIdinQRResultString(tagContent); stringurlString=Constants.PathCampusmap_Campusmap; urlString+="?"+Constants.ParamModelMap_SearchTermAlias+"="+pid; Uriurl=newUri(urlStringasstring,UriKind.Relative); NavigationService.Navigate(url); } privatevoidCaptureThreadFunc() { Sizeresolution=this.cam.PreviewResolution; int[]buffer=newint[(int)(resolution.Height*resolution.Width)]; while(!this.captureThreadExit) { this.cam.GetPreviewBufferArgb32(buffer); this.OnCaptureImage(buffer,(int)resolution.Width,(int)resolution.Height); this.isInAutofocus=true; System.Threading.Thread.Sleep(1000); } } #endregion #endregion } }