Xamarin Zxing条形码扫描器叠加层

3

我正在尝试使用Visual Studio和Xamarin为zxing条形码扫描器创建一个覆盖层,但我不知道如何实际实现。

我为此创建了一个小型布局,名为overlay.axml,我希望它可以在相机视图的顶部绘制出来。

Overlay.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <Space
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/space1"
        android:layout_weight="1" />
    <ToggleButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tbtnTorch"
        android:layout_weight="0"
        android:textOn="Torch On"
        android:textOff="Torch Off" />
</LinearLayout>

此外,我认为我需要创建一个从View继承的类来将布局与其绑定,但我对此不确定,这是我到目前为止得到的内容。
Overlay.cs:
public Overlay(Context context) : base(context)
{

}
public Overlay(Context context, IAttributeSet attrs) : base(context, attrs)
{

}

public Overlay(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{

}

protected override void OnDraw(Android.Graphics.Canvas canvas)
{
    base.OnDraw(canvas);
}

我现在希望将遮罩层绑定到我的ZXing扫描器实例上,但是我不确定该如何做。这是我目前的代码。

MainActivity.cs:
scanner = new MobileBarcodeScanner(this);
options = new MobileBarcodeScannerOptions();
scanner.UseCustomOverlay = true;
scanner.CustomOverlay = new Overlay(this);

[...]

var result = await scanner.scan(options);

如果不使用叠加层,则可以像预期的那样正常工作,但是在使用叠加层时,当我开始扫描时,只会得到黑屏。编辑:在模拟器上出现黑屏,在真实设备上没有任何叠加层(如预期的那样)。


我假设您有一个通过所有这些麻烦的理由,但以防万一您没有考虑过,您是否考虑过只是使用https://components.xamarin.com/view/zxing.net.mobile,其中包括“入门”文档和所有内容。 - ClintL
1个回答

1
你不需要创建另一个 Overlay 类,只需按照以下方式填充覆盖层:
var zxingOverlay = LayoutInflater.FromContext(<YOUTCONTEXT>).Inflate(Resource.Layout.overlay, null);

将此内容分配给您的扫描器。
scanner.CustomOverlay = zxingOverlay;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接