Change scan area layout in ZXing

Viewed 1505

I want to replace scan area in ZXing to my custom view (the one with green borders on a photo below) to my own rectangle with my custom width, height and custom border color. How to do this? Currently I'm using 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()
    }
}

My layout code:

<?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 Answers

Probably it is not a solution for you, but I had the same issue in the past with similar library. Maybe you can find something useful.

You'll have to edit this class.

I did a similar modification for one of my app, did add a call to draw the edges in the onDraw() method I called drawFocusOverlay(frame, canvas); This is for the 4 edges. focusPaint determining the color and thickness.

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

As for the rectangle shape I believe it's the protected Rect framingRect; variable, you'll need to change how it's size is calculated here.

To make it work with your project, I advice you clone the repo, make the modifications and include it as an Android Library to your own project.

Related