【发布时间】:2021-01-10 10:20:21
【问题描述】:
您好,我正在使用 android mlkit 条码扫描仪,没有带有 androidx 的 firebase,我遵循此代码 https://medium.com/@surya.n1447/google-vision-ml-kit-with-camerax-64bbbfd4c6fd 当我扫描二维码时,它太慢了,我不知道如何提高扫描速度,是否有一些技巧或类似的东西?还是改用 zxing 或 Google vision 更好? 我用的是小米 10 t pro
类 ScanPersonFragment : Fragment() {
private var processingBarcode = AtomicBoolean(false)
private var mediaPlayer: MediaPlayer? = null
private lateinit var cameraExecutor: ExecutorService
private lateinit var scanBarcodeViewModel: ScanPersonViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
cameraExecutor = Executors.newSingleThreadExecutor()
scanBarcodeViewModel = ViewModelProvider(this).get(ScanPersonViewModel::class.java)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val v = inflater.inflate(R.layout.fragment_scan_person_destination, container, false)
mediaPlayer = MediaPlayer.create(context, R.raw.beep)
scanBarcodeViewModel.progressState.observe(viewLifecycleOwner, {
v.fragment_scan_person_barcode_progress_bar.visibility = if (it) View.VISIBLE else View.GONE
})
scanBarcodeViewModel.navigation.observe(viewLifecycleOwner, { navDirections ->
navDirections?.let {
findNavController().navigate(navDirections)
scanBarcodeViewModel.doneNavigating()
}
})
return v
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (allPermissionsGranted()) {
startCamera()
} else {
requestPermissions(
REQUIRED_PERMISSIONS,
REQUEST_CODE_PERMISSIONS
)
}
}
override fun onResume() {
super.onResume()
processingBarcode.set(false)
}
private fun startCamera() {
// Create an instance of the ProcessCameraProvider,
// which will be used to bind the use cases to a lifecycle owner.
val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
val imageCapture = ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
//.setTargetResolution(Size(400, 400))
.build()
// Add a listener to the cameraProviderFuture.
// The first argument is a Runnable, which will be where the magic actually happens.
// The second argument (way down below) is an Executor that runs on the main thread.
cameraProviderFuture.addListener({
// Add a ProcessCameraProvider, which binds the lifecycle of your camera to
// the LifecycleOwner within the application's life.
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
// Initialize the Preview object, get a surface provider from your PreviewView,
// and set it on the preview instance.
val preview = Preview.Builder().build().also {
it.setSurfaceProvider(
fragment_scan_person_barcode_preview_view.surfaceProvider
)
}
// Setup the ImageAnalyzer for the ImageAnalysis use case
val imageAnalysis = ImageAnalysis.Builder()
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build()
.also {
it.setAnalyzer(cameraExecutor, BarcodeAnalyzer { barcode ->
if (processingBarcode.compareAndSet(false, true)) {
mediaPlayer?.start()
searchBarcode(barcode)
}
})
}
// Select back camera
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
// Unbind any bound use cases before rebinding
cameraProvider.unbindAll()
// Bind use cases to lifecycleOwner
cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageAnalysis)
} catch (e: Exception) {
Log.e("PreviewUseCase", "Binding failed! :(", e)
}
}, ContextCompat.getMainExecutor(requireContext()))
}
private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(
requireContext(), it
) == PackageManager.PERMISSION_GRANTED
}
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<String>, grantResults:
IntArray
) {
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (allPermissionsGranted()) {
startCamera()
} else {
Toast.makeText(
requireContext(),
"Permissions not granted by the user.",
Toast.LENGTH_SHORT
).show()
}
}
}
private fun searchBarcode(barcode: String) {
scanBarcodeViewModel.searchBarcode(barcode)
}
override fun onDestroy() {
cameraExecutor.shutdown()
super.onDestroy()
}
companion object {
private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
private const val REQUEST_CODE_PERMISSIONS = 10
}
类 BarcodeAnalyzer(private valbarcodeListener: BarcodeListener) : ImageAnalysis.Analyzer {
@SuppressLint("UnsafeExperimentalUsageError")
override fun analyze(imageProxy: ImageProxy) {
val mediaImage = imageProxy.image
if (mediaImage != null) {
val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
val options = BarcodeScannerOptions.Builder().setBarcodeFormats(Barcode.FORMAT_QR_CODE).build()
val scanner = BarcodeScanning.getClient(options)
// Pass image to the scanner and have it do its thing
scanner.process(image)
.addOnSuccessListener { barcodes ->
// Task completed successfully
for (barcode in barcodes) {
barcodeListener(barcode.rawValue ?: "")
}
}
.addOnFailureListener {
// You should really do something about Exceptions
}
.addOnCompleteListener {
// It's important to close the imageProxy
imageProxy.close()
}
}
}
}
【问题讨论】:
-
您能确认图片的图片尺寸吗?小米 Mi 10t pro 有一个 108 兆像素的摄像头。在这种情况下,我们可能想要限制图像大小。我看到您注释掉 setTargetResolution 行,这可能是一个问题。请查看developers.google.com/ml-kit/vision/barcode-scanning/… 以获取有关适当分辨率的指导。
-
另外,如果您目前正在使用 firebase ML Kit,您应该迁移到 ML Kit(没有 Firebase Branding)。我们改进了新 ML Kit 中的条码扫描延迟。这是迁移指南:developers.google.com/ml-kit/migration
-
我有没有 Firebase 云的 ml 套件,我减少了我的 qr 数据,我将分辨率切换到 1920x1080 我尝试使用一些 qr 代码,我发现当我有一个大的 qr 代码时很好,但是当我在 18 毫米磁带上有小 qrcode 会减慢它像 50 到 50 一样一次在缩放后识别一次而不是因为我的情况我需要从 18mm 磁带上读取 qr 代码我发现类似 boofcv 的东西并且工作得很好但我没有'不明白为什么?我认为 google mlkit 是最好的之一,但对我来说速度不够,如果你有一些技巧如何像 scandit 一样快速读取 qr 代码,我会很高兴
-
@EagleCode 您使用的是 API 的捆绑版本还是非捆绑版本?在捆绑的变体中,我们有一个更新的实现,应该可以更快地处理小条形码(相对于整个图像),因为我们检测到条形码,然后在发送到解码器之前对其进行裁剪。在接下来的几个月中,我们正在更新非捆绑式实施。此外,提供一些示例图像(以您使用的分辨率)将有助于我们在此处重现。谢谢!
-
嘿@Chrisito 我已经提交了一个错误报告以及以下链接中要复制的图像,请找到下面的链接。 issuetracker.google.com/u/1/issues/180881635
标签: android computer-vision android-camera firebase-mlkit google-mlkit