限制条形码检测区域以使其绘制到屏幕上

时间:2019-05-06 12:20:32

标签: c# xamarin.android barcode-scanner

首先,我知道了一个问题:Vision API Barcode - Restrict the detection area to center rect of screen,但这并没有帮助我,所以我不得不问。

我有以下代码:

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            ScanValueText = FindViewById<TextView>(Resource.Id.ScanValueText);
            CameraView = FindViewById<SurfaceView>(Resource.Id.CameraView);
            ZoomBar = FindViewById<SeekBar>(Resource.Id.ZoomBar);
            CropView = FindViewById<TextView>(Resource.Id.CropView);

            ZoomBar.ProgressChanged += ZoomBar_ProgressChanged;
            CropView.Touch += CropView_Touch;

            barcodeDetector = new BarcodeDetector.Builder(this)
                .SetBarcodeFormats(BarcodeFormat.QrCode)
                .Build();
            cameraSource = new CameraSource
                .Builder(this, barcodeDetector)
                .SetRequestedPreviewSize(1920, 1080)
                .SetAutoFocusEnabled(true)
                .SetRequestedFps(15.0f)
                .Build();

            CameraView.Holder.AddCallback(this);
            barcodeDetector.SetProcessor(this);
        }

        #region Permissions
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            switch (requestCode)
            {
                case RequestCameraPermisionID:
                    {
                        if (grantResults[0] == Permission.Granted)
                        {
                            if (ActivityCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
                            {
                                //Request Permision  
                                ActivityCompat.RequestPermissions(this, new string[]
                                {
                    Manifest.Permission.Camera
                                }, RequestCameraPermisionID);
                                return;
                            }
                            try
                            {
                                cameraSource.Start(CameraView.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)
            {
                //Request Permision  
                ActivityCompat.RequestPermissions(this, new string[]
                {
                    Manifest.Permission.Camera
                }, RequestCameraPermisionID);
                return;
            }
            try
            {
                cameraSource.Start(CameraView.Holder);
            }
            catch (InvalidOperationException)
            {
            }
            Camera = CameraHelper.GetCamera(cameraSource);
            CameraParameters = Camera.GetParameters();

            ZoomBar.Max = CameraParameters.MaxZoom;
        }
        public void SurfaceDestroyed(ISurfaceHolder holder)
        {
            cameraSource.Stop();
        }
        public void Release()
        {

        }
        #endregion

        #region Detection
        public void ReceiveDetections(Detections detections)
        {
            SparseArray qrcodes = detections.DetectedItems;
            if (qrcodes.Size() != 0)
            {
                ScanValueText.Post(() => {
                    ScanValueText.Text = ((Barcode)qrcodes.ValueAt(0)).RawValue;
                });
            }
        }
        #endregion

        #region Eventhandler
        private void ZoomBar_ProgressChanged(object sender, SeekBar.ProgressChangedEventArgs e)
        {
            CameraParameters.Zoom = e.Progress;
            Camera.SetParameters(CameraParameters);
        }

        private void CropView_Touch(object sender, View.TouchEventArgs e)
        {
            List<Area> areaList = new List<Area>();
            MotionEvent me = (MotionEvent)e.Event;
            int x = (int)me.GetX();
            int y = (int)me.GetY();

            int width = CropView.LayoutParameters.Width;
            int height = CropView.LayoutParameters.Height;

            if ((x - width <= 20 && x - width > 0) || (width - x <= 20 && width - x > 0))
            {
                switch (me.Action)
                {
                    case MotionEventActions.Down:
                        break;
                    case MotionEventActions.Move:
                        CropView.LayoutParameters.Width = x;
                        CropView.LayoutParameters.Height = y;
                        CropView.RequestLayout();
                        break;
                    case MotionEventActions.Up:
                        break;
                }
            }
        }

此视图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <FrameLayout
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frameLayout1" >
        <SurfaceView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/CameraView" />
        <LinearLayout
            android:orientation="vertical"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/linearLayout1">
            <TextView
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/ScanValueText"
                android:gravity="center"
                android:layout_marginTop="10.0dp"
                android:layout_marginBottom="10.0dp"
                android:layout_marginLeft="10.0dp"
                android:layout_marginRight="10.0dp" />
            <FrameLayout
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/frameLayout2">
                <TextureView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/textureView1" />
                <TextView
                    android:text="TextView"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="400px"
                    android:layout_height="500px"
                    android:id="@+id/CropView"
                    android:background="@drawable/back"
                    android:layout_gravity="center"  />
                <SeekBar
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/ZoomBar"
                    android:layout_marginRight="5.0dp"
                    android:layout_marginBottom="5.0dp"
                    android:layout_marginLeft="5.0dp"
                    android:layout_gravity="bottom" />
            </FrameLayout>
        </LinearLayout>
    </FrameLayout>
</RelativeLayout>

我在TextView周围绘制了一个蓝色矩形,可以通过触摸事件调整其大小。

一切正常,但存在一些问题。 我希望条形码检测器仅扫描蓝色矩形中的区域。我相信必须将处理器设置为该区域,但是我不知道该怎么做以及需要哪个处理器。

0 个答案:

没有答案