Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to retrieve the colors used in the QR code from the image. #76

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ If you want to use jsQR to scan a webcam stream you'll need to extract the [`Ima

## Usage

jsQR exports a method that takes in 3 arguments representing the image data you wish to decode.
jsQR exports a method that takes in 3 arguments representing the image data you wish to decode. Additionally it can take an options object to further configure scanning behavior.

```javascript
const code = jsQR(imageData, width, height);
const code = jsQR(imageData, width, height, options?);

if (code) {
console.log("Found QR code", code);
Expand All @@ -61,7 +61,10 @@ if (code) {
As such the length of this array should be `4 * width * height`.
This data is in the same form as the [`ImageData`](https://developer.mozilla.org/en-US/docs/Web/API/ImageData) interface, and it's also [commonly](https://www.npmjs.com/package/jpeg-js#decoding-jpegs) [returned](https://github.com/lukeapage/pngjs/blob/master/README.md#property-data) by node modules for reading images.
- `width` - The width of the image you wish to decode.
- `height` The height of the image you wish to decode.
- `height` - The height of the image you wish to decode.
- `options` (optional) - Additional options.
- `attemptInverted` - (default: `true`) - Should jsQR attempt to invert the image to find QR codes with white modules on black backgrounds instead of the black modules on white background. This option defaults to true for backwards compatibility but causes a ~50% performance hit, and will probably be disabled in future versions.
- `retrieveColors` - (default: `false`) - Should jsQR include the colors used in the QR code in its output. This can slow down scanning as every pixel of the QR code is sampled and converted into the CIELab* color space for averaging.

### Return value
If a QR is able to be decoded the library will return an object with the following keys.
Expand All @@ -73,6 +76,7 @@ Has points for the following locations.
- Corners - `topRightCorner`/`topLeftCorner`/`bottomRightCorner`/`bottomLeftCorner`;
- Finder patterns - `topRightFinderPattern`/`topLeftFinderPattern`/`bottomLeftFinderPattern`
- May also have a point for the `bottomRightAlignmentPattern` assuming one exists and can be located.
- `colors` (not included by default, see the `options` argument) - An object with `qr` and `background` keys containing the Uint8ClampedArray RGBA representations of the average colors used in the QR code (e.g. `[0, 0, 0, 255]` and `[255, 255, 255, 255]` respectively).

Because the library is written in [typescript](http://www.typescriptlang.org/) you can also view the [type definitions](./dist/index.d.ts) to understand the API.

Expand Down
11 changes: 11 additions & 0 deletions dist/color-retriever/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BitMatrix } from "../BitMatrix";
import { QRLocation } from "../locator";
import { Point } from "../Point";
export interface QRColors {
qr: Uint8ClampedArray;
background: Uint8ClampedArray;
}
export declare function retrieveColors(location: QRLocation, extracted: {
matrix: BitMatrix;
mappingFunction: (x: number, y: number) => Point;
}, sourceData: Uint8ClampedArray, sourceWidth: number): QRColors;
2 changes: 1 addition & 1 deletion dist/decoder/decodeData/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export declare enum Mode {
Alphanumeric = "alphanumeric",
Byte = "byte",
Kanji = "kanji",
ECI = "eci",
ECI = "eci"
}
export declare function decode(data: Uint8ClampedArray, version: number): DecodedQR;
8 changes: 7 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { QRColors } from "./color-retriever";
import { Chunks } from "./decoder/decodeData";
import { Point } from "./locator";
export interface QRCode {
Expand All @@ -14,6 +15,11 @@ export interface QRCode {
bottomLeftFinderPattern: Point;
bottomRightAlignmentPattern?: Point;
};
colors?: QRColors;
}
declare function jsQR(data: Uint8ClampedArray, width: number, height: number): QRCode | null;
export interface Options {
attemptInverted?: boolean;
retrieveColors?: boolean;
}
declare function jsQR(data: Uint8ClampedArray, width: number, height: number, options?: Options): QRCode | null;
export default jsQR;
Loading