使用Kotlin在Android中扫描条形码?
此示例演示如何在Kotlin中实现条形码扫描。
步骤1 - 在Android Studio中创建一个新项目,转到文件?新建项目,然后填写所有必需的详细信息以创建一个新项目。
步骤2 - 将以下代码添加到res/layout/activity_main.xml中。
示例
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:padding="8dp" android:text="Tutorials Point" android:textColor="@android:color/holo_green_dark" android:textSize="48sp" android:textStyle="bold" /> <TextView android:id="@+id/txtContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button" android:layout_centerInParent="true" android:layout_marginBottom="10dp" android:text=" code reader" android:textSize="16sp" android:textStyle="bold" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginTop="50dp" android:text="Process" /> </RelativeLayout>
步骤3 - 向build.gradle(模块:app)中添加以下依赖项
implementation 'com.google.zxing:core:3.3.3' implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
步骤4 - 向src/MainActivity.kt中添加以下代码
示例
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.zxing.integration.android.IntentIntegrator
class MainActivity : AppCompatActivity() {
lateinit var btnBarcode: Button
lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
btnBarcode = findViewById(R.id.button)
textView = findViewById(R.id.txtContent)
btnBarcode.setOnClickListener {
val intentIntegrator = IntentIntegrator(this@MainActivity)
intentIntegrator.setBeepEnabled(false)
intentIntegrator.setCameraId(0)
intentIntegrator.setPrompt("SCAN")
intentIntegrator.setBarcodeImageEnabled(false)
intentIntegrator.initiateScan()
}
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
if (result != null) {
if (result.contents == null) {
Toast.makeText(this, "cancelled", Toast.LENGTH_SHORT).show()
} else {
Log.d("MainActivity", "Scanned")
Toast.makeText(this, "Scanned -> " + result.contents, Toast.LENGTH_SHORT)
.show()
textView.text = String.format("Scanned Result: %s", result)
}
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
}步骤5 - 向androidManifest.xml中添加以下代码
示例
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.q12"> <uses-feature android:name="android.hardware.camera.autoFocus" /> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <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/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行您的应用程序。我假设您已经将实际的Android移动设备与计算机连接。 要从Android Studio运行该应用程序,请打开项目的某个活动文件,然后单击工具栏中的运行图标
。选择移动设备作为选项,然后检查将显示默认屏幕的移动设备

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP