Xamarin.Android:QR码扫描-提高阅读器质量

时间:2019-02-22 10:44:25

标签: xamarin.android camera qr-code barcode scanning

我尝试创建一个可以扫描二维码并处理其数据的简单应用。

我是通过以下教程完成的:https://www.c-sharpcorner.com/article/xamarin-android-qr-code-reader-by-mobile-camera/

该代码可以编译并正常运行-摄像机图像可以正确显示,但是现在我在几种不同的设备上测试了该代码,在某些设备上,他可以将qr代码保存到相机中并读取正确的数据,但不幸的是,在其他一些设备上却没有。不管二维码到相机的距离如何。

相机质量本身并不是我想的问题,相机图像的显示质量似乎不错,至少对于我来说,以及在同一设备上安装外部qr应用程序时(我自己)应用程序无法读取二维码,此外部应用程序可以读取二维码而没有任何问题。

这是什么原因?如何提高> install.packages("rgl") Installing package into ‘/home/xxxxxx/R/x86_64-redhat-linux-gnu-library/3.4’ (as ‘lib’ is unspecified) trying URL 'http://cran.rstudio.com/src/contrib/rgl_0.99.16.tar.gz' Content type 'application/x-gzip' length 3058794 bytes (2.9 MB) ================================================== downloaded 2.9 MB * installing *source* package ‘rgl’ ... ** package ‘rgl’ successfully unpacked and MD5 sums checked checking for gcc... gcc -m64 -std=gnu99 checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc -m64 -std=gnu99 accepts -g... yes checking for gcc -m64 -std=gnu99 option to accept ISO C89... none needed checking how to run the C preprocessor... gcc -m64 -std=gnu99 -E checking for gcc... (cached) gcc -m64 -std=gnu99 checking whether we are using the GNU C compiler... (cached) yes checking whether gcc -m64 -std=gnu99 accepts -g... (cached) yes checking for gcc -m64 -std=gnu99 option to accept ISO C89... (cached) none needed checking for libpng-config... yes configure: using libpng-config configure: using libpng dynamic linkage checking for X... libraries , headers checking GL/gl.h usability... no checking GL/gl.h presence... no checking for GL/gl.h... no checking GL/glu.h usability... no checking GL/glu.h presence... no checking for GL/glu.h... no configure: error: missing required header GL/gl.h 的质量,以便他能够在所有设备上读取二维码?

让我简要分享一下我的看法:
-三星Galaxy SIII Neo:自己的应用程序(下面的代码)可以工作,因此能够读取和处理二维码,也可以读取和处理外部二维码应用程序
-Acer One 10:相同的应用程序,相机图像显示良好(类似于Samsung设备),但是持有相同的qr码(上面已起作用)来扫描相机,则没有效果。无论其与设备的距离如何。但是,始终可以使用外部qr应用程序。
-三星Galaxy S4:与Acer One 10相同

到目前为止,请允许我最终分享我的代码,也许对我来说更容易:

QrCodeScanner.axml:

BarcodeDetector

QrCodeScannerActivity.cs:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <SurfaceView
        android:layout_width="480dp"
        android:layout_height="480dp"
        android:layout_centerInParent="true"
        android:id="@+id/surfaceView" />
    <TextView
        android:text="Please focus qr code
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/surfaceView"
        android:textSize="20sp"
        android:layout_marginTop="20dp"     
        android:gravity="center_horizontal"
        android:id="@+id/textView" />
</RelativeLayout>

AndroidManifest.xml:

//...
using Android.Gms.Vision;
using Android.Gms.Vision.Barcodes;
using Android.Support.V4.App;
using Android.Support.V7.App;
using static Android.Gms.Vision.Detector;
//...
[Activity(Label = "QrCodeScannerActivity")]
public class QrCodeScannerActivity : AppCompatActivity, ISurfaceHolderCallback, IProcessor
{
    SurfaceView cameraPreview;
    TextView resultTxt;
    BarcodeDetector barcodeDetector;
    CameraSource cameraSource;
    const int RequestCameraPermissionID = 1001;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.QrCodeScanner);
        cameraPreview = FindViewById<SurfaceView>(Resource.Id.surfaceView);
        resultTxt = FindViewById<TextView>(Resource.Id.textView);

        barcodeDetector = new BarcodeDetector
            .Builder(this)
            .SetBarcodeFormats(BarcodeFormat.QrCode)                
            .Build();
        cameraSource = new CameraSource
            .Builder(this, barcodeDetector)
            .SetAutoFocusEnabled(true)
            .SetFacing(CameraFacing.Front)
            .SetRequestedPreviewSize(480, 480)
            .SetRequestedFps(30.0f)                
            .Build();

        cameraPreview.Holder.AddCallback(this);
        barcodeDetector.SetProcessor(this);
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
    {
        switch (requestCode)
        {
            case RequestCameraPermissionID:
            {
                if (grantResults[0] == Permission.Granted)
                {
                    if (ActivityCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
                    {
                        ActivityCompat.RequestPermissions(this, new string[]
                        {
                            Manifest.Permission.Camera
                        }, RequestCameraPermissionID);
                        return;
                        }
                        try
                        {
                            cameraSource.Start(cameraPreview.Holder);
                        }
                        catch (InvalidOperationException)
                        {

                        }
                    }
                }
                break;
        }
    }
    public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
    {

    }
    public void SurfaceCreated(ISurfaceHolder holder)
    {
        if (ActivityCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
        {
            ActivityCompat.RequestPermissions(this, new string[]
            {
                Manifest.Permission.Camera
            }, RequestCameraPermissionID);
            return;
        }
        try
        {
            cameraSource.Start(cameraPreview.Holder);
        }
        catch (InvalidOperationException)
        {

        }
    }
    public void SurfaceDestroyed(ISurfaceHolder holder)
    {
        cameraSource.Stop();
    }
    public void ReceiveDetections(Detections detections)
    {            
        SparseArray qrcodes = detections.DetectedItems;
        if (qrcodes.Size() != 0)
        {
            resultTxt.Post(() =>
            {
                Vibrator vib = (Vibrator)GetSystemService(Context.VibratorService);
                vib.Vibrate(1000);
                resultTxt.Text = ((Barcode)qrcodes.ValueAt(0)).RawValue;
            });
        }
    }
    public void Release()
    {

    }
}

真的很感谢每一个答案。

最诚挚的问候

0 个答案:

没有答案