public String detectAndDecode(Mat img, Mat points, Mat straight_qrcode)
- 参数一:img,含有QR二维码的灰度图像或者彩色(BGR)图像。
- 参数二:points,检测到的QR二维码的最小区域四边形的4个顶点坐标。
- 参数三:straight_qrcode,经过矫正和二值化的QR二维码。【可选参数】
- 返回值:字符串类型,如果解码失败,则为空串。
public boolean detectAndDecodeMulti(Mat img, List decoded_info, Mat points, List straight_qrcode)
- 参数一:img,含有QR二维码的灰度图像或者彩色(BGR)图像。
- 参数二:decoded_info,多个二维码的解码信息。
- 参数三:points,检测到的多个QR二维码的最小区域四边形的4个顶点坐标集合。【可选参数】
- 参数四:straight_qrcode,所有检测到的二维码矫正和二值化的后的结果集合。【可选参数】
- 返回值:字符串类型,如果解码失败,则为空串。
操作
/**
- QR二维码检测
- author: yidong
- 2020/10/27
*/
class QRDetectActivity : AppCompatActivity() {
private lateinit var mBinding: ActivityQrDetectBinding
private lateinit var mQRCodeDetector: QRCodeDetector
private var mPhotoSavePath = “”
private lateinit var mUri: Uri
private lateinit var mSource: Mat
private lateinit var mGray: Mat
private lateinit var mOperationSheet: BottomSheetDialog
private lateinit var mSheetBinding: LayoutQrDetectOpBinding
private lateinit var mPhotoSheet: BottomSheetDialog
private lateinit var mPhotoOpBinding: LayoutPhotoOpBinding
// 请求相机权限
private val requestCameraPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
if (it) {
mPhotoSavePath =
cacheDir.path + File.separator + “${System.currentTimeMillis()}.png”
mUri = MediaStoreUtils.getIntentUri(this, File(mPhotoSavePath))
requestCamera.launch(mUri)
} else {
Toast.makeText(applicationContext, “无相机权限”, Toast.LENGTH_SHORT).show()
}
}
// 请求外部存储权限
private val requestStoragePermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
if (it) {
pickImage.launch(“image/*”)
} else {
Toast.makeText(applicationContext, “无存储权限”, Toast.LENGTH_SHORT).show()
}
}
private val requestCamera = registerForActivityResult(ActivityResultContracts.TakePicture()) {
if (it) {
val bgr = Imgcodecs.imread(mPhotoSavePath, Imgcodecs.IMREAD_COLOR)
if (bgr.empty()) {
Toast.makeText(applicationContext, “读取拍照结果失败”, Toast.LENGTH_SHORT).show()
return@registerForActivityResult
} else {
Imgproc.cvtColor(bgr, mSource, Imgproc.COLOR_BGR2RGB)
Imgproc.cvtColor(bgr, mGray, Imgproc.COLOR_BGR2GRAY)
mBinding.ivLena.showMat(mSource)
}
} else {
Toast.makeText(applicationContext, “拍照失败”, Toast.LENGTH_SHORT).show()
}
}
private val pickImage = registerForActivityResult(ActivityResultContracts.GetContent()) {
if (it != null) {
val filePath = MediaStoreUtils.getMediaPath(this, it)
if (filePath.isNullOrEmpty()) {
Toast.makeText(applicationContext, “读取图片失败”, Toast.LENGTH_SHORT).show()
return@registerForActivityResult
}
val bgr = Imgcodecs.imread(filePath, Imgcodecs.IMREAD_COLOR)
if (bgr.empty()) {
Toast.makeText(applicationContext, “读取图片失败”, Toast.LENGTH_SHORT).show()
return@registerForActivityResult
} else {
Imgproc.cvtColor(bgr, mSource, Imgproc.COLOR_BGR2RGB)
Imgproc.cvtColor(bgr, mGray, Imgproc.COLOR_BGR2GRAY)
mBinding.ivLena.showMat(mSource)
}
} else {
Toast.makeText(applicationContext, “选图失败”, Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_qr_detect)
mQRCodeDetector = QRCodeDetector()
mSource = Mat()
mGray = Mat()
val bgr = Utils.loadResource(this, R.drawable.qrcode)
Imgproc.cvtColor(bgr, mSource, Imgproc.COLOR_BGR2RGB)
Imgproc.cvtColor(bgr, mGray, Imgproc.COLOR_BGR2GRAY)
mBinding.ivLena.showMat(mSource)
createDialog()
}
private fun createDialog() {
mOperationSheet = BottomSheetDialog(this)
mSheetBinding = LayoutQrDetectOpBinding.inflate(layoutInflater, null, false)
mOperationSheet.setContentView(mSheetBinding.root)
mSheetBinding.tvDetect.setOnClickListener {
mOperationSheet.dismiss()
doDetect()
}
mSheetBinding.tvDecode.setOnClickListener {
mOperationSheet.dismiss()
doDecode()
}
mPhotoSheet = BottomSheetDialog(this)
mPhotoOpBinding = LayoutPhotoOpBinding.inflate(layoutInflater, null, false)
mPhotoSheet.setContentView(mPhotoOpBinding.root)
mPhotoOpBinding.tvCamera.setOnClickListener {
mPhotoSheet.dismiss()
requestCameraPermission.launch(
Manifest.permission.CAMERA
)
}
mPhotoOpBinding.tvPhoto.setOnClickListener {
mPhotoSheet.dismiss()
requestStoragePermission.launch(
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
}
}
private fun doDetect() {
val points = Mat()
val isHasQr = mQRCodeDetector.detect(mSource, points)
if (isHasQr) {
val pointArr = FloatArray(8)
points.get(0, 0, pointArr)
Log.d(App.TAG, pointArr.toList().toString())
val tmp = mSource.clone()
for (i in pointArr.indices step 2) {
val start = Point(pointArr[i % 8].toDouble(), pointArr[(i + 1) % 8].toDouble())
val end = Point(pointArr[(i + 2) % 8].toDouble(), pointArr[(i + 3) % 8].toDouble())
Imgproc.line(tmp, start, end, Scalar(255.0, 0.0, 0.0), 8, Imgproc.LINE_8)
}
mBinding.ivResult.showMat(tmp)
tmp.release()
}
}
private fun doDecode() {
val points = Mat()
val isHasQr = mQRCodeDetector.detect(mGray, points)
if (isHasQr) {
val result = mQRCodeDetector.decode(mGray, points)
if (result.isEmpty()) {
Toast.makeText(applicationContext, “无法解码”, Toast.LENGTH_SHORT).show()
最后
希望本文对你有所启发,有任何面试上的建议也欢迎留言分享给大家。
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
好了~如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!
得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
[外链图片转存中…(img-ISBEe4Bl-1719205875340)]
为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!