Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android OpenCV实现QR二维码检测

Android基于OpenCV实现QR二维码检测

作者:易冬

QR码比普通一维条码具有快速读取和更大的存储资料容量,也无需要像一维条码般在扫描时需要直线对准扫描仪。因此其应用范围已经扩展到包括产品跟踪,物品识别,文档管理,库存营销等方面。本文讲解Android基于OpenCV实现QR二维码检测的步骤

QR二维码

QR码(英语:Quick Response Code;全称为快速响应矩阵图码)是二维码的一种,于1994年由日本DENSO WAVE公司发明。QR来自英文Quick Response的缩写,即快速反应,因为发明者希望QR码可以快速解码其内容。QR码使用四种标准化编码模式(数字、字母数字、字节(二进制)和日文(Shift_JIS))来存储数据。QR码常见于日本,为目前日本最通用的二维空间条码,在世界各国广泛运用于手机读码操作。QR码比普通一维条码具有快速读取和更大的存储资料容量,也无需要像一维条码般在扫描时需要直线对准扫描仪。因此其应用范围已经扩展到包括产品跟踪,物品识别,文档管理,库存营销等方面。【维基百科】

QR二维码格式

QR码呈正方形,常见的是黑白两色。在3个角落,印有较小,像“回”字的正方图案。这3个是帮助解码软件定位的图案,用户不需要对准,无论以任何角度扫描,资料仍然可以正确被读取。日本QR码的标准JIS X 0510在1999年1月发布,而其对应的ISO国际标准ISO/IEC18004,则在2000年6月获得批准。根据Denso Wave公司的网站资料,QR码是属于开放式的标准,QR码的规格公开,虽由Denso Wave公司持有的专利权益,但不会被运行。除了标准的QR码之外,也存在一种称为“微型QR码”的格式,是QR码标准的缩小版本,主要是为了无法处理较大型扫描的应用而设计。微型QR码同样有多种标准,最高可存储35个字符。【维基百科】

QR二维码结构

QR码最大特征为其左上,右上,左下三个大型的如同“回”字的黑白间同心方图案,为QR码识别定位标记,失去其中一个会影响识别。而呈棋盘般分布的有别与大定位标记的较小的同心方则为其校正标记,用于校正识别,版本1没有校正标记,版本2在右下方,其中心点在左下和右上定位标记的外边框的相交点,版本10开始以每个等距的方式出现在右下校正点至左下和右上定位标记的外边框的连线、左上与左下定位标记的外边框的连线、左上与右上定位标记的外边框的连线之间、这四边线上等距点对边相连线,版本10等距有1个,版本25为3个,版本40为5个。【维基百科】

API

QRCodeDetector类结构

检测QR二维码

public boolean detect(Mat img, Mat points)
public boolean detectMulti(Mat img, Mat points)

识别QR二维码

public String decode(Mat img, Mat points, Mat straight_qrcode) 
public boolean decodeMulti(Mat img, Mat points, List<String> decoded_info, List<Mat> straight_qrcode)

检测并识别QR二维码

public String detectAndDecode(Mat img, Mat points, Mat straight_qrcode)
public boolean detectAndDecodeMulti(Mat img, List<String> decoded_info, Mat points, List<Mat> 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()
            } else {
                Snackbar.make(mBinding.root, "解码结果:$result", 3000).show()
            }
            Log.d(App.TAG, result)
        } else {
            Toast.makeText(applicationContext, "未检测到QRCode", Toast.LENGTH_SHORT).show()
        }
    }

    private fun selectMedia() {
        if (this::mPhotoSheet.isInitialized) {
            mPhotoSheet.show()
        }
    }

    private fun selectOps() {
        if (this::mOperationSheet.isInitialized) {
            mOperationSheet.show()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_qr_detect, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu_pick_photo -> selectMedia()
            R.id.menu_qr_ops -> selectOps()
        }
        return true
    }

    override fun onDestroy() {
        mSource.release()
        mGray.release()
        super.onDestroy()
    }
}

结果

源码

github.com/onlyloveyd/…

以上就是Android基于OpenCV实现QR二维码检测的详细内容,更多关于Android OpenCV实现QR二维码检测的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文