Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zxing-cpp/example/ZXingOpenCV.h @ 3:2c135c81b16c
MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:44:09 +0200 |
| parents | b50eed0cc0ef |
| children |
comparison
equal
deleted
inserted
replaced
| 0:6015a75abc2d | 3:2c135c81b16c |
|---|---|
| 1 /* | |
| 2 * Copyright 2020 Axel Waggershauser | |
| 3 */ | |
| 4 // SPDX-License-Identifier: Apache-2.0 | |
| 5 | |
| 6 #pragma once | |
| 7 | |
| 8 #include "ReadBarcode.h" | |
| 9 | |
| 10 #include <opencv2/opencv.hpp> | |
| 11 | |
| 12 inline ZXing::ImageView ImageViewFromMat(const cv::Mat& image) | |
| 13 { | |
| 14 using ZXing::ImageFormat; | |
| 15 | |
| 16 auto fmt = ImageFormat::None; | |
| 17 switch (image.channels()) { | |
| 18 case 1: fmt = ImageFormat::Lum; break; | |
| 19 case 2: fmt = ImageFormat::LumA; break; | |
| 20 case 3: fmt = ImageFormat::BGR; break; | |
| 21 case 4: fmt = ImageFormat::BGRA; break; | |
| 22 } | |
| 23 | |
| 24 if (image.depth() != CV_8U || fmt == ImageFormat::None) | |
| 25 return {nullptr, 0, 0, ImageFormat::None}; | |
| 26 | |
| 27 return {image.data, image.cols, image.rows, fmt}; | |
| 28 } | |
| 29 | |
| 30 inline ZXing::Barcodes ReadBarcodes(const cv::Mat& image, const ZXing::ReaderOptions& options = {}) | |
| 31 { | |
| 32 return ZXing::ReadBarcodes(ImageViewFromMat(image), options); | |
| 33 } | |
| 34 | |
| 35 inline void DrawBarcode(cv::Mat& img, ZXing::Barcode barcode) | |
| 36 { | |
| 37 auto pos = barcode.position(); | |
| 38 auto zx2cv = [](ZXing::PointI p) { return cv::Point(p.x, p.y); }; | |
| 39 auto contour = std::vector<cv::Point>{zx2cv(pos[0]), zx2cv(pos[1]), zx2cv(pos[2]), zx2cv(pos[3])}; | |
| 40 const auto* pts = contour.data(); | |
| 41 int npts = contour.size(); | |
| 42 | |
| 43 cv::polylines(img, &pts, &npts, 1, true, CV_RGB(0, 255, 0)); | |
| 44 cv::putText(img, barcode.text(), zx2cv(pos[3]) + cv::Point(0, 20), cv::FONT_HERSHEY_DUPLEX, 0.5, CV_RGB(0, 255, 0)); | |
| 45 } |
