Xamarin.安卓:ZXing扫描仪从一个小区域(不是全屏)



我想在一个小区域显示扫描仪,而不是像这里的帖子那样全屏显示(但Xamarin.Android和MvvvmCross在片段中)。

为此,我有我的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto">
<FrameLayout
    android:id="@+id/barcodeview"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_weight="0">
    <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#d13033"
        android:layout_gravity="center_vertical" />
</FrameLayout>
<Mvx.MvxListView
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    local:MvxItemTemplate="@layout/item_product"
    local:MvxBind="ItemsSource Products" />

像https://i.stack.imgur.com/nouxm.jpg

我的片段视图:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    base.OnCreateView(inflater, container, savedInstanceState);
    var view = this.BindingInflate(Resource.Layout.Products, null);
    var activity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity;
    MobileBarcodeScanner.Initialize(activity.Application);
    var barcodeview = view.FindViewById<FrameLayout>(Resource.Id.barcodeview);
    var scanner = new ZXingScannerFragment();
    scanner.UseCustomOverlayView = true;
    scanner.CustomOverlayView = barcodeview;
    scanner.StartScanning(result =>
    {
        Mvx.Trace("Scanned with ZXingScannerFragment : " + result.Text);
    });
    //_mobileBarcodeScanner = new MobileBarcodeScanner();
    //_mobileBarcodeScanner.UseCustomOverlay = true;
    //_mobileBarcodeScanner.CustomOverlay = barcodeview;
    //_mobileBarcodeScanner.Torch(true);
    //_mobileBarcodeScanner.ScanContinuously(result =>
    //{
    //    Mvx.Trace("Scanned :" + result.Text);
    //});
    return view;
}

}

我尝试了MobileBackodeScanner和ZXingScannerFragment类:

使用MobileBackodeScanner,扫描仪可以全屏启动并工作。

使用ZXingScannerFragment时,扫描仪不会启动。即使我使用FragmentManager.Replace(barcodeview, new ZXingScannerFragment()).Commit())

我做了一些类似的事情,但使用了不同的方法。我将ZXing直接集成到我的应用程序中。然后我使用了在这里找到的类

https://github.com/zxing/zxing/blob/master/android/src/com/google/zxing/client/android/CaptureActivity.java

以模仿我的班级。要修改的重要代码是onResume和onPause方法,类似

SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
if (hasSurface) {
    // The activity was paused but not stopped, so the surface still exists. Therefore
    // surfaceCreated() won't be called, so init the camera here.
    initCamera(surfaceHolder);
 } else {
        // Install the callback and wait for surfaceCreated() to init the camera.
     surfaceHolder.addCallback(this);
}

其中R.id.preview_view可以替换为要使用的小区域视图。

相关内容

最新更新