Java – How do I create a custom View using Zxing?

How do I create a custom View using Zxing?… here is a solution to the problem.

How do I create a custom View using Zxing?

I’m trying to create a custom View to scan a QR code using Zxing

Actually, I can implement this library on my Gradle’s dependencies and everything works perfectly with the default layout View (‘com.journeyapps:zxing-android-embedded:3.6.0').

Now I want to create my own View, but this class is a read-only file

How do I import zxing and edit the core code?

I

also checked the project and I realized that in this project I could edit the complete code: https://github.com/journeyapps/zxing-android-embedded

Solution

Use this class:

package com.company.project.view.activities

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.google.zxing.Result
import kotlinx.android.synthetic.main.activity_scaling_scanner.*
import me.dm7.barcodescanner.zxing.ZXingScannerView
import com.company.project.R

class ScanActivity : AppCompatActivity(), ZXingScannerView.ResultHandler {

private val FLASH_STATE = "FLASH_STATE"
    private val AUTOFOCUS_STATE = "AUTOFOCUS_STATE"

private var mScannerView: ZXingScannerView? = null
    private var mFlash: Boolean = false
    private var mAutofocus: Boolean = true
    private var resultText: String = ""

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scanner)

mScannerView = ZXingScannerView(this)

content_frame.addView(mScannerView)

mScannerView?. setFlash(mFlash)
        mScannerView?. setAutoFocus(true)

switchFlash.setOnClickListener({
            mFlash = !mFlash
            mScannerView?. setFlash(mFlash)
        })

}

override fun onResume() {
        super.onResume()
        mScannerView?. setResultHandler(this)
        mScannerView?. setAspectTolerance(0.2f)
        mScannerView?. setFlash(mFlash)
        mScannerView?. setAutoFocus(mAutofocus)
        mScannerView?. startCamera()

switchFlash.setChecked(mFlash)
    }

override fun onPause() {
        super.onPause()
        mScannerView?. stopCamera()
    }

override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?. putBoolean(FLASH_STATE, mFlash)
        outState?. putBoolean(AUTOFOCUS_STATE, mAutofocus)
    }

override fun handleResult(result: Result?) {
        mScannerView?. resumeCameraPreview(this)

if (result == null) {
            return
        }

mScannerView?. stopCamera()

resultText = result.text;
        val resultIntent: Intent= Intent().putExtra("BRCode", resultText)
        setResult(1, resultIntent)
        finish()
    }
}

This layout View :

activity_scanner.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp" />
        <LinearLayout
            android:orientation="horizontal"
            android:gravity="center"
            android:background="#222"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

<Switch
                    android:id="@+id/switchFlash"
                    android:layout_width="0dp"
                    android:layout_height="65dp"
                    android:layout_weight="1"
                    android:text="@string/flashValue"
                    android:textColor="?attr/colorBackgroundFloating"
                    android:paddingLeft="300dp" android:layout_gravity="center"/>

</LinearLayout>
    </LinearLayout>

</LinearLayout>

Then call this from your MainActivity

    scanView.setOnClickListener {
        scanQRCode()
    }

fun scanQRCode(){
    val intent = Intent(this, ScanActivity::class.java)
    startActivityForResult(intent, 2)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (data == null) {
        showIncorrectQRCodeDialogue()
        return
    }

if (data.getStringExtra("BRCode")!=null) {

val brCode: String = data.getStringExtra("BRCode")

val intent = Intent(this, NewActivity::class.java);

startActivity(intent);

}else {
        showIncorrectQRCodeDialogue()
    }
}

You will be able to edit the scan layout and scan activity categories according to your custom requirements.

Hope this helps. Happy coding.

Related Problems and Solutions