2016-07-22 76 views
0

我想創建一個活動掃描QR碼和活動來延長IOnclickListenr:我怎麼可以掃描QR碼xamarin安卓

[Activity(Label = "ScanActivity")] 
    public class ScanActivity : Activity ,IOnClickListener 

我定義的OnCreate如下,我有一個掃描按鈕,你可以見:

  protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 
      SetContentView(Resource.Layout.activity_scan); 
      Button scanBtn = FindViewById<Button>(Resource.Id.button_scan); 
      scanBtn.SetOnClickListener(this);        
     } 

然後我要實現這個功能:

MobileBarcodeScanner scanner; 
     public void OnClick(View v) 
     { 
      //respond to clicks 
      if (v.Id == Resource.Id.button_scan) 
      { 
       //scan 
       //IntentIntegrator scanIntegrator = new IntentIntegrator(this); 
       //scanIntegrator.initiateScan(); 


      } 
     } 
     public void onActivityResult(int requestCode, Android.App.Result resultCode, Intent intent) 
     { 
    //  if (scanningResult != null) { 
// String scanContent = scanningResult.getContents(); 

// } 
//else{ 
//Toast toast = Toast.makeText(getApplicationContext(), 
// "No scan data received!", Toast.LENGTH_SHORT); 
// toast.show(); 
//} 

     } 

我不知道我怎麼能用c#和Xamarin.Android做到這一點,任何建議都將非常感激,謝謝。

回答

0

我推薦使用ZXing.Net.Mobile的包。我用它來進行條碼和二維碼掃描。工作得很好!

QRCodeWriter writer = new QRCodeWriter(); 
ZXing.Common.BitMatrix matrix; 

int size = 120; matrix = writer.encode("http://aumansoftware.com;", BarcodeFormat.QR_CODE, size, size, null); 

SizeF qrcCodeSize = new SizeF(size,size); 

UIGraphics.BeginImageContext(qrcCodeSize); 

using (CGContext cont = UIGraphics.GetCurrentContext()) { cont.SetLineWidth(1); cont.SetFillColor(UIColor.White.CGColor); cont.AddRect(new RectangleF(0,0,qrcCodeSize.Width,qrcCodeSize.Height)); cont.DrawPath(CGPathDrawingMode.Fill); 

        cont.SetFillColor(UIColor.Black.CGColor); 


        for (int y = 0; y < matrix.Height; y++) 
        { 
         for (int x = 0; x < matrix.Width; x++) 
         { 

          if(matrix[x,y]) cont.AddRect(new RectangleF(x,y,1,1)); 


         } 

        } 


        cont.DrawPath(CGPathDrawingMode.Fill); 

        UIImage qrcImage = UIGraphics.GetImageFromCurrentImageContext(); 
// show image in imageview UIImageView qrcImageView defined earlier... qrcImageView.Image = qrcImage; qrcImageView.SetNeedsDisplay(); 

}//end using cont 

UIGraphics.EndImageContext();