Xamarin.Android:来自小区域的ZXing扫描仪(不是全屏)

时间:2016-08-01 16:48:03

标签: android xamarin barcode zxing

我想在一个小区域内显示扫描仪,而不是全屏显示,如post here(但片段中的Xamarin.Android和MvvmCross)。

为此,我有我的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" />

http://imgur.com/a/t1sgq

类似

我的片段视图:

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;
}

}

我尝试使用MobileBarcodeScanner和ZXingScannerFragment类:

使用MobileBarcodeScanner,扫描仪可以全屏启动并运行。

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

1 个答案:

答案 0 :(得分:0)

我做了一些非常类似的事情,但使用了不同的方法。我将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可以替换为您要使用的小区域视图。