diff mupdf-source/thirdparty/zxing-cpp/example/ZXingOpenCV.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mupdf-source/thirdparty/zxing-cpp/example/ZXingOpenCV.h	Mon Sep 15 11:43:07 2025 +0200
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2020 Axel Waggershauser
+ */
+// SPDX-License-Identifier: Apache-2.0
+
+#pragma once
+
+#include "ReadBarcode.h"
+
+#include <opencv2/opencv.hpp>
+
+inline ZXing::ImageView ImageViewFromMat(const cv::Mat& image)
+{
+	using ZXing::ImageFormat;
+
+	auto fmt = ImageFormat::None;
+	switch (image.channels()) {
+	case 1: fmt = ImageFormat::Lum; break;
+	case 2: fmt = ImageFormat::LumA; break;
+	case 3: fmt = ImageFormat::BGR; break;
+	case 4: fmt = ImageFormat::BGRA; break;
+	}
+
+	if (image.depth() != CV_8U || fmt == ImageFormat::None)
+		return {nullptr, 0, 0, ImageFormat::None};
+
+	return {image.data, image.cols, image.rows, fmt};
+}
+
+inline ZXing::Barcodes ReadBarcodes(const cv::Mat& image, const ZXing::ReaderOptions& options = {})
+{
+	return ZXing::ReadBarcodes(ImageViewFromMat(image), options);
+}
+
+inline void DrawBarcode(cv::Mat& img, ZXing::Barcode barcode)
+{
+	auto pos = barcode.position();
+	auto zx2cv = [](ZXing::PointI p) { return cv::Point(p.x, p.y); };
+	auto contour = std::vector<cv::Point>{zx2cv(pos[0]), zx2cv(pos[1]), zx2cv(pos[2]), zx2cv(pos[3])};
+	const auto* pts = contour.data();
+	int npts = contour.size();
+
+	cv::polylines(img, &pts, &npts, 1, true, CV_RGB(0, 255, 0));
+	cv::putText(img, barcode.text(), zx2cv(pos[3]) + cv::Point(0, 20), cv::FONT_HERSHEY_DUPLEX, 0.5, CV_RGB(0, 255, 0));
+}