我正在尝试创建自定义视图以使用Zxing扫描QR码
实际上,我可以在gradle的依赖项上实现此库,并且一切都可以与默认布局视图('com.journeyapps:zxing-android-embedded:3.6.0'
)完美配合
现在我想创建自己的视图,但是此类是只读文件
如何导入zxing并编辑核心代码?
我也检查了这个项目,并且意识到在这个项目中我可以编辑完整的代码:https://github.com/journeyapps/zxing-android-embedded
答案 0 :(得分:1)
使用此类:
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()
}
}
您将能够根据自己的要求编辑扫描布局和扫描活动类别。
希望这对您有所帮助。编码愉快。