gradle 适配最新开发工具 android studio2024.2.2
parent
06bea1aa89
commit
1f59c77b7d
@ -0,0 +1,45 @@
|
|||||||
|
package com.rehome.dywoa.Listener
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.camera.core.ImageAnalysis
|
||||||
|
import androidx.camera.core.ImageProxy
|
||||||
|
import com.google.mlkit.vision.barcode.Barcode
|
||||||
|
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
|
||||||
|
import com.google.mlkit.vision.barcode.BarcodeScanning
|
||||||
|
import com.google.mlkit.vision.common.InputImage
|
||||||
|
|
||||||
|
class QRCodeAnalyser(private val listener: (List<Barcode>, Int, Int) -> Unit) :
|
||||||
|
ImageAnalysis.Analyzer {
|
||||||
|
//配置当前扫码格式
|
||||||
|
private val options = BarcodeScannerOptions.Builder()
|
||||||
|
.setBarcodeFormats(
|
||||||
|
Barcode.FORMAT_QR_CODE,
|
||||||
|
Barcode.FORMAT_AZTEC
|
||||||
|
).build()
|
||||||
|
|
||||||
|
//获取解析器
|
||||||
|
private val detector = BarcodeScanning.getClient(options)
|
||||||
|
|
||||||
|
@SuppressLint("UnsafeExperimentalUsageError", "UnsafeOptInUsageError")
|
||||||
|
override fun analyze(imageProxy: ImageProxy) {
|
||||||
|
val mediaImage = imageProxy.image ?: kotlin.run {
|
||||||
|
imageProxy.close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
|
||||||
|
detector.process(image)
|
||||||
|
.addOnSuccessListener { barCodes ->
|
||||||
|
Log.i("app", "barCodes: ${barCodes.size}")
|
||||||
|
if (barCodes.size > 0) {
|
||||||
|
listener.invoke(barCodes, imageProxy.width, imageProxy.height)
|
||||||
|
//接收到结果后,就关闭解析
|
||||||
|
detector.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.addOnFailureListener { Log.i("app", "Error: ${it.message}") }
|
||||||
|
.addOnCompleteListener { imageProxy.close() }
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 3.3 KiB |
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:shape="rectangle">
|
||||||
|
<solid android:color="#00ffffff" />
|
||||||
|
|
||||||
|
<stroke
|
||||||
|
android:width="2dp"
|
||||||
|
android:color="#FF00FF00" />
|
||||||
|
<corners
|
||||||
|
android:bottomLeftRadius="1px"
|
||||||
|
android:bottomRightRadius="1px"
|
||||||
|
android:topLeftRadius="1px"
|
||||||
|
android:topRightRadius="1px" />
|
||||||
|
</shape>
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
<?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_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical"
|
||||||
|
tools:context=".ui.activity.QrCodeActivity">
|
||||||
|
|
||||||
|
<include
|
||||||
|
android:id="@+id/toolbarView"
|
||||||
|
layout="@layout/layout_base" />
|
||||||
|
|
||||||
|
<FrameLayout
|
||||||
|
android:id="@+id/fr_camera"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
<androidx.camera.view.PreviewView
|
||||||
|
android:id="@+id/previewView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="center"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:orientation="vertical">
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@drawable/scan_bg"
|
||||||
|
android:orientation="vertical">
|
||||||
|
<com.rehome.dywoa.weiget.ScanView
|
||||||
|
android:id="@+id/scanView"
|
||||||
|
android:layout_width="250dp"
|
||||||
|
android:layout_height="250dp"
|
||||||
|
android:background="@android:color/transparent" />
|
||||||
|
</LinearLayout>
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="10dp"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textColor="#666666"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:text="@string/scan_text"/>
|
||||||
|
</LinearLayout>
|
||||||
|
</FrameLayout>
|
||||||
|
</LinearLayout>
|
||||||
Loading…
Reference in New Issue