我正在开发一个Android应用程序,使用zxing库来扫描QR码,如下所示:
首先将库集成到Gradle中:
implementation 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
implementation 'com.google.zxing:core:3.3.0'
其次是AndroidManifest.xml中的活动:
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />
然后在按下按钮时扫描QR码的代码:
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.setPrompt("Start scanning");
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setOrientationLocked(false);
integrator.initiateScan();
最后,解析从扫描仪获得的信息(永远不会执行)
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
} else {
String code = result.getContents();
textView.setText(code);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
相机已打开,看起来好像正在扫描,但是无法检测和读取QR码,没有返回任何内容。
答案 0 :(得分:1)
我看不出您的代码有什么问题,但我可以为您提供适合我的确切代码:
(我正在使用anyorientation,这意味着您必须创建一个新的Java类文件)
清单:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
<activity
android:name=".AnyOrientationCaptureActivity"
android:screenOrientation="fullSensor"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"></activity>
build.gradle:app
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
implementation 'com.android.support:appcompat-v7:25.3.1'
AnyOrientationCaptureActivity Java:
package com.your.package;
import com.journeyapps.barcodescanner.CaptureActivity;
public class AnyOrientationCaptureActivity extends CaptureActivity {
}
MainActivity:
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(AnyOrientationCaptureActivity.class);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);
integrator.setPrompt("Scan the QR code");
integrator.setOrientationLocked(false);
integrator.setBeepEnabled(false);
integrator.initiateScan();
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != CUSTOMIZED_REQUEST_CODE && requestCode != IntentIntegrator.REQUEST_CODE) {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
return;
}
switch (requestCode) {
case CUSTOMIZED_REQUEST_CODE: {
Toast.makeText(this, "REQUEST_CODE = " + requestCode, Toast.LENGTH_LONG).show();
break;
}
default:
break;
}
IntentResult result = IntentIntegrator.parseActivityResult(resultCode, data);
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
//Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
String mvalue = result.getContents();
}
}
答案 1 :(得分:0)
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 = true
private var mAutofocus: Boolean = true
private var resultText: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scaling_scanner)
mScannerView = ZXingScannerView(this)
content_frame.addView(mScannerView)
mScannerView?.setFlash(mFlash)
mScannerView?.setAutoFocus(mAutofocus)
switchFlash.setOnClickListener({
mFlash = !mFlash
mScannerView?.setFlash(mFlash)
})
switchAutoFocus.setOnClickListener ({
mAutofocus = mAutofocus
mScannerView?.setAutoFocus(mAutofocus)
})
}
override fun onResume() {
super.onResume()
mScannerView?.setResultHandler(this)
mScannerView?.setAspectTolerance(0.2f)
mScannerView?.setFlash(mFlash)
mScannerView?.setAutoFocus(mAutofocus)
mScannerView?.startCamera()
switchAutoFocus.setChecked(mAutofocus)
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()
}
}
使用此ScanActivity类。
然后在“活动”中使用:
scanView.setOnClickListener {
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
}
// here is your BRCode result //
val brCode:String=data.getStringExtra("BRCode")
}