我正在尝试使用Zxing创建一个用于扫描QR码的自定义视图。
实际上,我可以在gradle依赖项中使用该库,并且默认布局视图('com.journeyapps:zxing-android-embedded:3.6.0')一切都很完美。
现在我想创建自己的视图,但这些类是只读文件。
我该如何导入zxing并编辑核心代码?
我还检查了这个项目,并意识到在这个项目中,我能够编辑全部代码:https://github.com/journeyapps/zxing-android-embedded
我正在尝试使用Zxing创建一个用于扫描QR码的自定义视图。
实际上,我可以在gradle依赖项中使用该库,并且默认布局视图('com.journeyapps:zxing-android-embedded:3.6.0')一切都很完美。
现在我想创建自己的视图,但这些类是只读文件。
我该如何导入zxing并编辑核心代码?
我还检查了这个项目,并意识到在这个项目中,我能够编辑全部代码:https://github.com/journeyapps/zxing-android-embedded
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()
}
}
这个layoutView是:
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>
在你的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()
}
}
您可以编辑扫描布局和扫描活动类,以符合您的自定义要求。
希望这可以帮助到您。祝编码愉快。