2015-07-16

[Xamarin] Scan QRCode 使用 ZXing.Net.Mobile

專案需求,最近需要掃描QR Code 所以簡單研究一下,結果超簡單,哈哈,筆記一下給需要的人,跟注意的事項

主要我們是透過 ZXing.Net.Mobile

Image 023
1.首先我們得知道我們要用的東西  ZXing.Net.Mobile 所以我們在專案中的

Components 按下滑鼠右鍵,選擇Get more components

Image 024

之後搜尋 ZXing 就會找到 ZXing.Net.Mobile 

Image 025

之後就把它安裝吧,安裝完之後專案會多這些東西

Image 027

2. 專案我們設定一顆按鈕讓他點下去之後就可以進行掃描 QRCode,並且結果我們用Toast 顯示出來

在這之前,千萬別忘記,你必須要在Properties 中 的  AndroidManifest.xml 加入相機的Permissions



  <uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

3. 按鈕按下後的程式碼



        private async void BtnScan_Click(object sender, EventArgs e)
{
//啟動掃描器
var scanner = new ZXing.Mobile.MobileBarcodeScanner(this);
var result = await scanner.Scan();
//如果有結果再顯示
if (result != null)
{
Toast.MakeText(this, result.Text, ToastLength.Long).Show();

}

}

4.結果

Screenshot_2015-07-16-15-22-48
Screenshot_2015-07-16-15-22-42

範例下載: