gpt4 book ai didi

android - 第一次检测后停止移动视觉 API

转载 作者:行者123 更新时间:2023-12-02 12:46:23 24 4
gpt4 key购买 nike

我正在尝试使用谷歌移动视觉 API 检测 QR 码。

问题是,在检测到二维码后,只要二维码对摄像头可见,api 就会不断调用“receiveDetections”函数。我需要在第一次检测后停止并将结果发送到我的服务器以验证此代码。如何在第一次检测后停止进程?

    override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.qrcode_scanner)

detector = BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.ALL_FORMATS).build()
detector.setProcessor(object: Detector.Processor<Barcode> {
override fun release() {
override fun receiveDetections(detections: Detector.Detections<Barcode>?) {

val barcodes = detections?.detectedItems
if(barcodes!!.size()>0) {
Log.e("qrcode",barcodes.valueAt(0).displayValue)
sendQRCodeToServer(url,barcodes.valueAt(0).displayValue)

}
}

})


cameraSource = CameraSource.Builder(this,detector).setRequestedPreviewSize(1920,1080).setRequestedFps(25f).setAutoFocusEnabled(true).build()

svBarcode.holder.addCallback(object: SurfaceHolder.Callback2 {
override fun surfaceRedrawNeeded(holder: SurfaceHolder?) {
}
override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
}

override fun surfaceDestroyed(holder: SurfaceHolder?) {
cameraSource.stop()
}



override fun surfaceCreated(holder: SurfaceHolder?) {

if(ContextCompat.checkSelfPermission(this@Scanner,
Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
cameraSource.start(holder)
startAnimation()

} else ActivityCompat.requestPermissions(this@Scanner, arrayOf(Manifest.permission.CAMERA),123)

}

})
}

}

override fun onDestroy() {
super.onDestroy()
detector.release()
cameraSource.stop()
cameraSource.release()
}

最佳答案

你可以创建停止相机的功能,例如

private fun stopCamera(){
cameraSource.stop()
}

detector = BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.ALL_FORMATS).build()
detector.setProcessor(object: Detector.Processor<Barcode> {
override fun release() {
override fun receiveDetections(detections: Detector.Detections<Barcode>?) {

val barcodes = detections?.detectedItems
if(barcodes!!.size()>0) {
Log.e("qrcode",barcodes.valueAt(0).displayValue)
sendQRCodeToServer(url,barcodes.valueAt(0).displayValue)
//add this to stop camera
stopCamera()
}
}

})

编辑:首先为标志检测创建变量

//to flag first detection
private var firstDetection=true

override fun onCreate(savedInstanceState: Bundle?) {
///.......
detector = BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.ALL_FORMATS).build()
detector.setProcessor(object: Detector.Processor<Barcode> {
override fun release() {

}
override fun receiveDetections(detections: Detector.Detections<Barcode>?) {

val barcodes = detections?.detectedItems
//check firstDetection
if(barcodes!!.size()>0 && firstDetection) {
sendQRCodeToServer(url,barcodes.valueAt(0).displayValue)
//set firstDetection
firstDetection=false
}
}

})
}
}

希望这对您有所帮助....

关于android - 第一次检测后停止移动视觉 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59416861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com