如何使用xamarin android扫描QR码

时间:2016-07-22 16:05:05

标签: c# xamarin.android qr-code

我想创建一个活动来扫描QR码,活动扩展到IOnclickListenr:

    [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做到这一点,任何建议都将非常感谢,谢谢。

1 个答案:

答案 0 :(得分: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();