检测QR二维码
public boolean detect(Mat img, Mat points)
- 参数一:img,待检测是否含有QR二维码的的灰度图或者彩色(BGR)图像。
- 参数二:points,检测到的QR二维码的最小区域四边形的4个顶点坐标集合。
- 返回值:布尔类型,true,代表检测到QR二维码;false,代表未检测到QR二维码。
public boolean detectMulti(Mat img, Mat points)
- 参数一:img,待检测是否含有QR二维码的的灰度图或者彩色(BGR)图像。
- 参数二:points,多个检测结果QR二维码的最小区域四边形的4个顶点坐标集合。
- 返回值:布尔类型,true,代表检测到QR二维码;false,代表未检测到QR二维码。
识别QR二维码
public String decode(Mat img, Mat points, Mat straight_qrcode)
- 参数一:img,含有QR二维码的灰度图像或者彩色(BGR)图像。
- 参数二:points,detect方法得到的points值。数据量不可为空。
- 参数三:straight_qrcode,经过矫正和二值化的QR二维码。【可选参数】
- 返回值:字符串类型,如果解码失败,则为空串。
public boolean decodeMulti(Mat img, Mat points, List<String> decoded_info, List<Mat> straight_qrcode)
- 参数一:img,含有QR二维码的灰度图像或者彩色(BGR)图像。
- 参数二:points,detect方法得到的points值。数据量不可为空。
- 参数三:decoded_info,多个二维码的解码信息。
- 参数四:straight_qrcode,所有检测到的二维码矫正和二值化的后的结果集合。【可选参数】
- 返回值:布尔类型,true,代表解码成功,反之,解码失败。
检测并识别QR二维码
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<String> decoded_info, Mat points, List<Mat> 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)
mSheetBinding.tvDecode.setOnClickListener {
mOperationSheet.dismiss()
doDecode()
}
mPhotoSheet = BottomSheetDialog(this)
|