From b337c50bb064692395578e675c09f2bc50e3fc66 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 5 Sep 2025 03:24:06 +0000 Subject: [PATCH 1/2] Initial plan From 76dc1f900863ed1ec4361c5bcc4236eb88d94d15 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 5 Sep 2025 03:38:06 +0000 Subject: [PATCH 2/2] Add comprehensive QRCode type definitions and documentation Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com> --- doc/QRCode-Support.md | 81 ++++++++++++++ src/types/opencv/objdetect.ts | 198 ++++++++++++++++++++++++++++++++++ test/QRCodeDetector.test.ts | 53 +++++++++ test/QRCodeEncoder.test.ts | 55 ++++++++++ 4 files changed, 387 insertions(+) create mode 100644 doc/QRCode-Support.md create mode 100644 test/QRCodeDetector.test.ts create mode 100644 test/QRCodeEncoder.test.ts diff --git a/doc/QRCode-Support.md b/doc/QRCode-Support.md new file mode 100644 index 0000000..eaac8e4 --- /dev/null +++ b/doc/QRCode-Support.md @@ -0,0 +1,81 @@ +# QRCode Support in OpenCV.js + +This document explains the current state of QR code functionality in opencv-js. + +## Available QR Code Functionality + +### QRCodeDetector ✅ +The `QRCodeDetector` class is fully available and functional: + +```typescript +const detector = new cv.QRCodeDetector(); +const image = cv.imread(imageElement); +const points = new cv.Mat(); +const straightQrcode = new cv.Mat(); + +// Detect and decode QR code +const decodedText = detector.detectAndDecode(image, points, straightQrcode); +console.log("Decoded text:", decodedText); +``` + +### QRCodeDetectorAruco ✅ +Enhanced QR code detection using ArUco-based methods: + +```typescript +const params = new cv.QRCodeDetectorAruco_Params(); +const detector = new cv.QRCodeDetectorAruco(params); +``` + +### QRCodeEncoder Constants ✅ +All QRCodeEncoder constants are available: + +```typescript +// Correction levels +cv.QRCodeEncoder_CORRECT_LEVEL_L // Low +cv.QRCodeEncoder_CORRECT_LEVEL_M // Medium +cv.QRCodeEncoder_CORRECT_LEVEL_Q // Quartile +cv.QRCodeEncoder_CORRECT_LEVEL_H // High + +// Encoding modes +cv.QRCodeEncoder_MODE_NUMERIC +cv.QRCodeEncoder_MODE_ALPHANUMERIC +cv.QRCodeEncoder_MODE_BYTE +cv.QRCodeEncoder_MODE_KANJI +cv.QRCodeEncoder_MODE_ECI +cv.QRCodeEncoder_MODE_STRUCTURED_APPEND +cv.QRCodeEncoder_MODE_AUTO + +// ECI encodings +cv.QRCodeEncoder_ECI_UTF8 +``` + +### QRCodeEncoder Enums ✅ +Enum classes are available: + +```typescript +cv.QRCodeEncoder_CorrectionLevel +cv.QRCodeEncoder_EncodeMode +cv.QRCodeEncoder_ECIEncodings +``` + +## Missing Functionality + +### QRCodeEncoder Class ❌ +The actual `QRCodeEncoder` class is **not available** in the current OpenCV.js build, although TypeScript definitions are provided for future compatibility. + +```typescript +// This will throw an error: +const encoder = new cv.QRCodeEncoder(); // ❌ Not available +``` + +## Workarounds + +For QR code generation, consider using external libraries such as: + +- [qrcode](https://www.npmjs.com/package/qrcode) +- [qr-code-generator](https://www.npmjs.com/package/qr-code-generator) +- [qrious](https://www.npmjs.com/package/qrious) + +## Future Support + +The TypeScript definitions for `QRCodeEncoder` are included for when this functionality becomes available in future OpenCV.js builds. \ No newline at end of file diff --git a/src/types/opencv/objdetect.ts b/src/types/opencv/objdetect.ts index 5b8be12..bb55904 100644 --- a/src/types/opencv/objdetect.ts +++ b/src/types/opencv/objdetect.ts @@ -101,3 +101,201 @@ export declare const CASCADE_SCALE_IMAGE: any; // initializer: = 2 export declare const CASCADE_FIND_BIGGEST_OBJECT: any; // initializer: = 4 export declare const CASCADE_DO_ROUGH_SEARCH: any; // initializer: = 8 + +/** + * QRCodeDetector class for detecting and decoding QR codes. + */ +export declare class QRCodeDetector { + /** + * Creates a new QRCodeDetector instance. + */ + constructor(); + + /** + * Detects QR codes in the image. + * @param img Input image + * @param points Output array of vertices of the found QR code quadrangle + * @returns True if QR code was detected + */ + detect(img: any, points?: any): boolean; + + /** + * Decodes QR code from the image. + * @param img Input image containing QR code + * @param points Vertices of the QR code quadrangle + * @param straight_qrcode Output image of the rectified QR code + * @returns Decoded string + */ + decode(img: any, points?: any, straight_qrcode?: any): string; + + /** + * Detects and decodes QR code in one step. + * @param img Input image + * @param points Output array of vertices of the found QR code quadrangle + * @param straight_qrcode Output image of the rectified QR code + * @returns Decoded string + */ + detectAndDecode(img: any, points?: any, straight_qrcode?: any): string; + + /** + * Releases the object. + */ + delete(): void; +} + +/** + * QRCodeDetectorAruco class for detecting QR codes using ArUco-based method. + */ +export declare class QRCodeDetectorAruco { + /** + * Creates a new QRCodeDetectorAruco instance. + * @param params Optional parameters + */ + constructor(params?: QRCodeDetectorAruco_Params); + + /** + * Detects QR codes in the image. + * @param img Input image + * @param points Output array of vertices of the found QR code quadrangle + * @returns True if QR code was detected + */ + detect(img: any, points?: any): boolean; + + /** + * Decodes QR code from the image. + * @param img Input image containing QR code + * @param points Vertices of the QR code quadrangle + * @param straight_qrcode Output image of the rectified QR code + * @returns Decoded string + */ + decode(img: any, points?: any, straight_qrcode?: any): string; + + /** + * Detects and decodes QR code in one step. + * @param img Input image + * @param points Output array of vertices of the found QR code quadrangle + * @param straight_qrcode Output image of the rectified QR code + * @returns Decoded string + */ + detectAndDecode(img: any, points?: any, straight_qrcode?: any): string; + + /** + * Releases the object. + */ + delete(): void; +} + +/** + * Parameters for QRCodeDetectorAruco. + */ +export declare class QRCodeDetectorAruco_Params { + constructor(); + + /** + * Releases the object. + */ + delete(): void; +} + +/** + * QRCodeEncoder class for generating QR codes. + * + * Note: This class is not currently available in the OpenCV.js build, + * although the constants and enums are available. This is a type definition + * for compatibility and future support. + * + * @see https://github.com/TechStark/opencv-js/issues/58 + */ +export declare class QRCodeEncoder { + /** + * Creates a new QRCodeEncoder instance. + * @param parameters Optional parameters for QR code generation + */ + constructor(parameters?: QRCodeEncoder_Params); + + /** + * Encode text data into a QR code. + * @param encoded_info The text data to encode + * @param qrcode Output matrix containing the generated QR code + */ + encode(encoded_info: string, qrcode: any): void; + + /** + * Encode text data into a QR code with specified parameters. + * @param encoded_info The text data to encode + * @param qrcode Output matrix containing the generated QR code + * @param params Parameters for QR code generation + */ + encodeStructuredAppend(encoded_info: string, qrcode: any, params: QRCodeEncoder_Params): void; +} + +/** + * Parameters for QRCodeEncoder. + */ +export declare class QRCodeEncoder_Params { + constructor(); + + /** + * The version of the QR code (size). If 0, the optimal version is chosen. + */ + version: int; + + /** + * The error correction level. + */ + correction_level: QRCodeEncoder_CorrectionLevel; + + /** + * The encoding mode. + */ + mode: QRCodeEncoder_EncodeMode; + + /** + * The structure append index for structured append mode. + */ + structure_number: int; +} + +/** + * QR Code error correction levels. + */ +export declare enum QRCodeEncoder_CorrectionLevel { + CORRECT_LEVEL_L = 0, + CORRECT_LEVEL_M = 1, + CORRECT_LEVEL_Q = 2, + CORRECT_LEVEL_H = 3 +} + +/** + * QR Code encoding modes. + */ +export declare enum QRCodeEncoder_EncodeMode { + MODE_NUMERIC = 0, + MODE_ALPHANUMERIC = 1, + MODE_BYTE = 2, + MODE_KANJI = 3, + MODE_ECI = 4, + MODE_STRUCTURED_APPEND = 5, + MODE_AUTO = 6 +} + +/** + * QR Code ECI encodings. + */ +export declare enum QRCodeEncoder_ECIEncodings { + ECI_UTF8 = 26 +} + +// QRCodeEncoder constants (already available in runtime) +export declare const QRCodeEncoder_CORRECT_LEVEL_L: any; +export declare const QRCodeEncoder_CORRECT_LEVEL_M: any; +export declare const QRCodeEncoder_CORRECT_LEVEL_Q: any; +export declare const QRCodeEncoder_CORRECT_LEVEL_H: any; +export declare const QRCodeEncoder_MODE_NUMERIC: any; +export declare const QRCodeEncoder_MODE_ALPHANUMERIC: any; +export declare const QRCodeEncoder_MODE_BYTE: any; +export declare const QRCodeEncoder_MODE_KANJI: any; +export declare const QRCodeEncoder_MODE_ECI: any; +export declare const QRCodeEncoder_MODE_STRUCTURED_APPEND: any; +export declare const QRCodeEncoder_MODE_AUTO: any; +export declare const QRCodeEncoder_ECI_UTF8: any; diff --git a/test/QRCodeDetector.test.ts b/test/QRCodeDetector.test.ts new file mode 100644 index 0000000..41b067f --- /dev/null +++ b/test/QRCodeDetector.test.ts @@ -0,0 +1,53 @@ +import { setupOpenCv } from "./cv"; + +beforeAll(setupOpenCv); + +describe("QRCodeDetector Integration", () => { + it("should be able to create QRCodeDetector instance", () => { + expect(() => { + const detector = new cv.QRCodeDetector(); + detector.delete(); + }).not.toThrow(); + }); + + it("should have proper methods on QRCodeDetector", () => { + const detector = new cv.QRCodeDetector(); + + expect(typeof detector.detect).toBe('function'); + expect(typeof detector.decode).toBe('function'); + expect(typeof detector.detectAndDecode).toBe('function'); + + detector.delete(); + }); + + it("should be able to create QRCodeDetectorAruco instance", () => { + expect(() => { + const detector = new cv.QRCodeDetectorAruco(); + detector.delete(); + }).not.toThrow(); + }); + + it("should be able to create QRCodeDetectorAruco with params", () => { + expect(() => { + const params = new cv.QRCodeDetectorAruco_Params(); + const detector = new cv.QRCodeDetectorAruco(params); + + params.delete(); + detector.delete(); + }).not.toThrow(); + }); + + it("should handle empty image gracefully", () => { + const detector = new cv.QRCodeDetector(); + const emptyImage = new cv.Mat(100, 100, cv.CV_8UC3, new cv.Scalar(255, 255, 255)); + + expect(() => { + const result = detector.detectAndDecode(emptyImage); + expect(typeof result).toBe('string'); + expect(result).toBe(''); // Empty string for no QR code found + }).not.toThrow(); + + emptyImage.delete(); + detector.delete(); + }); +}); \ No newline at end of file diff --git a/test/QRCodeEncoder.test.ts b/test/QRCodeEncoder.test.ts new file mode 100644 index 0000000..fb3de6f --- /dev/null +++ b/test/QRCodeEncoder.test.ts @@ -0,0 +1,55 @@ +import { setupOpenCv } from "./cv"; + +beforeAll(setupOpenCv); + +describe("QRCodeEncoder", () => { + it("should have correction level constants", () => { + expect(typeof cv.QRCodeEncoder_CORRECT_LEVEL_Q).toBe('number'); + expect(typeof cv.QRCodeEncoder_CORRECT_LEVEL_H).toBe('number'); + expect(typeof cv.QRCodeEncoder_CORRECT_LEVEL_L).toBe('number'); + expect(typeof cv.QRCodeEncoder_CORRECT_LEVEL_M).toBe('number'); + + // Verify the values are different + expect(cv.QRCodeEncoder_CORRECT_LEVEL_L).not.toBe(cv.QRCodeEncoder_CORRECT_LEVEL_M); + expect(cv.QRCodeEncoder_CORRECT_LEVEL_M).not.toBe(cv.QRCodeEncoder_CORRECT_LEVEL_Q); + expect(cv.QRCodeEncoder_CORRECT_LEVEL_Q).not.toBe(cv.QRCodeEncoder_CORRECT_LEVEL_H); + }); + + it("should have encode mode constants", () => { + expect(typeof cv.QRCodeEncoder_MODE_AUTO).toBe('number'); + expect(typeof cv.QRCodeEncoder_MODE_NUMERIC).toBe('number'); + expect(typeof cv.QRCodeEncoder_MODE_ALPHANUMERIC).toBe('number'); + expect(typeof cv.QRCodeEncoder_MODE_BYTE).toBe('number'); + expect(typeof cv.QRCodeEncoder_MODE_KANJI).toBe('number'); + expect(typeof cv.QRCodeEncoder_MODE_ECI).toBe('number'); + expect(typeof cv.QRCodeEncoder_MODE_STRUCTURED_APPEND).toBe('number'); + }); + + it("should have ECI encoding constants", () => { + expect(typeof cv.QRCodeEncoder_ECI_UTF8).toBe('number'); + }); + + it("should have enum classes available", () => { + expect(typeof cv.QRCodeEncoder_CorrectionLevel).toBe('function'); + expect(typeof cv.QRCodeEncoder_EncodeMode).toBe('function'); + expect(typeof cv.QRCodeEncoder_ECIEncodings).toBe('function'); + + // Note: These are enum-like functions, not traditional constructors + // They exist but may not be instantiable in the expected way + }); + + // Note: QRCodeEncoder class itself is not available in the current OpenCV.js build + it("should document that QRCodeEncoder class is not available", () => { + // This test documents the current limitation + expect(typeof cv.QRCodeEncoder).toBe('undefined'); + + // The constants exist but the class doesn't + expect(typeof cv.QRCodeEncoder_CORRECT_LEVEL_Q).toBe('number'); + }); + + it("should still have QRCodeDetector available for comparison", () => { + // QRCodeDetector should be available + expect(typeof cv.QRCodeDetector).toBe('function'); + expect(() => new cv.QRCodeDetector()).not.toThrow(); + }); +}); \ No newline at end of file