在ZXing中更改扫描区域布局

时间:2019-06-25 15:48:37

标签: android zxing

我想用我的自定义宽度,高度和自定义边框颜色将ZXing中的扫描区域替换为我的自定义视图(下一张照片带有绿色边框的视图)到我自己的矩形中。这该怎么做?目前,我正在使用com.journeyapps:zxing-android-embedded:3.6.0

class BarcodeFragment : Fragment() {

    private var barcodeView: CompoundBarcodeView? = null

    private val callback = object : BarcodeCallback {
        override fun barcodeResult(result: BarcodeResult) {
            if (result.text != null) {

            }
        }

        override fun possibleResultPoints(resultPoints: List<ResultPoint>) {}
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.scanner_fragment, container, false)
        barcodeView = view.findViewById(R.id.barcode_scanner)
        barcodeView!!.decodeContinuous(callback)

        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        barcodeView?.statusView?.visibility = View.GONE
    }

    override fun onResume() {
        barcodeView?.resume()
        super.onResume()
    }

    override fun onPause() {
        barcodeView?.pause()
        super.onPause()
    }
}

我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <com.journeyapps.barcodescanner.CompoundBarcodeView
        android:id="@+id/barcode_scanner"
        android:layout_width ="match_parent"
        android:layout_height="match_parent">
    </com.journeyapps.barcodescanner.CompoundBarcodeView>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/barcode_input_background"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:padding="@dimen/margin_general_16dp"
        android:layout_marginStart="@dimen/margin_medium_32dp"
        android:layout_marginEnd="@dimen/margin_medium_32dp"
        android:layout_marginTop="@dimen/margin_general_16dp"
        android:textColor="@color/smokyblack"
        android:textColorHint="@color/manatee"
        />
</android.support.constraint.ConstraintLayout>

enter image description here

2 个答案:

答案 0 :(得分:0)

可能这不是您的解决方案,但过去我有相同的issue和类似的库。也许您可以找到有用的东西。

答案 1 :(得分:0)

您必须编辑this class

我对我的一个应用做了类似的修改,确实添加了一个调用,以在我称为onDraw()的{​​{1}}方法中绘制边缘 这是四个边缘。 drawFocusOverlay(frame, canvas);确定颜色和厚度。

focusPaint

至于矩形,我相信它是protected void drawFocusOverlay(Rect frame, Canvas canvas) { int qrOverlayLength = 100; // Top left canvas.drawLine(frame.left, frame.top, frame.left + qrOverlayLength, frame.top, focusPaint); canvas.drawLine(frame.left, frame.top, frame.left, frame.top + qrOverlayLength, focusPaint); // Bottom Left canvas.drawLine(frame.left, frame.bottom, frame.left + qrOverlayLength, frame.bottom, focusPaint); canvas.drawLine(frame.left, frame.bottom, frame.left, frame.bottom - qrOverlayLength, focusPaint); // Top right canvas.drawLine(frame.right, frame.top, frame.right - qrOverlayLength, frame.top, focusPaint); canvas.drawLine(frame.right, frame.top, frame.right, frame.top + qrOverlayLength, focusPaint); // Bottom right canvas.drawLine(frame.right, frame.bottom, frame.right - qrOverlayLength, frame.bottom, focusPaint); canvas.drawLine(frame.right, frame.bottom, frame.right, frame.bottom - qrOverlayLength, focusPaint); } 变量,您需要更改it's size is calculated here.的方式

要使其与您的项目一起使用,我建议您克隆存储库,进行修改,并将其作为Android Library包含在您自己的项目中。