@@ -17,10 +17,6 @@ limitations under the License.
1717/**
1818 * Methods for decoding (reading) QR code patterns.
1919 * @module
20- * @example
21- ```js
22-
23- ```
2420 */
2521
2622import type { EncodingType , ErrorCorrection , Image , Mask , Point } from './index.ts' ;
@@ -53,6 +49,7 @@ for (let i = 0; i < SUM16.length; i++) {
5349const int = ( n : number ) => n >>> 0 ;
5450
5551type Point4 = [ Point , Point , Point , Point ] ;
52+ /** Finder and alignment points returned by the detector. */
5653export type FinderPoints = [ Pattern , Pattern , Point , Pattern ] ;
5754// distance ^ 2
5855const distance2 = ( p1 : Point , p2 : Point ) => {
@@ -1182,12 +1179,35 @@ function decodeBitmap(
11821179 return res ;
11831180}
11841181
1182+ /** QR decoding hooks and image preprocessing options. */
11851183export type DecodeOpts = {
1184+ /** Crop rectangular inputs to a centered square before decoding. */
11861185 cropToSquare ?: boolean ;
1186+ /**
1187+ * Custom byte-to-text decoder used for byte segments.
1188+ * @param bytes - Byte segment to decode.
1189+ * @returns Decoded text.
1190+ */
11871191 textDecoder ?: ( bytes : Uint8Array ) => string ;
1192+ /**
1193+ * Callback invoked with finder/alignment points after detection succeeds.
1194+ * @param points - Finder and alignment points returned by the detector.
1195+ */
11881196 pointsOnDetect ?: ( points : FinderPoints ) => void ;
1197+ /**
1198+ * Callback invoked with the grayscale bitmap that the detector sees.
1199+ * @param img - Grayscale image generated during bitmap conversion.
1200+ */
11891201 imageOnBitmap ?: ( img : Image ) => void ;
1202+ /**
1203+ * Callback invoked with the perspective-corrected QR image.
1204+ * @param img - Perspective-corrected QR image.
1205+ */
11901206 imageOnDetect ?: ( img : Image ) => void ;
1207+ /**
1208+ * Callback invoked with the final decoded QR image.
1209+ * @param img - Final QR image used to decode the payload.
1210+ */
11911211 imageOnResult ?: ( img : Image ) => void ;
11921212} ;
11931213
@@ -1211,6 +1231,22 @@ function cropToSquare(img: Image) {
12111231 return { offset, img : { height : squareSize , width : squareSize , data : croppedData } } ;
12121232}
12131233
1234+ /**
1235+ * Decode text from a QR image.
1236+ * @param img - RGB or RGBA image data that contains a QR code.
1237+ * @param opts - Decoder hooks and image preprocessing options. See {@link DecodeOpts}.
1238+ * @returns Decoded QR payload as a string.
1239+ * @throws If the image, decoder options, or QR contents are invalid. {@link Error}
1240+ * @example
1241+ * Decode text from a QR image.
1242+ * ```ts
1243+ * import encodeQR, { Bitmap } from 'qr';
1244+ * import decodeQR from 'qr/decode.js';
1245+ * const bits = encodeQR('Hello world', 'raw', { scale: 4 });
1246+ * const bm = new Bitmap({ width: bits[0].length, height: bits.length }, bits);
1247+ * const text = decodeQR(bm.toImage());
1248+ * ```
1249+ */
12141250export function decodeQR ( img : Image , opts : DecodeOpts = { } ) : string {
12151251 for ( const field of [ 'height' , 'width' ] as const ) {
12161252 if ( ! Number . isSafeInteger ( img [ field ] ) || img [ field ] <= 0 )
@@ -1240,6 +1276,22 @@ export function decodeQR(img: Image, opts: DecodeOpts = {}): string {
12401276 return res ;
12411277}
12421278
1279+ /**
1280+ * Default export alias for {@link decodeQR}.
1281+ * @param img - RGB or RGBA image data that contains a QR code.
1282+ * @param opts - Decoder hooks and image preprocessing options. See {@link DecodeOpts}.
1283+ * @returns Decoded QR payload as a string.
1284+ * @throws If the image, decoder options, or QR contents are invalid. {@link Error}
1285+ * @example
1286+ * Decode a rendered QR image via the default decoder export.
1287+ * ```ts
1288+ * import encodeQR, { Bitmap } from 'qr';
1289+ * import decodeQR from 'qr/decode.js';
1290+ * const bits = encodeQR('Hello world', 'raw', { scale: 4 });
1291+ * const bm = new Bitmap({ width: bits[0].length, height: bits.length }, bits);
1292+ * decodeQR(bm.toImage());
1293+ * ```
1294+ */
12431295export default decodeQR ;
12441296
12451297// Unsafe API utils, exported only for tests
0 commit comments