如何修复此 QR 码扫描仪的代码?
Posted
技术标签:
【中文标题】如何修复此 QR 码扫描仪的代码?【英文标题】:How to fix the code this QR Code Scanner? 【发布时间】:2021-09-13 05:47:04 【问题描述】:我差点在 android Studio 中做了一个用 WebView 扫描二维码的应用,但还没有完成。我的应用程序扫描代码后,该应用程序只是关闭而没有结果。你们能从我的代码中找到问题吗?
这是应用程序的完整代码。
MainActivity.java
package com.example.qr_code_scanner;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.javascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
WebView webView;
Button scanBtn;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanBtn = findViewById(R.id.scanBtn);
scanBtn.setOnClickListener(this);
@JavascriptInterface
public void onClick(View v)
scanCode();
private void scanCode()
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(CaptureActivity.class);
// integrator.setOrientationLocked(false);
// integrator.setRequestCode(200)
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scanning Code");
integrator.initiateScan();
webView.addJavascriptInterface(this, "Android");
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null)
if (result.getContents() != null)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(result.getContents());
builder.setTitle("Scanning Result");
builder.setPositiveButton("Scan Again", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
scanCode();
).setNegativeButton("Finish", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
finish();
);
AlertDialog dialog = builder.create();
dialog.show();
else
Toast.makeText(this, "No Results", Toast.LENGTH_LONG).show();
else
webView.loadUrl(result.getContents());
CaptureActivity.java
package com.example.qr_code_scanner;
public class CaptureActivity extends com.journeyapps.barcodescanner.CaptureActivity
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.qr_code_scanner">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.QR_Code_Scanner">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CaptureActivity"
android:screenOrientation="fullSensor"
android:stateNotNeeded="true"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
</application>
</manifest>
build.gradle (:app)
plugins
id 'com.android.application'
android
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig
applicationId "com.example.qr_code_scanner"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
compileOptions
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
dependencies
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.journeyapps:zxing-android-embedded:4.1.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<Button
android:id="@+id/scanBtn"
android:layout_
android:layout_
android:text="scan"
android:padding="4dp"
android:textSize="20dp"/>
</LinearLayout>
<WebView
android:id="@+id/webview"
android:layout_
android:layout_/>
</LinearLayout>
【问题讨论】:
如果您遇到错误,请发布 logcat。仅通过查看代码来找出错误是非常耗时的。 【参考方案1】:您没有定义您直接调用的 Webview id
webView.addJavascriptInterface(this, "Android");
你错过了这一行
webView = findViewById(R.id.webview);
您正在使用 zxing 的另一件事,您需要在清单中添加一些行,遵循 Zxing Documentation
【讨论】:
以上是关于如何修复此 QR 码扫描仪的代码?的主要内容,如果未能解决你的问题,请参考以下文章
编码使用 wifi direct 的文件共享应用程序时是不是需要扫描 QR 码
使用 Visual Studio 在 phonegap 应用程序中实现 QR 码扫描仪?