目錄
📂 前言
AR 眼鏡系統版本
條形碼識別
1. 🔱 技術方案
1.1 技術方案概述
1.2 實現方案
1)相機App顯示模塊
2)算法so庫JNI模塊
3)算法條形碼識別模塊
2. 💠 實現相機App顯示模塊
2.1 創建 BarcodeIdentifyDemoActivity.kt
2.2 創建 activity_barcode_identify_demo.xml
2.3 創建 AlgorithmLibHelper.kt
2.4 創建 AssetsHelper.kt
2.5 創建 ProductBean.kt
3. ?? 算法so庫JNI模塊
3.1 新建CMakeLists.txt,引入算法so庫以及頭文件
1)CMakeLists.txt 內容如下:
2)引入算法so庫以及頭文件
3.2 新建cpp文件,調用算法so庫方法
3.3 新建native方法,加載JNI模塊生成的庫以及映射對應方法
3.4 配置 build.gradle 文件
4. ? 小結
📂 前言
AR 眼鏡系統版本
????????W517 Android9。
條形碼識別
????????AR眼鏡中相機App,調用算法條形碼識別接口,獲取到算法接口返回文本后,將文本顯示在眼鏡頁面。
1. 🔱 技術方案
1.1 技術方案概述
????????條形碼識別功能的實現,主要包括以下三大模塊:相機App顯示模塊、算法so庫JNI模塊、以及算法條形碼識別模塊。
1.2 實現方案
1)相機App顯示模塊
-
實現相機預覽、拍照與保存功能;
-
傳入拍照后的圖片路徑,調用算法so庫JNI模塊;
-
顯示算法so庫JNI模塊返回的條形碼識別到的商品信息。
2)算法so庫JNI模塊
-
新建CMakeLists.txt,引入算法so庫以及頭文件;
-
新建cpp文件,調用算法so庫方法;
-
新建native方法,加載JNI模塊生成的庫以及映射對應方法。
3)算法條形碼識別模塊
-
對照片進行處理,獲取到照片中的二維碼;
-
調用二維碼識別so庫,獲取二維碼中的信息;
-
將二維碼信息與數據庫匹配,返回匹配到的商品信息。
2. 💠 實現相機App顯示模塊
2.1 創建 BarcodeIdentifyDemoActivity.kt
????????主要實現相機預覽、拍照與保存等功能。可參考《一周內從0到1開發一款 AR眼鏡 相機應用?》
class BarcodeIdentifyDemoActivity :BaseActivity<ActivityBarcodeIdentifyDemoBinding, MainViewModel>() {private val TAG = BarcodeIdentifyDemoActivity::class.java.simpleNameprivate val DEFAULT_CONTENT =" \"商品名稱\": \"\",\n" + " \"品牌\": \"\",\n" + " \"規格型號\": \"\",\n" + " \"價格\": \"\",\n" + " \"原產地\": \"\",\n" + " \"稅率\": \"\",\n" + " \"稅號\": \"\",\n" + " \"功能用途\":\"\""private val CAMERA_MAX_RESOLUTION = Size(3264, 2448)private var mCameraExecutor: ExecutorService = Executors.newSingleThreadExecutor()private var mImageCapture: ImageCapture? = nullprivate var mIsBarcodeRecognition: Boolean = falseprivate var mTimeOut: CountDownTimer? = nulloverride fun initBinding(inflater: LayoutInflater): ActivityBarcodeIdentifyDemoBinding =ActivityBarcodeIdentifyDemoBinding.inflate(inflater)override fun initData() {AlgorithmLibHelper.init(this)AlgorithmLibHelper.productBeanLiveData.observe(this) {Log.i(TAG, "initData: $it")if (it != null) {runOnUiThread {binding.loading.visibility = GONEmIsBarcodeRecognition = falsebinding.content.text =" \"商品名稱\": \"${it.product_name}\",\n" + " \"品牌\": \"${it.brand}\",\n" + " \"規格型號\": \"${it.specifications}\",\n" + " \"價格\": \"${it.price}\",\n" + " \"原產地\": \"${it.country_of_origin}\",\n" + " \"稅率\": \"${it.tax_rate}\",\n" + " \"稅號\": \"${it.tax_code}\",\n" + " \"功能用途\":\"${it.function_and_use}\""mTimeOut?.cancel()mTimeOut = null}} else {errorHandle()}}}override fun initViewModel() {viewModel.init(this)}override fun initView() {initWindow()switchToPhoto()binding.parent.setOnClickListener {try {if (mImageCapture == null || mIsBarcodeRecognition) {AGGToast(this, Toast.LENGTH_SHORT, "please hold on").show()} else {mIsBarcodeRecognition = truebinding.loading.setContent("Identify...")binding.loading.visibility = VISIBLEtakePicture()}} catch (e: Exception) {e.printStackTrace()}}binding.loading.visibility = VISIBLE}override fun onResume() {Log.i(TAG, "onResume: ")super.onResume()}override fun onStop() {Log.i(TAG, "onStop: ")super.onStop()}override fun onDestroy() {Log.i(TAG, "onDestroy: ")mTimeOut?.cancel()mTimeOut = nullAnimatorSwitchHelper.isAnimating = falseAnimatorSwitchHelper.isFirstSwitch = truesuper.onDestroy()mCameraExecutor.shutdown()XrEnvironment.getInstance().imuReset()AlgorithmLibHelper.release()}private fun initWindow() {Log.i(TAG, "initWindow: ")val lp = window.attributeslp.dofIndex = 0lp.subType = WindowManager.LayoutParams.WINDOW_IMMERSIVE_0DOFwindow.attributes = lp}private fun switchToPhoto() {Log.i(TAG, "switchToPhoto: ")val cameraProviderFuture = ProcessCameraProvider.getInstance(this)cameraProviderFuture.addListener({try {mImageCapture = ImageCapture.Builder().setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY).setTargetResolution(CAMERA_MAX_RESOLUTION).build()val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERAval cameraProvider = cameraProviderFuture.get()cameraProvider.unbindAll()// 可預覽val preview = Preview.Builder().build()binding.previewView.apply {implementationMode = PreviewView.ImplementationMode.COMPATIBLEpreview.setSurfaceProvider(surfaceProvider)clipToOutline = truevisibility = VISIBLE}cameraProvider.bindToLifecycle(this, cameraSelector, preview, mImageCapture)// 無預覽
// cameraProvider.bindToLifecycle(this, cameraSelector, mImageCapture)binding.loading.visibility = GONE} catch (e: java.lang.Exception) {Log.e(TAG, "bindCamera Failed!: $e")}}, ContextCompat.getMainExecutor(this))}/*** 拍照*/private fun takePicture() {Log.i(TAG, "takePicture: ")SoundPoolTools.playCameraPhoto(this)val photoFile = viewModel.createPhotoFile()mImageCapture?.takePicture(ImageCapture.OutputFileOptions.Builder(photoFile).build(),mCameraExecutor,object : ImageCapture.OnImageSavedCallback {override fun onError(exc: ImageCaptureException) {Log.e(TAG, "Photo capture failed: ${exc.message}", exc)}@SuppressLint("SetTextI18n")override fun onImageSaved(output: ImageCapture.OutputFileResults) {val savedUri = output.savedUri ?: Uri.fromFile(photoFile)Log.i(TAG, "Photo capture succeeded: ${savedUri.path}")runOnUiThread { updateFlashPreview(savedUri) }viewModel.updateMediaFile(this@BarcodeIdentifyDemoActivity, photoFile)// 調用條形碼識別算法lifecycleScope.launch {withContext(Dispatchers.IO) {savedUri.path?.let {AlgorithmLibHelper.identifyBarcode(it)}}}// 超時邏輯runOnUiThread {mTimeOut?.cancel()mTimeOut = nullmTimeOut = object : CountDownTimer(15000L, 1000) {override fun onTick(millisUntilFinished: Long) {}override fun onFinish() {Log.e(TAG, "onFinish: identify timeout")AGGToast(this@BarcodeIdentifyDemoActivity,Toast.LENGTH_SHORT,"identify timeout").show()errorHandle()}}.start()}}})}private fun updateFlashPreview(savedUri: Uri) {binding.flashPreview.apply {visibility = VISIBLEGlide.with(this@BarcodeIdentifyDemoActivity).load(savedUri).into(this)// 創建動畫val animatorAlpha = ObjectAnimator.ofFloat(this, "alpha", 0f, 1f)val animatorX = ObjectAnimator.ofFloat(this, "translationX", 0f, SizeUtils.dp2px(-144f).toFloat())// 同時播放X和Y軸的動畫val animatorSet = AnimatorSet()animatorSet.playTogether(animatorAlpha, animatorX)animatorSet.interpolator = EaseOutInterpolator()animatorSet.duration = CAMERA_FLASH_PREVIEW_ANIM_TIMEanimatorSet.start()// 設置動畫監聽器,在動畫結束后等待2秒,然后隱藏圖片animatorSet.addListener(object : AnimatorListenerAdapter() {override fun onAnimationEnd(animation: Animator) {super.onAnimationEnd(animation)// 2秒后隱藏圖片postDelayed({visibility = GONE}, CAMERA_FLASH_PREVIEW_SHOW_TIME)}})}}private fun errorHandle() {runOnUiThread {binding.content.text = DEFAULT_CONTENTbinding.loading.visibility = GONEmIsBarcodeRecognition = falsemTimeOut?.cancel()mTimeOut = null}}}
2.2 創建 activity_barcode_identify_demo.xml
????????包括顯示算法so庫JNI模塊返回的條形碼識別到的商品信息。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/parent"android:layout_width="match_parent"android:layout_height="match_parent"android:keepScreenOn="true"><com.agg.ui.AGGActionBarandroid:id="@+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"app:UIActionBarTitle="Barcode Identify"app:UITitleLevel="M"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><com.agg.ui.AGGTextViewandroid:id="@+id/content"style="@style/TextBody5"android:layout_width="match_parent"android:layout_height="0dp"android:layout_marginHorizontal="32dp"android:layout_marginTop="16dp"android:layout_marginBottom="64dp"android:gravity="center_vertical|start"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@id/title" /><androidx.camera.view.PreviewViewandroid:id="@+id/previewView"android:layout_width="138dp"android:layout_height="104dp"android:layout_marginEnd="24dp"android:layout_marginBottom="24dp"android:background="@drawable/shape_corner_20dp_stroke_4dp_ffffff"android:foreground="@drawable/shape_corner_20dp_stroke_4dp_ffffff"android:visibility="gone"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent" /><com.agg.ui.AGGCircleImageViewandroid:id="@+id/flashPreview"android:layout_width="138dp"android:layout_height="104dp"android:layout_marginEnd="24dp"android:layout_marginBottom="24dp"android:contentDescription="@null"android:visibility="gone"app:borderColor="#FFFFC810"app:borderWidth="4dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:radius="20dp" /><com.agg.ui.AGGIdentifyandroid:id="@+id/loading"android:layout_width="wrap_content"android:layout_height="wrap_content"app:UIContent="Open Camera..."app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
2.3 創建 AlgorithmLibHelper.kt
????????訪問條形碼算法的幫助類,進行封裝隔離,通過傳入拍照后的圖片路徑,調用算法so庫JNI模塊。
object AlgorithmLibHelper {val productBeanLiveData = MutableLiveData<ProductBean>()private val TAG = AlgorithmLibHelper::class.java.simpleNameprivate var algorithmLib: AlgorithmLib? = nullfun init(context: Context) {Log.i(TAG, "init: ")val jsonFilePath = getJsonFilePath(context, "barcode_information_final.json")Log.i(TAG, "init: jsonFilePath=$jsonFilePath")algorithmLib = AlgorithmLib()algorithmLib?.initMatch(jsonFilePath)}fun identifyBarcode(imagePath: String) = runBlocking {Log.i(TAG, "identifyBarcode: imagePath=$imagePath")val identifyBarContent = algorithmLib?.matchBarcode(imagePath) ?: ""Log.i(TAG, "identifyBarcode: identifyBarContent=$identifyBarContent")if (identifyBarContent.isNotEmpty()) {try {val productInfo = GsonUtils.fromJson(identifyBarContent, ProductInfo::class.java)productBeanLiveData.postValue(productInfo.product_info)} catch (e: Exception) {e.printStackTrace()productBeanLiveData.postValue(ProductBean())}} else {productBeanLiveData.postValue(ProductBean())}}fun release() {Log.i(TAG, "release: ")algorithmLib = null}}
2.4 創建 AssetsHelper.kt
????????由于算法庫的商品數據庫,采用的本地json數據庫,所以當前技術方案就是通過app預先在源代碼路徑中放好json文件,然后動態將json文件拷貝到應用路徑下,方便算法庫讀取與查詢。
object AssetsHelper {fun getJsonFilePath(context: Context, assetName: String): String {val targetPath =AGGFileUtils.getMediaOutputDirectory(context as ContextWrapper).absolutePath + File.separator + assetNamecopyAssetToFile(context, assetName, targetPath)return targetPath}private fun copyAssetToFile(context: Context, assetName: String, targetPath: String) {val assetManager = context.assetsval inputStream: InputStream = assetManager.open(assetName)val file = File(targetPath)val outputStream = FileOutputStream(file)val buffer = ByteArray(1024)var read: Intwhile (inputStream.read(buffer).also { read = it } != -1) {outputStream.write(buffer, 0, read)}outputStream.flush()outputStream.close()inputStream.close()}}
????????在src/main/assets/ 路徑下放置 barcode_information_final.json 文件。
2.5 創建 ProductBean.kt
????????商品信息的數據Bean,通過算法庫返回的商品Json數據格式轉換。
data class ProductBean(var product_name: String = "",var brand: String = "",var specifications: String = "",var price: String = "",var country_of_origin: String = "",var tax_rate: String = "",var tax_code: String = "",var function_and_use: String = "",
)data class ProductInfo(var product_info: ProductBean)/*
{"product_info": {"brand": "舒膚佳","country_of_origin": "中國","function_and_use": "用于日常清潔皮膚,有效去除污垢和細菌","price": "¥3.50","product_name": "香皂","specifications": "115G","tax_code": "1","tax_rate": "13%"}
}
*/
3. ?? 算法so庫JNI模塊
-
新建CMakeLists.txt,引入算法so庫以及頭文件;
-
新建cpp文件,調用算法so庫方法;
-
新建native方法,加載JNI模塊生成的庫以及映射對應方法。
3.1 新建CMakeLists.txt,引入算法so庫以及頭文件
1)CMakeLists.txt 內容如下:
# 1. 設置 CMake 的最低版本要求
cmake_minimum_required(VERSION 3.22.1)# 2. 設置項目名稱和支持的語言
project(BarcodeIdentify C CXX)# 3.1 指定頭文件目錄
include_directories(${PROJECT_SOURCE_DIR}/include)# 3.2 添加一個共享庫目標:創建一個共享庫來使用.so 庫
add_library( # Sets the name of the library.barcode_match_jni# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).BarcodeMatchJni.cpp)
add_library( # Sets the name of the library.lib_barcode_match_interface# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).IMPORTED)
add_library( # Sets the name of the library.lib_barcode_match# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).IMPORTED)
add_library( # Sets the name of the library.lib_ZXing# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).IMPORTED)# 4. 設置 .so 文件的路徑
set_target_properties(lib_barcode_match_interfacePROPERTIES IMPORTED_LOCATION${PROJECT_SOURCE_DIR}/lib/armeabi-v7a/libbarcode_match_interface.so)
set_target_properties(lib_barcode_matchPROPERTIES IMPORTED_LOCATION${PROJECT_SOURCE_DIR}/lib/armeabi-v7a/libbarcode_match.so)
set_target_properties(lib_ZXingPROPERTIES IMPORTED_LOCATION${PROJECT_SOURCE_DIR}/lib/armeabi-v7a/libZXing.so)# 5.1 鏈接系統庫(例如 log 庫)
find_library( # Sets the name of the path variable.log-lib# Specifies the name of the NDK library that# you want CMake to locate.log)
# 5.2 鏈接.so 庫到目標
target_link_libraries( # Specifies the target library.barcode_match_jnilib_barcode_match_interfacelib_barcode_matchlib_ZXing# Links the target library to the log library# included in the NDK.${log-lib})
2)引入算法so庫以及頭文件
????????算法 barcode_match_interface.h 頭文件如下:
#ifndef BARCODE_MATCH_INTERFACE_H
#define BARCODE_MATCH_INTERFACE_H
#include <string>
class barcode_match_interface
{
private:void *barcode_match;
public:barcode_match_interface(/* args */);~barcode_match_interface();int init_barcode_match_interface(std::string json_input_path);std::string barcode_match_process(std::string input_img_path);
};#endif
3.2 新建cpp文件,調用算法so庫方法
#include <jni.h>
#include <string>
#include <android/log.h>
#include "barcode_match_interface.h" // 包含算法庫的頭文件#define ALOGD(tag, ...) __android_log_print(ANDROID_LOG_DEBUG, tag, __VA_ARGS__)// 創建一個全局指針,用于存儲 BarcodeMatch 類的實例
barcode_match_interface* barcodeMatchInstance = nullptr;// 實現 JNI 方法:initMatch
extern "C"
JNIEXPORT jint JNICALL
Java_com_agg_mocamera_portal_feature_demo_lib_AlgorithmLib_initMatch(JNIEnv* env, jobject thiz, jstring jsonInputPath) {const char* nativeJsonInputPath = env->GetStringUTFChars(jsonInputPath, nullptr);barcodeMatchInstance = new barcode_match_interface();int result = barcodeMatchInstance->init_barcode_match_interface(std::string(nativeJsonInputPath));env->ReleaseStringUTFChars(jsonInputPath, nativeJsonInputPath);if (result != 0) {delete barcodeMatchInstance;barcodeMatchInstance = nullptr;}return result;return 0;
}// 實現 JNI 方法:matchBarcode
extern "C"
JNIEXPORT jstring JNICALL
Java_com_agg_mocamera_portal_feature_demo_lib_AlgorithmLib_matchBarcode(JNIEnv *env, jobject thiz, jstring inputImagePath) {const char* nativeInputImagePath = env->GetStringUTFChars(inputImagePath, nullptr);std::string result;if (barcodeMatchInstance != nullptr) {result = barcodeMatchInstance->barcode_match_process(std::string(nativeInputImagePath));} else {result = "Error: BarcodeMatch instance not initialized";}ALOGD("TAG-AGG","%s", result.c_str());env->ReleaseStringUTFChars(inputImagePath, nativeInputImagePath);return env->NewStringUTF(result.c_str());
}
3.3 新建native方法,加載JNI模塊生成的庫以及映射對應方法
class AlgorithmLib {external fun initMatch(jsonInputPath: String): Intexternal fun matchBarcode(imagePath: String): Stringcompanion object {init {System.loadLibrary("barcode_match_jni")}}}
3.4 配置 build.gradle 文件
????????在build.gradle文件中,確保項目支持所需的ABI架構。
android {defaultConfig {ndk {abiFilters 'armeabi-v7a'}}
????????在 build.gradle 文件中配置 CMake。
android {externalNativeBuild {cmake {path "src/main/cpp/CMakeLists.txt"}}
注:對于算法條形碼識別模塊,由于篇幅與技術棧問題,本文就不再贅述。
4. ? 小結
????????對于條形碼識別,本文只是一個基礎實現方案,更多業務細節請參考產品邏輯去實現。
????????另外,由于本人能力有限,如有錯誤,敬請批評指正,謝謝。