Implement QR code generation and reading

This commit is contained in:
Mikko Ahlroth 2019-06-10 20:53:26 +03:00
parent d346e6bc97
commit 4996a2563e
83 changed files with 12368 additions and 7 deletions

View file

@ -15,7 +15,11 @@
"webpack-dev-server": "^3.4.1"
},
"dependencies": {
"lit-element": "^2.1.0"
"@types/qrcode": "^1.3.3",
"@types/text-encoding": "^0.0.35",
"lit-element": "^2.1.0",
"qrcode": "^1.3.3",
"text-encoding": "^0.7.0"
},
"scripts": {
"build": "webpack --mode development",

View file

@ -1,4 +1,6 @@
import { LitElement, html } from 'lit-element'
import './budget-qr-reader'
import { QRReadEvent } from './budget-qr-reader';
export class JoinBudgetEvent extends CustomEvent<{ uuid: string }> { }
@ -7,11 +9,13 @@ export class CreateBudgetEvent extends CustomEvent<{ amount: number, currency: s
class BudgetAddComponent extends LitElement {
hasCreateError: boolean
hasJoinError: boolean
showQRReader: boolean
static get properties() {
return {
hasCreateError: { type: Boolean },
hasJoinError: { type: Boolean }
hasJoinError: { type: Boolean },
showQRReader: { type: Boolean }
}
}
@ -20,11 +24,12 @@ class BudgetAddComponent extends LitElement {
this.hasCreateError = false
this.hasJoinError = false
this.showQRReader = false
}
onCreate(e: Event) {
e.preventDefault()
const root = <ShadowRoot>this.shadowRoot
const root = this.shadowRoot
const amountInputEl = <HTMLInputElement>root.getElementById('create-budget-amount')
const currencyInputEl = <HTMLInputElement>root.getElementById('create-budget-currency')
@ -45,8 +50,7 @@ class BudgetAddComponent extends LitElement {
onJoin(e: Event) {
e.preventDefault()
const root = <ShadowRoot>this.shadowRoot
const inputEl = <HTMLInputElement>root.getElementById('join-budget-uuid')
const inputEl = <HTMLInputElement>this.shadowRoot.getElementById('join-budget-uuid')
this.dispatchEvent(new JoinBudgetEvent(
'joinBudget',
@ -56,6 +60,19 @@ class BudgetAddComponent extends LitElement {
inputEl.value = ''
}
onQRRead(e: QRReadEvent) {
const text = e.detail.text
this.dispatchEvent(new JoinBudgetEvent(
'joinBudget',
{ detail: { uuid: text } }
))
this.showQRReader = false
}
toggleShowQRReader() {
this.showQRReader = !this.showQRReader
}
render() {
return html`
<h2>Create a budget</h2>
@ -78,6 +95,14 @@ class BudgetAddComponent extends LitElement {
required
/>
<button type="submit">Join budget</button>
<p>or read QR using phone's camera:</p>
<button type="button" @click=${this.toggleShowQRReader}>Read QR</button>
<div>
${this.showQRReader ? html`<budget-qr-reader @qrRead=${this.onQRRead}></budget-qr-reader>` : null}
</div>
${this.hasJoinError ? html`<p>Could not add given budget, please check it is correctly typed.</p>` : null}
</form>
`

View file

@ -1,5 +1,6 @@
import { LitElement, html } from 'lit-element'
import { Budget } from '../services/models/budget'
import './budget-qr'
export class SpendMoneyEvent extends CustomEvent<{ budget: Budget, amount: number }> { }
@ -9,10 +10,12 @@ export class RemoveBudgetEvent extends CustomEvent<{ budget: Budget }> { }
class BudgetDisplayComponent extends LitElement {
budget: Budget
showShare: boolean
static get properties() {
return {
budget: { type: Budget }
budget: { type: Budget },
showShare: { type: Boolean }
}
}
@ -20,6 +23,7 @@ class BudgetDisplayComponent extends LitElement {
super()
this.budget = null
this.showShare = false
}
onSpendMoney() {
@ -56,6 +60,10 @@ class BudgetDisplayComponent extends LitElement {
))
}
toggleShare() {
this.showShare = !this.showShare
}
render() {
if (!this.budget) {
return html``
@ -69,7 +77,12 @@ class BudgetDisplayComponent extends LitElement {
</ul>
<button type="button" @click=${this.onSpendMoney}>Spend money</button>
<button type="button" @click=${this.onReset}>Reset</button>
<button type="button" @click=${this.toggleShare}>Share</button>
<button type="button" @click=${this.onRemove}>Remove</button>
<div>
${this.showShare ? html`<budget-qr .uuid=${this.budget.uuid}></budget-qr>` : null}
</div>
`
}
}

View file

@ -0,0 +1,83 @@
import { LitElement, html } from 'lit-element'
import { BrowserQRCodeReader, VideoInputDevice } from '../vendor/zxing-typescript/src/browser/BrowserQRCodeReader'
export class QRReadEvent extends CustomEvent<{ text: string }> { }
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
class BudgetQRReaderComponent extends LitElement {
reader: BrowserQRCodeReader
inputs: VideoInputDevice[]
currentInput: string
test: string
static get properties() {
return {
inputs: { type: Array },
currentInput: { type: String },
test: { type: String }
}
}
constructor() {
super()
this.reader = new BrowserQRCodeReader()
this.inputs = []
this.currentInput = null
}
async firstUpdated() {
this.inputs = await this.reader.getVideoInputDevices()
if (this.inputs.length > 0) {
this.currentInput = this.inputs[0].deviceId
this.startRead()
}
}
renderVideoInput(input: VideoInputDevice) {
return html`
<option
value=${input.deviceId}
?selected=${this.currentInput === input.deviceId}
>${input.label}</option>
`
}
async startRead() {
const root = this.shadowRoot
const videoEl = <HTMLVideoElement>root.getElementById('video')
let found = false
do {
const result = await this.reader.decodeFromInputVideoDevice(this.currentInput, videoEl)
const text = result.getText()
if (this._isUUID(text)) {
this.dispatchEvent(new QRReadEvent(
'qrRead',
{ detail: { text } }
))
found = true
}
} while (!found)
}
stopRead() {
this.reader.reset()
}
render() {
return html`
<video id="video" width="300" height="200" style="border: 1px solid gray"></video>
<select id="qr-reader-input">
${this.inputs.map(input => this.renderVideoInput(input))}
</select>
`
}
_isUUID(text: string) {
return UUID_RE.test(text)
}
}
customElements.define('budget-qr-reader', BudgetQRReaderComponent)

View file

@ -0,0 +1,40 @@
import { LitElement, html } from 'lit-element'
import * as QRCode from 'qrcode'
class BudgetQRComponent extends LitElement {
uuid: string
error: boolean
static get properties() {
return {
uuid: { type: String },
error: { type: Boolean }
}
}
constructor() {
super()
this.uuid = ''
this.error = false
}
firstUpdated() {
const root = <ShadowRoot>this.shadowRoot
const canvas = root.getElementById(`${this.uuid}-qr-canvas`)
QRCode.toCanvas(canvas, this.uuid, err => console.log(err))
}
render() {
if (this.error) {
return html`<p>There was an error rendering the QR code.</p>`
}
return html`
<canvas id="${this.uuid}-qr-canvas"></canvas>
`
}
}
customElements.define('budget-qr', BudgetQRComponent)

View file

@ -7,7 +7,6 @@ import { readBudget, readEvent } from '../services/readers'
import { getBudget, GetError, addEvent, resetBudget, createBudget, CreationError } from '../services/api'
import { getFromLocal, storeToLocal } from '../services/storage'
import { SpendMoneyEvent, ResetBudgetEvent, RemoveBudgetEvent } from './budget-display'
import { ETIME } from 'constants';
class WeekBudgetComponent extends LitElement {
budgets: Budget[] = []

View file

@ -0,0 +1,327 @@
import Reader from "./../core/Reader"
import BinaryBitmap from './../core/BinaryBitmap'
import HybridBinarizer from './../core/common/HybridBinarizer'
import Result from './../core/Result'
import Exception from './../core/Exception'
import HTMLCanvasElementLuminanceSource from './HTMLCanvasElementLuminanceSource'
import VideoInputDevice from './VideoInputDevice'
/**
* Base class for browser code reader.
*
* @export
* @class BrowserCodeReader
*/
export default class BrowserCodeReader {
private videoElement: HTMLVideoElement
private imageElement: HTMLImageElement
private canvasElement: HTMLCanvasElement
private canvasElementContext: CanvasRenderingContext2D
private timeoutHandler: number
private stream: MediaStream
private videoPlayEndedEventListener: EventListener
private videoPlayingEventListener: EventListener
private imageLoadedEventListener: EventListener
/**
* Creates an instance of BrowserCodeReader.
* @param {Reader} reader The reader instance to decode the barcode
* @param {number} [timeBetweenScansMillis=500] the time delay between subsequent decode tries
*
* @memberOf BrowserCodeReader
*/
public constructor(private reader: Reader, private timeBetweenScansMillis: number = 500) {}
/**
* Obtain the list of available devices with type 'videoinput'.
*
* @returns {Promise<VideoInputDevice[]>} an array of available video input devices
*
* @memberOf BrowserCodeReader
*/
public getVideoInputDevices(): Promise<VideoInputDevice[]> {
return new Promise<VideoInputDevice[]>((resolve, reject) => {
navigator.mediaDevices.enumerateDevices()
.then((devices: MediaDeviceInfo[]) => {
const sources = new Array<VideoInputDevice>()
let c = 0
for(let i = 0, length = devices.length; i != length; i++) {
const device = devices[i]
if (device.kind === 'videoinput') {
sources.push(new VideoInputDevice(device.deviceId, device.label || `Video source ${c}`))
c++
}
}
resolve(sources)
})
.catch((err: any) => {
reject(err)
})
})
}
/**
* Decodes the barcode from the device specified by deviceId while showing the video in the specified video element.
*
* @param {string} [deviceId] the id of one of the devices obtained after calling getVideoInputDevices. Can be undefined, in this case it will decode from one of the available devices, preffering the main camera (environment facing) if available.
* @param {(string|HTMLVideoElement)} [videoElement] the video element in page where to show the video while decoding. Can be either an element id or directly an HTMLVideoElement. Can be undefined, in which case no video will be shown.
* @returns {Promise<Result>} The decoding result.
*
* @memberOf BrowserCodeReader
*/
public decodeFromInputVideoDevice(deviceId?: string, videoElement?: string|HTMLVideoElement): Promise<Result> {
this.reset()
this.prepareVideoElement(videoElement)
let constraints: MediaStreamConstraints
if (undefined === deviceId) {
constraints = {
video: { facingMode: "environment" }
}
} else {
constraints = {
video: { deviceId }
}
}
const me = this
return new Promise<Result>((resolve, reject) => {
navigator.mediaDevices.getUserMedia(constraints)
.then((stream: MediaStream) => {
me.stream = stream
me.videoElement.srcObject = stream
me.videoPlayingEventListener = () => {
me.decodeOnceWithDelay(resolve, reject)
}
me.videoElement.addEventListener('playing', me.videoPlayingEventListener)
me.videoElement.play()
})
.catch((error) => {
reject(error)
})
})
}
/**
* Decodes a barcode form a video url.
*
* @param {string} videoUrl The video url to decode from, required.
* @param {(string|HTMLVideoElement)} [videoElement] The video element where to play the video while decoding. Can be undefined in which case no video is shown.
* @returns {Promise<Result>} The decoding result.
*
* @memberOf BrowserCodeReader
*/
public decodeFromVideoSource(videoUrl: string, videoElement?: string|HTMLVideoElement): Promise<Result> {
this.reset()
this.prepareVideoElement(videoElement)
const me = this
return new Promise<Result>((resolve, reject) => {
me.videoPlayEndedEventListener = () => {
me.stop()
reject(new Exception(Exception.NotFoundException))
}
me.videoElement.addEventListener('ended', me.videoPlayEndedEventListener)
me.videoPlayingEventListener = () => {
me.decodeOnceWithDelay(resolve, reject)
}
me.videoElement.addEventListener('playing', me.videoPlayingEventListener)
me.videoElement.setAttribute('autoplay', 'true')
me.videoElement.setAttribute('src', videoUrl)
})
}
private prepareVideoElement(videoElement?: string|HTMLVideoElement) {
if (undefined === videoElement) {
this.videoElement = document.createElement('video')
this.videoElement.width = 200
this.videoElement.height = 200
} else if (typeof videoElement === 'string') {
this.videoElement = <HTMLVideoElement>this.getMediaElement(videoElement, 'video')
} else {
this.videoElement = videoElement
}
}
private getMediaElement(mediaElementId: string, type: string) {
const mediaElement = document.getElementById(mediaElementId)
if (null === mediaElement) {
throw new Exception(Exception.ArgumentException, `element with id '${mediaElementId}' not found`)
}
if (mediaElement.nodeName.toLowerCase() !== type.toLowerCase()) {
console.log(mediaElement.nodeName)
throw new Exception(Exception.ArgumentException, `element with id '${mediaElementId}' must be an ${type} element`)
}
return mediaElement
}
/**
* Decodes the barcode from an image.
*
* @param {(string|HTMLImageElement)} [imageElement] The image element that can be either an element id or the element itself. Can be undefined in which case the decoding will be done from the imageUrl parameter.
* @param {string} [imageUrl]
* @returns {Promise<Result>} The decoding result.
*
* @memberOf BrowserCodeReader
*/
public decodeFromImage(imageElement?: string|HTMLImageElement, imageUrl?: string): Promise<Result> {
this.reset()
if (undefined === imageElement && undefined === imageUrl) {
throw new Exception(Exception.ArgumentException, 'either imageElement with a src set or an url must be provided')
}
this.prepareImageElement(imageElement)
const me = this
return new Promise<Result>((resolve, reject) => {
if (undefined !== imageUrl) {
me.imageLoadedEventListener = () => {
me.decodeOnce(resolve, reject, false, true)
}
me.imageElement.addEventListener('load', me.imageLoadedEventListener)
me.imageElement.src = imageUrl
} else if (this.isImageLoaded(this.imageElement)) {
me.decodeOnce(resolve, reject, false, true)
} else {
throw new Exception(Exception.ArgumentException, `either src or a loaded img should be provided`)
}
})
}
private isImageLoaded(img: HTMLImageElement) {
// During the onload event, IE correctly identifies any images that
// werent downloaded as not complete. Others should too. Gecko-based
// browsers act like NS4 in that they report this incorrectly.
if (!img.complete) {
return false
}
// However, they do have two very useful properties: naturalWidth and
// naturalHeight. These give the true size of the image. If it failed
// to load, either of these should be zero.
if (img.naturalWidth === 0) {
return false
}
// No other way of checking: assume its ok.
return true
}
private prepareImageElement(imageElement?: string|HTMLImageElement) {
if (undefined === imageElement) {
this.imageElement = document.createElement('img')
this.imageElement.width = 200
this.imageElement.height = 200
} else if (typeof imageElement === 'string') {
this.imageElement = <HTMLImageElement>this.getMediaElement(imageElement, 'img')
} else {
this.imageElement = imageElement
}
}
private decodeOnceWithDelay(resolve: (result: Result) => any, reject: (error: any) => any): void {
this.timeoutHandler = window.setTimeout(this.decodeOnce.bind(this, resolve, reject), this.timeBetweenScansMillis)
}
private decodeOnce(resolve: (result: Result) => any, reject: (error: any) => any, retryIfNotFound: boolean = true, retryIfChecksumOrFormatError: boolean = true): void {
if (undefined === this.canvasElementContext) {
this.prepareCaptureCanvas()
}
this.canvasElementContext.drawImage(this.videoElement||this.imageElement, 0, 0)
const luminanceSource = new HTMLCanvasElementLuminanceSource(this.canvasElement)
const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource))
try {
const result = this.readerDecode(binaryBitmap)
resolve(result)
} catch(re) {
console.log(retryIfChecksumOrFormatError, re)
if (retryIfNotFound && Exception.isOfType(re, Exception.NotFoundException)) {
console.log('not found, trying again...')
this.decodeOnceWithDelay(resolve, reject)
} else if (retryIfChecksumOrFormatError && ( Exception.isOfType(re, Exception.ChecksumException) || Exception.isOfType(re, Exception.FormatException) ) ) {
console.log('checksum or format error, trying again...', re)
this.decodeOnceWithDelay(resolve, reject)
} else {
reject(re)
}
}
}
protected readerDecode(binaryBitmap: BinaryBitmap): Result {
return this.reader.decode(binaryBitmap)
}
private prepareCaptureCanvas() {
const canvasElement = document.createElement('canvas')
let width, height
if (undefined !== this.videoElement) {
width = this.videoElement.videoWidth
height = this.videoElement.videoHeight
} else {
width = this.imageElement.naturalWidth || this.imageElement.width
height = this.imageElement.naturalHeight || this.imageElement.height
}
canvasElement.style.width = `${width}px`
canvasElement.style.height = `${height}px`
canvasElement.width = width
canvasElement.height = height
this.canvasElement = canvasElement
this.canvasElementContext = canvasElement.getContext('2d')
//this.videoElement.parentElement.appendChild(this.canvasElement)
}
private stop() {
if (undefined !== this.timeoutHandler) {
window.clearTimeout(this.timeoutHandler)
this.timeoutHandler = undefined
}
if (undefined !== this.stream) {
this.stream.getTracks()[0].stop()
this.stream = undefined
}
}
/**
* Resets the code reader to the initial state. Cancels any ongoing barcode scanning from video or camera.
*
* @memberOf BrowserCodeReader
*/
public reset() {
this.stop()
if (undefined !== this.videoPlayEndedEventListener && undefined !== this.videoElement) {
this.videoElement.removeEventListener('ended', this.videoPlayEndedEventListener)
}
if (undefined !== this.videoPlayingEventListener && undefined !== this.videoElement) {
this.videoElement.removeEventListener('playing', this.videoPlayingEventListener)
}
if (undefined !== this.videoElement) {
this.videoElement.srcObject = undefined
this.videoElement.removeAttribute('src')
this.videoElement = undefined
}
if (undefined !== this.videoPlayEndedEventListener && undefined !== this.imageElement) {
this.imageElement.removeEventListener('load', this.imageLoadedEventListener)
}
if (undefined !== this.imageElement) {
this.imageElement.src = undefined
this.imageElement.removeAttribute('src')
this.imageElement = undefined
}
this.canvasElementContext = undefined
this.canvasElement = undefined
}
}

View file

@ -0,0 +1,23 @@
import QRCodeReader from './../core/qrcode/QRCodeReader'
import VideoInputDevice from './VideoInputDevice'
import BrowserCodeReader from './BrowserCodeReader'
/**
* QR Code reader to use from browser.
*
* @class BrowserQRCodeReader
* @extends {BrowserCodeReader}
*/
class BrowserQRCodeReader extends BrowserCodeReader {
/**
* Creates an instance of BrowserQRCodeReader.
* @param {number} [timeBetweenScansMillis=500] the time delay between subsequent decode tries
*
* @memberOf BrowserQRCodeReader
*/
public constructor(timeBetweenScansMillis: number = 500) {
super(new QRCodeReader(), timeBetweenScansMillis)
}
}
export { VideoInputDevice, BrowserQRCodeReader }

View file

@ -0,0 +1,124 @@
import InvertedLuminanceSource from './../core/InvertedLuminanceSource'
import LuminanceSource from './../core/LuminanceSource'
import Exception from './../core/Exception'
export default class HTMLCanvasElementLuminanceSource extends LuminanceSource {
private buffer: Uint8ClampedArray
private static DEGREE_TO_RADIANS = Math.PI / 180
private tempCanvasElement: HTMLCanvasElement = null
public constructor(private canvas: HTMLCanvasElement) {
super(canvas.width, canvas.height)
this.buffer = HTMLCanvasElementLuminanceSource.makeBufferFromCanvasImageData(canvas)
}
private static makeBufferFromCanvasImageData(canvas: HTMLCanvasElement): Uint8ClampedArray {
const imageData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height)
return HTMLCanvasElementLuminanceSource.toGrayscaleBuffer(imageData.data, canvas.width, canvas.height)
}
private static toGrayscaleBuffer(imageBuffer: Uint8ClampedArray, width: number, height: number): Uint8ClampedArray {
const grayscaleBuffer = new Uint8ClampedArray(width * height)
for(let i = 0, j = 0, length = imageBuffer.length; i < length; i += 4, j++) {
let gray
const alpha = imageBuffer[i + 4]
// The color of fully-transparent pixels is irrelevant. They are often, technically, fully-transparent
// black (0 alpha, and then 0 RGB). They are often used, of course as the "white" area in a
// barcode image. Force any such pixel to be white:
if (alpha === 0) {
gray = 0xFF
} else {
const pixelR = imageBuffer[i]
const pixelG = imageBuffer[i+1]
const pixelB = imageBuffer[i+2]
// .299R + 0.587G + 0.114B (YUV/YIQ for PAL and NTSC),
// (306*R) >> 10 is approximately equal to R*0.299, and so on.
// 0x200 >> 10 is 0.5, it implements rounding.
gray = (306 * pixelR +
601 * pixelG +
117 * pixelB +
0x200) >> 10
}
grayscaleBuffer[j] = gray
}
return grayscaleBuffer
}
public getRow(y: number/*int*/, row: Uint8ClampedArray): Uint8ClampedArray {
if (y < 0 || y >= this.getHeight()) {
throw new Exception(Exception.IllegalArgumentException, "Requested row is outside the image: " + y)
}
const width: number/*int*/ = this.getWidth()
const start = y * width
if (row === null ) {
row = this.buffer.slice(start, start + width)
} else {
if (row.length < width) {
row = new Uint8ClampedArray(width)
}
// The underlying raster of image consists of bytes with the luminance values
// TODO: can avoid set/slice?
row.set(this.buffer.slice(start, start + width))
}
return row
}
public getMatrix(): Uint8ClampedArray {
return this.buffer
}
public isCropSupported(): boolean {
return true
}
public crop(left: number/*int*/, top: number/*int*/, width: number/*int*/, height: number/*int*/): LuminanceSource {
this.crop(left, top, width, height)
return this
}
/**
* This is always true, since the image is a gray-scale image.
*
* @return true
*/
public isRotateSupported(): boolean {
return true
}
public rotateCounterClockwise(): LuminanceSource {
this.rotate(-90)
return this
}
public rotateCounterClockwise45(): LuminanceSource {
this.rotate(-45)
return this
}
private getTempCanvasElement() {
if (null === this.tempCanvasElement) {
const tempCanvasElement = this.canvas.ownerDocument.createElement('canvas')
tempCanvasElement.style.width = `${this.canvas.width}px`
tempCanvasElement.style.height = `${this.canvas.height}px`
}
return this.tempCanvasElement
}
private rotate(angle: number) {
const tempCanvasElement = this.getTempCanvasElement()
const tempContext = tempCanvasElement.getContext('2d')
tempContext.rotate(angle * HTMLCanvasElementLuminanceSource.DEGREE_TO_RADIANS)
tempContext.drawImage(this.canvas, 0, 0)
this.buffer = HTMLCanvasElementLuminanceSource.makeBufferFromCanvasImageData(tempCanvasElement)
return this
}
public invert(): LuminanceSource {
return new InvertedLuminanceSource(this)
}
}

View file

@ -0,0 +1 @@
import QRCodeWriter from './../core/qrcode/QRCodeWriter'

View file

@ -0,0 +1,17 @@
/**
* Video input device metadata containing the id and label of the device if available.
*
* @export
* @class VideoInputDevice
*/
export default class VideoInputDevice {
/**
* Creates an instance of VideoInputDevice.
* @param {string} deviceId the video input device id
* @param {string} label the label of the device if available
*
* @memberOf VideoInputDevice
*/
public constructor(public deviceId: string, public label: string) {}
}

View file

@ -0,0 +1,82 @@
/*
* Direct port to TypeScript of ZXing by Adrian Toșcă
*/
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
/**
* Enumerates barcode formats known to this package. Please keep alphabetized.
*
* @author Sean Owen
*/
const enum BarcodeFormat {
/** Aztec 2D barcode format. */
AZTEC,
/** CODABAR 1D format. */
CODABAR,
/** Code 39 1D format. */
CODE_39,
/** Code 93 1D format. */
CODE_93,
/** Code 128 1D format. */
CODE_128,
/** Data Matrix 2D barcode format. */
DATA_MATRIX,
/** EAN-8 1D format. */
EAN_8,
/** EAN-13 1D format. */
EAN_13,
/** ITF (Interleaved Two of Five) 1D format. */
ITF,
/** MaxiCode 2D barcode format. */
MAXICODE,
/** PDF417 format. */
PDF_417,
/** QR Code 2D barcode format. */
QR_CODE,
/** RSS 14 */
RSS_14,
/** RSS EXPANDED */
RSS_EXPANDED,
/** UPC-A 1D format. */
UPC_A,
/** UPC-E 1D format. */
UPC_E,
/** UPC/EAN extension format. Not a stand-alone format. */
UPC_EAN_EXTENSION
}
export default BarcodeFormat

View file

@ -0,0 +1,85 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
import LuminanceSource from './LuminanceSource'
import BitArray from './common/BitArray'
import BitMatrix from './common/BitMatrix'
/**
* This class hierarchy provides a set of methods to convert luminance data to 1 bit data.
* It allows the algorithm to vary polymorphically, for example allowing a very expensive
* thresholding technique for servers and a fast one for mobile. It also permits the implementation
* to vary, e.g. a JNI version for Android and a Java fallback version for other platforms.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
abstract class Binarizer {
protected constructor(private source: LuminanceSource) {}
public getLuminanceSource(): LuminanceSource {
return this.source
}
/**
* Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
* cached data. Callers should assume this method is expensive and call it as seldom as possible.
* This method is intended for decoding 1D barcodes and may choose to apply sharpening.
* For callers which only examine one row of pixels at a time, the same BitArray should be reused
* and passed in with each call for performance. However it is legal to keep more than one row
* at a time if needed.
*
* @param y The row to fetch, which must be in [0, bitmap height)
* @param row An optional preallocated array. If null or too small, it will be ignored.
* If used, the Binarizer will call BitArray.clear(). Always use the returned object.
* @return The array of bits for this row (true means black).
* @throws NotFoundException if row can't be binarized
*/
public abstract getBlackRow(y: number/*iny*/, row: BitArray): BitArray /*throws NotFoundException*/
/**
* Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive
* and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
* may not apply sharpening. Therefore, a row from this matrix may not be identical to one
* fetched using getBlackRow(), so don't mix and match between them.
*
* @return The 2D array of bits for the image (true means black).
* @throws NotFoundException if image can't be binarized to make a matrix
*/
public abstract getBlackMatrix(): BitMatrix /*throws NotFoundException*/
/**
* Creates a new object with the same type as this Binarizer implementation, but with pristine
* state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache
* of 1 bit data. See Effective Java for why we can't use Java's clone() method.
*
* @param source The LuminanceSource this Binarizer will operate on.
* @return A new concrete Binarizer implementation object.
*/
public abstract createBinarizer(source: LuminanceSource): Binarizer
public getWidth(): number/*int*/ {
return this.source.getWidth()
}
public getHeight(): number/*int*/ {
return this.source.getHeight()
}
}
export default Binarizer

View file

@ -0,0 +1,151 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects
* accept a BinaryBitmap and attempt to decode it.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
/*namespace com.google.zxing {*/
import Exception from './Exception'
import Binarizer from './Binarizer'
import BitArray from './common/BitArray'
import BitMatrix from './common/BitMatrix'
import LuminanceSource from './LuminanceSource'
export default class BinaryBitmap {
private matrix: BitMatrix
public constructor(private binarizer: Binarizer) {
if (binarizer === null) {
throw new Exception(Exception.IllegalArgumentException, "Binarizer must be non-null.")
}
}
/**
* @return The width of the bitmap.
*/
public getWidth(): number/*int*/ {
return this.binarizer.getWidth()
}
/**
* @return The height of the bitmap.
*/
public getHeight(): number/*int*/ {
return this.binarizer.getHeight()
}
/**
* Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
* cached data. Callers should assume this method is expensive and call it as seldom as possible.
* This method is intended for decoding 1D barcodes and may choose to apply sharpening.
*
* @param y The row to fetch, which must be in [0, bitmap height)
* @param row An optional preallocated array. If null or too small, it will be ignored.
* If used, the Binarizer will call BitArray.clear(). Always use the returned object.
* @return The array of bits for this row (true means black).
* @throws NotFoundException if row can't be binarized
*/
public getBlackRow(y: number/*int*/, row: BitArray): BitArray /*throws NotFoundException */ {
return this.binarizer.getBlackRow(y, row)
}
/**
* Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
* and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
* may not apply sharpening. Therefore, a row from this matrix may not be identical to one
* fetched using getBlackRow(), so don't mix and match between them.
*
* @return The 2D array of bits for the image (true means black).
* @throws NotFoundException if image can't be binarized to make a matrix
*/
public getBlackMatrix(): BitMatrix /*throws NotFoundException*/ {
// The matrix is created on demand the first time it is requested, then cached. There are two
// reasons for this:
// 1. This work will never be done if the caller only installs 1D Reader objects, or if a
// 1D Reader finds a barcode before the 2D Readers run.
// 2. This work will only be done once even if the caller installs multiple 2D Readers.
if (this.matrix === null || this.matrix === undefined) {
this.matrix = this.binarizer.getBlackMatrix()
}
return this.matrix
}
/**
* @return Whether this bitmap can be cropped.
*/
public isCropSupported(): boolean {
return this.binarizer.getLuminanceSource().isCropSupported()
}
/**
* Returns a new object with cropped image data. Implementations may keep a reference to the
* original data rather than a copy. Only callable if isCropSupported() is true.
*
* @param left The left coordinate, which must be in [0,getWidth())
* @param top The top coordinate, which must be in [0,getHeight())
* @param width The width of the rectangle to crop.
* @param height The height of the rectangle to crop.
* @return A cropped version of this object.
*/
public crop(left: number/*int*/, top: number/*int*/, width: number/*int*/, height: number/*int*/): BinaryBitmap {
const newSource: LuminanceSource = this.binarizer.getLuminanceSource().crop(left, top, width, height)
return new BinaryBitmap(this.binarizer.createBinarizer(newSource))
}
/**
* @return Whether this bitmap supports counter-clockwise rotation.
*/
public isRotateSupported(): boolean {
return this.binarizer.getLuminanceSource().isRotateSupported()
}
/**
* Returns a new object with rotated image data by 90 degrees counterclockwise.
* Only callable if {@link #isRotateSupported()} is true.
*
* @return A rotated version of this object.
*/
public rotateCounterClockwise(): BinaryBitmap {
const newSource: LuminanceSource = this.binarizer.getLuminanceSource().rotateCounterClockwise()
return new BinaryBitmap(this.binarizer.createBinarizer(newSource))
}
/**
* Returns a new object with rotated image data by 45 degrees counterclockwise.
* Only callable if {@link #isRotateSupported()} is true.
*
* @return A rotated version of this object.
*/
public rotateCounterClockwise45(): BinaryBitmap {
const newSource: LuminanceSource = this.binarizer.getLuminanceSource().rotateCounterClockwise45()
return new BinaryBitmap(this.binarizer.createBinarizer(newSource))
}
/*@Override*/
public toString(): string {
try {
return this.getBlackMatrix().toString()
} catch (e /*: NotFoundException*/) {
return ""
}
}
}

View file

@ -0,0 +1,122 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
/**
* Encapsulates a type of hint that a caller may pass to a barcode reader to help it
* more quickly or accurately decode it. It is up to implementations to decide what,
* if anything, to do with the information that is supplied.
*
* @author Sean Owen
* @author dswitkin@google.com (Daniel Switkin)
* @see Reader#decode(BinaryBitmap,java.util.Map)
*/
const enum DecodeHintType {
/**
* Unspecified, application-specific hint. Maps to an unspecified {@link Object}.
*/
OTHER/*(Object.class)*/,
/**
* Image is a pure monochrome image of a barcode. Doesn't matter what it maps to;
* use {@link Boolean#TRUE}.
*/
PURE_BARCODE/*(Void.class)*/,
/**
* Image is known to be of one of a few possible formats.
* Maps to a {@link List} of {@link BarcodeFormat}s.
*/
POSSIBLE_FORMATS/*(List.class)*/,
/**
* Spend more time to try to find a barcode; optimize for accuracy, not speed.
* Doesn't matter what it maps to; use {@link Boolean#TRUE}.
*/
TRY_HARDER/*(Void.class)*/,
/**
* Specifies what character encoding to use when decoding, where applicable (type String)
*/
CHARACTER_SET/*(String.class)*/,
/**
* Allowed lengths of encoded data -- reject anything else. Maps to an {@code Int32Array}.
*/
ALLOWED_LENGTHS/*(Int32Array.class)*/,
/**
* Assume Code 39 codes employ a check digit. Doesn't matter what it maps to;
* use {@link Boolean#TRUE}.
*/
ASSUME_CODE_39_CHECK_DIGIT/*(Void.class)*/,
/**
* Assume the barcode is being processed as a GS1 barcode, and modify behavior as needed.
* For example this affects FNC1 handling for Code 128 (aka GS1-128). Doesn't matter what it maps to;
* use {@link Boolean#TRUE}.
*/
ASSUME_GS1/*(Void.class)*/,
/**
* If true, return the start and end digits in a Codabar barcode instead of stripping them. They
* are alpha, whereas the rest are numeric. By default, they are stripped, but this causes them
* to not be. Doesn't matter what it maps to; use {@link Boolean#TRUE}.
*/
RETURN_CODABAR_START_END/*(Void.class)*/,
/**
* The caller needs to be notified via callback when a possible {@link ResultPoint}
* is found. Maps to a {@link ResultPointCallback}.
*/
NEED_RESULT_POINT_CALLBACK/*(ResultPointCallback.class)*/,
/**
* Allowed extension lengths for EAN or UPC barcodes. Other formats will ignore this.
* Maps to an {@code Int32Array} of the allowed extension lengths, for example [2], [5], or [2, 5].
* If it is optional to have an extension, do not set this hint. If this is set,
* and a UPC or EAN barcode is found but an extension is not, then no result will be returned
* at all.
*/
ALLOWED_EAN_EXTENSIONS/*(Int32Array.class)*/,
// End of enumeration values.
/**
* Data type the hint is expecting.
* Among the possible values the {@link Void} stands out as being used for
* hints that do not expect a value to be supplied (flag hints). Such hints
* will possibly have their value ignored, or replaced by a
* {@link Boolean#TRUE}. Hint suppliers should probably use
* {@link Boolean#TRUE} as directed by the actual hint documentation.
*/
// private valueType: Class<?>
// DecodeHintType(valueType: Class<?>) {
// this.valueType = valueType
// }
// public getValueType(): Class<?> {
// return valueType
// }
}
export default DecodeHintType

View file

@ -0,0 +1,58 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Exception from './Exception'
/*namespace com.google.zxing {*/
/**
* Simply encapsulates a width and height.
*/
export default class Dimension {
public constructor(private width: number/*int*/, private height: number/*int*/) {
if (width < 0 || height < 0) {
throw new Exception(Exception.IllegalArgumentException)
}
}
public getWidth(): number/*int*/ {
return this.width
}
public getHeight(): number/*int*/ {
return this.height
}
/*@Override*/
public equals(other: any): boolean {
if (other instanceof Dimension) {
const d = <Dimension> other
return this.width == d.width && this.height == d.height
}
return false
}
/*@Override*/
public hashCode(): number/*int*/ {
return this.width * 32713 + this.height;
}
/*@Override*/
public toString(): string {
return this.width + "x" + this.height
}
}

View file

@ -0,0 +1,106 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
/**
* These are a set of hints that you may pass to Writers to specify their behavior.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
enum EncodeHintType {
/**
* Specifies what degree of error correction to use, for example in QR Codes.
* Type depends on the encoder. For example for QR codes it's type
* {@link com.google.zxing.qrcode.decoder.ErrorCorrectionLevel ErrorCorrectionLevel}.
* For Aztec it is of type {@link Integer}, representing the minimal percentage of error correction words.
* For PDF417 it is of type {@link Integer}, valid values being 0 to 8.
* In all cases, it can also be a {@link String} representation of the desired value as well.
* Note: an Aztec symbol should have a minimum of 25% EC words.
*/
ERROR_CORRECTION,
/**
* Specifies what character encoding to use where applicable (type {@link String})
*/
CHARACTER_SET,
/**
* Specifies the matrix shape for Data Matrix (type {@link com.google.zxing.datamatrix.encoder.SymbolShapeHint})
*/
DATA_MATRIX_SHAPE,
/**
* Specifies a minimum barcode size (type {@link Dimension}). Only applicable to Data Matrix now.
*
* @deprecated use width/height params in
* {@link com.google.zxing.datamatrix.DataMatrixWriter#encode(String, BarcodeFormat, int, int)}
*/
/*@Deprecated*/
MIN_SIZE,
/**
* Specifies a maximum barcode size (type {@link Dimension}). Only applicable to Data Matrix now.
*
* @deprecated without replacement
*/
/*@Deprecated*/
MAX_SIZE,
/**
* Specifies margin, in pixels, to use when generating the barcode. The meaning can vary
* by format; for example it controls margin before and after the barcode horizontally for
* most 1D formats. (Type {@link Integer}, or {@link String} representation of the integer value).
*/
MARGIN,
/**
* Specifies whether to use compact mode for PDF417 (type {@link Boolean}, or "true" or "false"
* {@link String} value).
*/
PDF417_COMPACT,
/**
* Specifies what compaction mode to use for PDF417 (type
* {@link com.google.zxing.pdf417.encoder.Compaction Compaction} or {@link String} value of one of its
* enum values).
*/
PDF417_COMPACTION,
/**
* Specifies the minimum and maximum number of rows and columns for PDF417 (type
* {@link com.google.zxing.pdf417.encoder.Dimensions Dimensions}).
*/
PDF417_DIMENSIONS,
/**
* Specifies the required number of layers for an Aztec code.
* A negative number (-1, -2, -3, -4) specifies a compact Aztec code.
* 0 indicates to use the minimum number of layers (the default).
* A positive number (1, 2, .. 32) specifies a normal (non-compact) Aztec code.
* (Type {@link Integer}, or {@link String} representation of the integer value).
*/
AZTEC_LAYERS,
/**
* Specifies the exact version of QR code to be encoded.
* (Type {@link Integer}, or {@link String} representation of the integer value).
*/
QR_VERSION,
}
export default EncodeHintType

View file

@ -0,0 +1,28 @@
export default class Exception {
public static IllegalArgumentException = "IllegalArgumentException"
public static NotFoundException = "NotFoundException"
public static ArithmeticException = "ArithmeticException"
public static FormatException = "FormatException"
public static ChecksumException = "ChecksumException"
public static WriterException = "WriterException"
public static IllegalStateException = "IllegalStateException"
public static UnsupportedOperationException = "UnsupportedOperationException"
public static ReedSolomonException = "ReedSolomonException"
public static ArgumentException = "ArgumentException"
public static ReaderException = "ReaderException"
public constructor(private type: string, private message?: string) {}
public getType(): string {
return this.type
}
public getMessage(): string|undefined {
return this.message
}
public static isOfType(ex: any, type: string): boolean {
return ex.type === type
}
}

View file

@ -0,0 +1,87 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import LuminanceSource from './LuminanceSource'
/*namespace com.google.zxing {*/
/**
* A wrapper implementation of {@link LuminanceSource} which inverts the luminances it returns -- black becomes
* white and vice versa, and each value becomes (255-value).
*
* @author Sean Owen
*/
export default class InvertedLuminanceSource extends LuminanceSource {
public constructor(private delegate: LuminanceSource) {
super(delegate.getWidth(), delegate.getHeight())
}
/*@Override*/
public getRow(y: number/*int*/, row?: Uint8ClampedArray): Uint8ClampedArray {
const sourceRow = this.delegate.getRow(y, row)
const width: number/*int*/ = this.getWidth()
for (let i = 0; i < width; i++) {
sourceRow[i] = /*(byte)*/ (255 - (sourceRow[i] & 0xFF))
}
return sourceRow
}
/*@Override*/
public getMatrix(): Uint8ClampedArray {
const matrix: Uint8ClampedArray = this.delegate.getMatrix()
const length: number/*int*/ = this.getWidth() * this.getHeight()
const invertedMatrix = new Uint8ClampedArray(length)
for (let i = 0; i < length; i++) {
invertedMatrix[i] = /*(byte)*/ (255 - (matrix[i] & 0xFF))
}
return invertedMatrix
}
/*@Override*/
public isCropSupported(): boolean {
return this.delegate.isCropSupported()
}
/*@Override*/
public crop(left: number/*int*/, top: number/*int*/, width: number/*int*/, height: number/*int*/): LuminanceSource {
return new InvertedLuminanceSource(this.delegate.crop(left, top, width, height))
}
/*@Override*/
public isRotateSupported(): boolean {
return this.delegate.isRotateSupported()
}
/**
* @return original delegate {@link LuminanceSource} since invert undoes itself
*/
/*@Override*/
public invert(): LuminanceSource {
return this.delegate
}
/*@Override*/
public rotateCounterClockwise(): LuminanceSource {
return new InvertedLuminanceSource(this.delegate.rotateCounterClockwise())
}
/*@Override*/
public rotateCounterClockwise45(): LuminanceSource {
return new InvertedLuminanceSource(this.delegate.rotateCounterClockwise45())
}
}

View file

@ -0,0 +1,155 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Exception from './Exception'
import InvertedLuminanceSource from './InvertedLuminanceSource'
import StringBuilder from './util/StringBuilder'
/*namespace com.google.zxing {*/
/**
* The purpose of this class hierarchy is to abstract different bitmap implementations across
* platforms into a standard interface for requesting greyscale luminance values. The interface
* only provides immutable methods; therefore crop and rotation create copies. This is to ensure
* that one Reader does not modify the original luminance source and leave it in an unknown state
* for other Readers in the chain.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
abstract class LuminanceSource {
protected constructor(private width: number/*int*/, private height: number/*int*/) {}
/**
* Fetches one row of luminance data from the underlying platform's bitmap. Values range from
* 0 (black) to 255 (white). Because Java does not have an unsigned byte type, callers will have
* to bitwise and with 0xff for each value. It is preferable for implementations of this method
* to only fetch this row rather than the whole image, since no 2D Readers may be installed and
* getMatrix() may never be called.
*
* @param y The row to fetch, which must be in [0,getHeight())
* @param row An optional preallocated array. If null or too small, it will be ignored.
* Always use the returned object, and ignore the .length of the array.
* @return An array containing the luminance data.
*/
public abstract getRow(y: number/*int*/, row?: Uint8ClampedArray): Uint8ClampedArray
/**
* Fetches luminance data for the underlying bitmap. Values should be fetched using:
* {@code int luminance = array[y * width + x] & 0xff}
*
* @return A row-major 2D array of luminance values. Do not use result.length as it may be
* larger than width * height bytes on some platforms. Do not modify the contents
* of the result.
*/
public abstract getMatrix(): Uint8ClampedArray
/**
* @return The width of the bitmap.
*/
public getWidth(): number/*int*/ {
return this.width
}
/**
* @return The height of the bitmap.
*/
public getHeight(): number/*int*/ {
return this.height
}
/**
* @return Whether this subclass supports cropping.
*/
public isCropSupported(): boolean {
return false
}
/**
* Returns a new object with cropped image data. Implementations may keep a reference to the
* original data rather than a copy. Only callable if isCropSupported() is true.
*
* @param left The left coordinate, which must be in [0,getWidth())
* @param top The top coordinate, which must be in [0,getHeight())
* @param width The width of the rectangle to crop.
* @param height The height of the rectangle to crop.
* @return A cropped version of this object.
*/
public crop(left: number/*int*/, top: number/*int*/, width: number/*int*/, height: number/*int*/): LuminanceSource {
throw new Exception(Exception.UnsupportedOperationException, "This luminance source does not support cropping.")
}
/**
* @return Whether this subclass supports counter-clockwise rotation.
*/
public isRotateSupported(): boolean {
return false
}
/**
* @return a wrapper of this {@code LuminanceSource} which inverts the luminances it returns -- black becomes
* white and vice versa, and each value becomes (255-value).
*/
public abstract invert(): LuminanceSource
/**
* Returns a new object with rotated image data by 90 degrees counterclockwise.
* Only callable if {@link #isRotateSupported()} is true.
*
* @return A rotated version of this object.
*/
public rotateCounterClockwise(): LuminanceSource {
throw new Exception(Exception.UnsupportedOperationException, "This luminance source does not support rotation by 90 degrees.")
}
/**
* Returns a new object with rotated image data by 45 degrees counterclockwise.
* Only callable if {@link #isRotateSupported()} is true.
*
* @return A rotated version of this object.
*/
public rotateCounterClockwise45(): LuminanceSource {
throw new Exception(Exception.UnsupportedOperationException, "This luminance source does not support rotation by 45 degrees.")
}
/*@Override*/
public toString(): string {
const row = new Uint8ClampedArray(this.width)
let result = new StringBuilder()
for (let y = 0; y < this.height; y++) {
const sourceRow = this.getRow(y, row)
for (let x = 0; x < this.width; x++) {
const luminance = sourceRow[x] & 0xFF
let c
if (luminance < 0x40) {
c = '#'
} else if (luminance < 0x80) {
c = '+'
} else if (luminance < 0xC0) {
c = '.'
} else {
c = ' '
}
result.append(c)
}
result.append('\n')
}
return result.toString()
}
}
export default LuminanceSource

View file

@ -0,0 +1,181 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import DecodeHintType from './DecodeHintType'
import Reader from './Reader'
import Result from './Result'
import BinaryBitmap from './BinaryBitmap'
import BarcodeFormat from './BarcodeFormat'
import QRCodeReader from './qrcode/QRCodeReader'
import Exception from './Exception'
/*namespace com.google.zxing {*/
/**
* MultiFormatReader is a convenience class and the main entry point into the library for most uses.
* By default it attempts to decode all barcode formats that the library supports. Optionally, you
* can provide a hints object to request different behavior, for example only decoding QR codes.
*
* @author Sean Owen
* @author dswitkin@google.com (Daniel Switkin)
*/
export default class MultiFormatReader implements Reader {
private hints: Map<DecodeHintType, any>|null
private readers: Reader[]
/**
* This version of decode honors the intent of Reader.decode(BinaryBitmap) in that it
* passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly.
* Use setHints() followed by decodeWithState() for continuous scan applications.
*
* @param image The pixel data to decode
* @return The contents of the image
* @throws NotFoundException Any errors which occurred
*/
/*@Override*/
// public decode(image: BinaryBitmap): Result /*throws NotFoundException */ {
// setHints(null)
// return decodeInternal(image)
// }
/**
* Decode an image using the hints provided. Does not honor existing state.
*
* @param image The pixel data to decode
* @param hints The hints to use, clearing the previous state.
* @return The contents of the image
* @throws NotFoundException Any errors which occurred
*/
/*@Override*/
public decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any>): Result /*throws NotFoundException */ {
this.setHints(hints)
return this.decodeInternal(image)
}
/**
* Decode an image using the state set up by calling setHints() previously. Continuous scan
* clients will get a <b>large</b> speed increase by using this instead of decode().
*
* @param image The pixel data to decode
* @return The contents of the image
* @throws NotFoundException Any errors which occurred
*/
public decodeWithState(image: BinaryBitmap): Result /*throws NotFoundException */ {
// Make sure to set up the default state so we don't crash
if (this.readers === null || this.readers === undefined) {
this.setHints(null)
}
return this.decodeInternal(image)
}
/**
* This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls
* to decodeWithState(image) can reuse the same set of readers without reallocating memory. This
* is important for performance in continuous scan clients.
*
* @param hints The set of hints to use for subsequent calls to decode(image)
*/
public setHints(hints?: Map<DecodeHintType, any>|null): void {
this.hints = hints
const tryHarder: boolean = hints !== null && hints !== undefined && undefined !== hints.get(DecodeHintType.TRY_HARDER)
/*@SuppressWarnings("unchecked")*/
const formats = hints === null || hints === undefined ? null : hints.get(DecodeHintType.POSSIBLE_FORMATS)
const readers = new Array<Reader>()
if (formats !== null && formats !== undefined) {
const addOneDReader: boolean =
formats.contains(BarcodeFormat.UPC_A) ||
formats.contains(BarcodeFormat.UPC_E) ||
formats.contains(BarcodeFormat.EAN_13) ||
formats.contains(BarcodeFormat.EAN_8) ||
formats.contains(BarcodeFormat.CODABAR) ||
formats.contains(BarcodeFormat.CODE_39) ||
formats.contains(BarcodeFormat.CODE_93) ||
formats.contains(BarcodeFormat.CODE_128) ||
formats.contains(BarcodeFormat.ITF) ||
formats.contains(BarcodeFormat.RSS_14) ||
formats.contains(BarcodeFormat.RSS_EXPANDED)
// Put 1D readers upfront in "normal" mode
// TYPESCRIPTPORT: TODO: uncomment below as they are ported
// if (addOneDReader && !tryHarder) {
// readers.push(new MultiFormatOneDReader(hints))
// }
if (formats.contains(BarcodeFormat.QR_CODE)) {
readers.push(new QRCodeReader())
}
// if (formats.contains(BarcodeFormat.DATA_MATRIX)) {
// readers.push(new DataMatrixReader())
// }
// if (formats.contains(BarcodeFormat.AZTEC)) {
// readers.push(new AztecReader())
// }
// if (formats.contains(BarcodeFormat.PDF_417)) {
// readers.push(new PDF417Reader())
// }
// if (formats.contains(BarcodeFormat.MAXICODE)) {
// readers.push(new MaxiCodeReader())
// }
// // At end in "try harder" mode
// if (addOneDReader && tryHarder) {
// readers.push(new MultiFormatOneDReader(hints))
// }
}
if (readers.length === 0) {
// if (!tryHarder) {
// readers.push(new MultiFormatOneDReader(hints))
// }
readers.push(new QRCodeReader())
// readers.push(new DataMatrixReader())
// readers.push(new AztecReader())
// readers.push(new PDF417Reader())
// readers.push(new MaxiCodeReader())
// if (tryHarder) {
// readers.push(new MultiFormatOneDReader(hints))
// }
}
this.readers = readers//.toArray(new Reader[readers.size()])
}
/*@Override*/
public reset(): void {
if (this.readers !== null) {
for (let i = 0, length = this.readers.length; i !== length; i++) {
const reader = this.readers[i]
reader.reset()
}
}
}
private decodeInternal(image: BinaryBitmap): Result /*throws NotFoundException */ {
if (this.readers !== null) {
for (let i = 0, length = this.readers.length; i !== length; i++) {
const reader = this.readers[i]
try {
return reader.decode(image, this.hints)
} catch (re/*ReaderException*/) {
// continue
}
}
}
throw new Exception(Exception.NotFoundException)
}
}

View file

@ -0,0 +1,108 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
import BitMatrix from './common/BitMatrix'
// import DataMatrixWriter from './datamatrix/DataMatrixWriter'
// import CodaBarWriter from './oned/CodaBarWriter'
// import Code128Writer from './oned/Code128Writer'
// import Code39Writer from './oned/Code39Writer'
// import Code93Writer from './oned/Code93Writer'
// import EAN13Writer from './oned/EAN13Writer'
// import EAN8Writer from './oned/EAN8Writer'
// import ITFWriter from './oned/ITFWriter'
// import UPCAWriter from './oned/UPCAWriter'
// import UPCEWriter from './oned/UPCEWriter'
// import PDF417Writer from './pdf417/PDF417Writer'
import QRCodeWriter from './qrcode/QRCodeWriter'
import Writer from './Writer'
import BarcodeFormat from './BarcodeFormat'
import EncodeHintType from './EncodeHintType'
import Exception from './Exception'
/*import java.util.Map;*/
/**
* This is a factory class which finds the appropriate Writer subclass for the BarcodeFormat
* requested and encodes the barcode with the supplied contents.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
export default class MultiFormatWriter implements Writer {
/*@Override*/
// public encode(contents: string,
// format: BarcodeFormat,
// width: number/*int*/,
// height: number/*int*/): BitMatrix /*throws WriterException */ {
// return encode(contents, format, width, height, null)
// }
/*@Override*/
public encode(contents: string,
format: BarcodeFormat,
width: number/*int*/, height: number/*int*/,
hints: Map<EncodeHintType, any>): BitMatrix /*throws WriterException */ {
let writer: Writer
switch (format) {
// case BarcodeFormat.EAN_8:
// writer = new EAN8Writer()
// break
// case BarcodeFormat.UPC_E:
// writer = new UPCEWriter()
// break
// case BarcodeFormat.EAN_13:
// writer = new EAN13Writer()
// break
// case BarcodeFormat.UPC_A:
// writer = new UPCAWriter()
// break
case BarcodeFormat.QR_CODE:
writer = new QRCodeWriter()
break
// case BarcodeFormat.CODE_39:
// writer = new Code39Writer()
// break
// case BarcodeFormat.CODE_93:
// writer = new Code93Writer()
// break
// case BarcodeFormat.CODE_128:
// writer = new Code128Writer()
// break
// case BarcodeFormat.ITF:
// writer = new ITFWriter()
// break
// case BarcodeFormat.PDF_417:
// writer = new PDF417Writer()
// break
// case BarcodeFormat.CODABAR:
// writer = new CodaBarWriter()
// break
// case BarcodeFormat.DATA_MATRIX:
// writer = new DataMatrixWriter()
// break
// case BarcodeFormat.AZTEC:
// writer = new AztecWriter()
// break
default:
throw new Exception(Exception.IllegalArgumentException, "No encoder available for format " + format)
}
return writer.encode(contents, format, width, height, hints)
}
}

View file

@ -0,0 +1,166 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
import System from './util/System'
import Exception from './Exception'
import LuminanceSource from './LuminanceSource'
import InvertedLuminanceSource from './InvertedLuminanceSource'
/**
* This object extends LuminanceSource around an array of YUV data returned from the camera driver,
* with the option to crop to a rectangle within the full data. This can be used to exclude
* superfluous pixels around the perimeter and speed up decoding.
*
* It works for any pixel format where the Y channel is planar and appears first, including
* YCbCr_420_SP and YCbCr_422_SP.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
export default class PlanarYUVLuminanceSource extends LuminanceSource {
private static THUMBNAIL_SCALE_FACTOR: number/*int*/ = 2
public constructor(private yuvData: Uint8ClampedArray,
private dataWidth: number/*int*/,
private dataHeight: number/*int*/,
private left: number/*int*/,
private top: number/*int*/,
width: number/*int*/,
height: number/*int*/,
reverseHorizontal: boolean) {
super(width, height)
if (left + width > dataWidth || top + height > dataHeight) {
throw new Exception(Exception.IllegalArgumentException, "Crop rectangle does not fit within image data.")
}
if (reverseHorizontal) {
this.reverseHorizontal(width, height)
}
}
/*@Override*/
public getRow(y: number/*int*/, row?: Uint8ClampedArray): Uint8ClampedArray {
if (y < 0 || y >= this.getHeight()) {
throw new Exception(Exception.IllegalArgumentException, "Requested row is outside the image: " + y)
}
const width: number/*int*/ = this.getWidth();
if (row === null || row === undefined || row.length < width) {
row = new Uint8ClampedArray(width)
}
const offset = (y + this.top) * this.dataWidth + this.left
System.arraycopy(this.yuvData, offset, row, 0, width)
return row
}
/*@Override*/
public getMatrix(): Uint8ClampedArray {
const width: number/*int*/ = this.getWidth();
const height: number/*int*/ = this.getHeight();
// If the caller asks for the entire underlying image, save the copy and give them the
// original data. The docs specifically warn that result.length must be ignored.
if (width == this.dataWidth && height == this.dataHeight) {
return this.yuvData
}
const area = width * height;
const matrix = new Uint8ClampedArray(area)
let inputOffset = this.top * this.dataWidth + this.left;
// If the width matches the full width of the underlying data, perform a single copy.
if (width === this.dataWidth) {
System.arraycopy(this.yuvData, inputOffset, matrix, 0, area)
return matrix
}
// Otherwise copy one cropped row at a time.
for (let y = 0; y < height; y++) {
const outputOffset = y * width;
System.arraycopy(this.yuvData, inputOffset, matrix, outputOffset, width)
inputOffset += this.dataWidth
}
return matrix
}
/*@Override*/
public isCropSupported(): boolean {
return true
}
/*@Override*/
public crop(left: number/*int*/, top: number/*int*/, width: number/*int*/, height: number/*int*/): LuminanceSource {
return new PlanarYUVLuminanceSource(this.yuvData,
this.dataWidth,
this.dataHeight,
this.left + left,
this.top + top,
width,
height,
false)
}
public renderThumbnail(): Int32Array {
const width: number/*int*/ = this.getWidth() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
const height: number/*int*/ = this.getHeight() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
const pixels = new Int32Array(width * height);
const yuv = this.yuvData
let inputOffset = this.top * this.dataWidth + this.left;
for (let y = 0; y < height; y++) {
const outputOffset = y * width;
for (let x = 0; x < width; x++) {
const grey = yuv[inputOffset + x * PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR] & 0xff;
pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101);
}
inputOffset += this.dataWidth * PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR;
}
return pixels
}
/**
* @return width of image from {@link #renderThumbnail()}
*/
public getThumbnailWidth(): number/*int*/ {
return this.getWidth() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR
}
/**
* @return height of image from {@link #renderThumbnail()}
*/
public getThumbnailHeight(): number/*int*/ {
return this.getHeight() / PlanarYUVLuminanceSource.THUMBNAIL_SCALE_FACTOR
}
private reverseHorizontal(width: number/*int*/, height: number/*int*/): void {
const yuvData = this.yuvData
for (let y = 0, rowStart = this.top * this.dataWidth + this.left; y < height; y++, rowStart += this.dataWidth) {
const middle = rowStart + width / 2
for (let x1 = rowStart, x2 = rowStart + width - 1; x1 < middle; x1++, x2--) {
const temp = yuvData[x1]
yuvData[x1] = yuvData[x2]
yuvData[x2] = temp
}
}
}
public invert(): LuminanceSource {
return new InvertedLuminanceSource(this)
}
}

View file

@ -0,0 +1,165 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
import './InvertedLuminanceSource' // required because of circular dependencies between LuminanceSource and InvertedLuminanceSource
import InvertedLuminanceSource from './InvertedLuminanceSource'
import LuminanceSource from './LuminanceSource'
import Exception from './Exception'
import System from './util/System'
/**
* This class is used to help decode images from files which arrive as RGB data from
* an ARGB pixel array. It does not support rotation.
*
* @author dswitkin@google.com (Daniel Switkin)
* @author Betaminos
*/
export default class RGBLuminanceSource extends LuminanceSource {
// public constructor(width: number/*int*/, height: number/*int*/, const pixels: Int32Array) {
// super(width, height)
// dataWidth = width
// dataHeight = height
// left = 0
// top = 0
// // In order to measure pure decoding speed, we convert the entire image to a greyscale array
// // up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.
// //
// // Total number of pixels suffices, can ignore shape
// const size = width * height;
// luminances = new byte[size]
// for (let offset = 0; offset < size; offset++) {
// const pixel = pixels[offset]
// const r = (pixel >> 16) & 0xff; // red
// const g2 = (pixel >> 7) & 0x1fe; // 2 * green
// const b = pixel & 0xff; // blue
// // Calculate green-favouring average cheaply
// luminances[offset] = (byte) ((r + g2 + b) / 4)
// }
// }
private luminances: Uint8ClampedArray
public constructor(luminances: Uint8ClampedArray|Int32Array,
width: number/*int*/,
height: number/*int*/,
private dataWidth?: number/*int*/,
private dataHeight?: number/*int*/,
private left?: number/*int*/,
private top?: number/*int*/) {
super(width, height)
if (luminances.BYTES_PER_ELEMENT === 4) {//Int32Array
const size = width * height;
const luminancesUint8Array = new Uint8ClampedArray(size)
for (let offset = 0; offset < size; offset++) {
const pixel = luminances[offset]
const r = (pixel >> 16) & 0xff; // red
const g2 = (pixel >> 7) & 0x1fe; // 2 * green
const b = pixel & 0xff; // blue
// Calculate green-favouring average cheaply
luminancesUint8Array[offset] = /*(byte) */((r + g2 + b) / 4) & 0xFF
}
this.luminances = luminancesUint8Array
} else {
this.luminances = <Uint8ClampedArray> luminances
}
if (undefined === dataWidth) {
this.dataWidth = width
}
if (undefined === dataHeight) {
this.dataHeight = height
}
if (undefined === left) {
this.left = 0
}
if (undefined === top) {
this.top = 0
}
if (this.left + width > this.dataWidth || this.top + height > this.dataHeight) {
throw new Exception(Exception.IllegalArgumentException, "Crop rectangle does not fit within image data.")
}
}
/*@Override*/
public getRow(y: number/*int*/, row?: Uint8ClampedArray): Uint8ClampedArray {
if (y < 0 || y >= this.getHeight()) {
throw new Exception(Exception.IllegalArgumentException, "Requested row is outside the image: " + y)
}
const width = this.getWidth()
if (row === null || row === undefined || row.length < width) {
row = new Uint8ClampedArray(width)
}
const offset = (y + this.top) * this.dataWidth + this.left
System.arraycopy(this.luminances, offset, row, 0, width)
return row
}
/*@Override*/
public getMatrix(): Uint8ClampedArray {
const width = this.getWidth();
const height = this.getHeight();
// If the caller asks for the entire underlying image, save the copy and give them the
// original data. The docs specifically warn that result.length must be ignored.
if (width === this.dataWidth && height === this.dataHeight) {
return this.luminances
}
const area = width * height;
const matrix = new Uint8ClampedArray(area)
let inputOffset = this.top * this.dataWidth + this.left;
// If the width matches the full width of the underlying data, perform a single copy.
if (width === this.dataWidth) {
System.arraycopy(this.luminances, inputOffset, matrix, 0, area)
return matrix
}
// Otherwise copy one cropped row at a time.
for (let y = 0; y < height; y++) {
const outputOffset = y * width;
System.arraycopy(this.luminances, inputOffset, matrix, outputOffset, width)
inputOffset += this.dataWidth
}
return matrix
}
/*@Override*/
public isCropSupported(): boolean {
return true
}
/*@Override*/
public crop(left: number/*int*/, top: number/*int*/, width: number/*int*/, height: number/*int*/): LuminanceSource {
return new RGBLuminanceSource(this.luminances,
width,
height,
this.dataWidth,
this.dataHeight,
this.left + left,
this.top + top,)
}
public invert(): LuminanceSource {
return new InvertedLuminanceSource(this)
}
}

View file

@ -0,0 +1,75 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
/*import java.util.Map;*/
import BinaryBitmap from './BinaryBitmap'
import Result from './Result'
import DecodeHintType from './DecodeHintType'
export default Reader
/**
* Implementations of this interface can decode an image of a barcode in some format into
* the it: string encodes. For example, {@link com.google.zxing.qrcode.QRCodeReader} can
* decode a QR code. The decoder may optionally receive hints from the caller which may help
* it decode more quickly or accurately.
*
* See {@link MultiFormatReader}, which attempts to determine what barcode
* format is present within the image as well, and then decodes it accordingly.
*
* @author Sean Owen
* @author dswitkin@google.com (Daniel Switkin)
*/
interface Reader {
/**
* Locates and decodes a barcode in some format within an image.
*
* @param image image of barcode to decode
* @return which: string the barcode encodes
* @throws NotFoundException if no potential barcode is found
* @throws ChecksumException if a potential barcode is found but does not pass its checksum
* @throws FormatException if a potential barcode is found but format is invalid
*/
// decode(image: BinaryBitmap): Result /*throws NotFoundException, ChecksumException, FormatException*/
/**
* Locates and decodes a barcode in some format within an image. This method also accepts
* hints, each possibly associated to some data, which may help the implementation decode.
*
* @param image image of barcode to decode
* @param hints passed as a {@link Map} from {@link DecodeHintType}
* to arbitrary data. The
* meaning of the data depends upon the hint type. The implementation may or may not do
* anything with these hints.
* @return which: string the barcode encodes
* @throws NotFoundException if no potential barcode is found
* @throws ChecksumException if a potential barcode is found but does not pass its checksum
* @throws FormatException if a potential barcode is found but format is invalid
*/
decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any>|null): Result
/*throws NotFoundException, ChecksumException, FormatException*/
/**
* Resets any internal state the implementation has after a decode, to prepare it
* for reuse.
*/
reset(): void
}

View file

@ -0,0 +1,159 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
/*import java.util.EnumMap;*/
/*import java.util.Map;*/
import ResultPoint from './ResultPoint'
import BarcodeFormat from './BarcodeFormat'
import System from './util/System'
import ResultMetadataType from './ResultMetadataType'
/**
* <p>Encapsulates the result of decoding a barcode within an image.</p>
*
* @author Sean Owen
*/
export default class Result {
private resultMetadata: Map<ResultMetadataType, Object>
// public constructor(private text: string,
// Uint8Array rawBytes,
// ResultPoconst resultPoints: Int32Array,
// BarcodeFormat format) {
// this(text, rawBytes, resultPoints, format, System.currentTimeMillis())
// }
// public constructor(text: string,
// Uint8Array rawBytes,
// ResultPoconst resultPoints: Int32Array,
// BarcodeFormat format,
// long timestamp) {
// this(text, rawBytes, rawBytes == null ? 0 : 8 * rawBytes.length,
// resultPoints, format, timestamp)
// }
public constructor(private text: string,
private rawBytes: Uint8Array,
private numBits: number/*int*/,
private resultPoints: Array<ResultPoint>,
private format: BarcodeFormat,
private timestamp: number/*long*/) {
this.text = text
this.rawBytes = rawBytes
if (undefined === numBits || null === numBits) {
this.numBits = (rawBytes === null || rawBytes === undefined) ? 0 : 8 * rawBytes.length
} else {
this.numBits = numBits
}
this.resultPoints = resultPoints
this.format = format
this.resultMetadata = null
if (undefined === timestamp || null === timestamp) {
this.timestamp = System.currentTimeMillis()
} else {
this.timestamp = timestamp
}
}
/**
* @return raw text encoded by the barcode
*/
public getText(): string {
return this.text
}
/**
* @return raw bytes encoded by the barcode, if applicable, otherwise {@code null}
*/
public getRawBytes(): Uint8Array {
return this.rawBytes
}
/**
* @return how many bits of {@link #getRawBytes()} are valid; typically 8 times its length
* @since 3.3.0
*/
public getNumBits(): number/*int*/ {
return this.numBits
}
/**
* @return points related to the barcode in the image. These are typically points
* identifying finder patterns or the corners of the barcode. The exact meaning is
* specific to the type of barcode that was decoded.
*/
public getResultPoints(): Array<ResultPoint> {
return this.resultPoints
}
/**
* @return {@link BarcodeFormat} representing the format of the barcode that was decoded
*/
public getBarcodeFormat(): BarcodeFormat {
return this.format
}
/**
* @return {@link Map} mapping {@link ResultMetadataType} keys to values. May be
* {@code null}. This contains optional metadata about what was detected about the barcode,
* like orientation.
*/
public getResultMetadata(): Map<ResultMetadataType, Object> {
return this.resultMetadata
}
public putMetadata(type: ResultMetadataType, value: Object): void {
if (this.resultMetadata === null) {
this.resultMetadata = new Map<ResultMetadataType, Object>()
}
this.resultMetadata.set(type, value)
}
public putAllMetadata(metadata: Map<ResultMetadataType, Object>): void {
if (metadata !== null) {
if (this.resultMetadata === null) {
this.resultMetadata = metadata
} else {
this.resultMetadata = new Map(metadata)
}
}
}
public addResultPoints(newPoints: Array<ResultPoint>): void {
const oldPoints = this.resultPoints
if (oldPoints === null) {
this.resultPoints = newPoints
} else if (newPoints !== null && newPoints.length > 0) {
const allPoints: Array<ResultPoint> = new Array(oldPoints.length + newPoints.length)
System.arraycopy(oldPoints, 0, allPoints, 0, oldPoints.length)
System.arraycopy(newPoints, 0, allPoints, oldPoints.length, newPoints.length)
this.resultPoints = allPoints
}
}
public getTimestamp(): number/*long*/ {
return this.timestamp
}
/*@Override*/
public toString(): string {
return this.text
}
}

View file

@ -0,0 +1,99 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
/**
* Represents some type of metadata about the result of the decoding that the decoder
* wishes to communicate back to the caller.
*
* @author Sean Owen
*/
enum ResultMetadataType {
/**
* Unspecified, application-specific metadata. Maps to an unspecified {@link Object}.
*/
OTHER,
/**
* Denotes the likely approximate orientation of the barcode in the image. This value
* is given as degrees rotated clockwise from the normal, upright orientation.
* For example a 1D barcode which was found by reading top-to-bottom would be
* said to have orientation "90". This key maps to an {@link Integer} whose
* value is in the range [0,360).
*/
ORIENTATION,
/**
* <p>2D barcode formats typically encode text, but allow for a sort of 'byte mode'
* which is sometimes used to encode binary data. While {@link Result} makes available
* the complete raw bytes in the barcode for these formats, it does not offer the bytes
* from the byte segments alone.</p>
*
* <p>This maps to a {@link java.util.List} of byte arrays corresponding to the
* raw bytes in the byte segments in the barcode, in order.</p>
*/
BYTE_SEGMENTS,
/**
* Error correction level used, if applicable. The value type depends on the
* format, but is typically a String.
*/
ERROR_CORRECTION_LEVEL,
/**
* For some periodicals, indicates the issue number as an {@link Integer}.
*/
ISSUE_NUMBER,
/**
* For some products, indicates the suggested retail price in the barcode as a
* formatted {@link String}.
*/
SUGGESTED_PRICE,
/**
* For some products, the possible country of manufacture as a {@link String} denoting the
* ISO country code. Some map to multiple possible countries, like "US/CA".
*/
POSSIBLE_COUNTRY,
/**
* For some products, the extension text
*/
UPC_EAN_EXTENSION,
/**
* PDF417-specific metadata
*/
PDF417_EXTRA_METADATA,
/**
* If the code format supports structured append and the current scanned code is part of one then the
* sequence number is given with it.
*/
STRUCTURED_APPEND_SEQUENCE,
/**
* If the code format supports structured append and the current scanned code is part of one then the
* parity is given with it.
*/
STRUCTURED_APPEND_PARITY,
}
export default ResultMetadataType

View file

@ -0,0 +1,125 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
import MathUtils from './common/detector/MathUtils'
import Float from './util/Float'
/**
* <p>Encapsulates a point of interest in an image containing a barcode. Typically, this
* would be the location of a finder pattern or the corner of the barcode, for example.</p>
*
* @author Sean Owen
*/
export default class ResultPoint {
public constructor(private x: number/*float*/, private y: number/*float*/) {}
public getX(): number/*float*/ {
return this.x
}
public getY(): number/*float*/ {
return this.y
}
/*@Override*/
public equals(other: Object): boolean {
if (other instanceof ResultPoint) {
const otherPoint = <ResultPoint> other
return this.x == otherPoint.x && this.y == otherPoint.y
}
return false
}
/*@Override*/
public hashCode(): number/*int*/ {
return 31 * Float.floatToIntBits(this.x) + Float.floatToIntBits(this.y);
}
/*@Override*/
public toString(): string {
return "(" + this.x + ',' + this.y + ')'
}
/**
* Orders an array of three ResultPoints in an order [A,B,C] such that AB is less than AC
* and BC is less than AC, and the angle between BC and BA is less than 180 degrees.
*
* @param patterns array of three {@code ResultPoint} to order
*/
public static orderBestPatterns(patterns: Array<ResultPoint>): void {
// Find distances between pattern centers
const zeroOneDistance = this.distance(patterns[0], patterns[1])
const oneTwoDistance = this.distance(patterns[1], patterns[2])
const zeroTwoDistance = this.distance(patterns[0], patterns[2])
let pointA: ResultPoint
let pointB: ResultPoint
let pointC: ResultPoint
// Assume one closest to other two is B; A and C will just be guesses at first
if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance) {
pointB = patterns[0]
pointA = patterns[1]
pointC = patterns[2]
} else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance) {
pointB = patterns[1]
pointA = patterns[0]
pointC = patterns[2]
} else {
pointB = patterns[2]
pointA = patterns[0]
pointC = patterns[1]
}
// Use cross product to figure out whether A and C are correct or flipped.
// This asks whether BC x BA has a positive z component, which is the arrangement
// we want for A, B, C. If it's negative, then we've got it flipped around and
// should swap A and C.
if (this.crossProductZ(pointA, pointB, pointC) < 0.0) {
const temp = pointA
pointA = pointC
pointC = temp
}
patterns[0] = pointA
patterns[1] = pointB
patterns[2] = pointC
}
/**
* @param pattern1 first pattern
* @param pattern2 second pattern
* @return distance between two points
*/
public static distance(pattern1: ResultPoint, pattern2: ResultPoint): number/*float*/ {
return MathUtils.distance(pattern1.x, pattern1.y, pattern2.x, pattern2.y)
}
/**
* Returns the z component of the cross product between vectors BC and BA.
*/
private static crossProductZ(pointA: ResultPoint,
pointB: ResultPoint,
pointC: ResultPoint): number/*float*/ {
const bX = pointB.x
const bY = pointB.y
return ((pointC.x - bX) * (pointA.y - bY)) - ((pointC.y - bY) * (pointA.x - bX))
}
}

View file

@ -0,0 +1,33 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
import ResultPoint from './ResultPoint'
export default ResultPointCallback
/**
* Callback which is invoked when a possible result point (significant
* point in the barcode image such as a corner) is found.
*
* @see DecodeHintType#NEED_RESULT_POINT_CALLBACK
*/
interface ResultPointCallback {
foundPossibleResultPoint(point: ResultPoint): void
}

View file

@ -0,0 +1,63 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing {*/
import BitMatrix from './common/BitMatrix'
import BarcodeFormat from './BarcodeFormat'
import EncodeHintType from './EncodeHintType'
export default Writer
/*import java.util.Map;*/
/**
* The base class for all objects which encode/generate a barcode image.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
interface Writer {
/**
* Encode a barcode using the default settings.
*
* @param contents The contents to encode in the barcode
* @param format The barcode format to generate
* @param width The preferred width in pixels
* @param height The preferred height in pixels
* @return {@link BitMatrix} representing encoded barcode image
* @throws WriterException if contents cannot be encoded legally in a format
*/
// encode(contents: string, format: BarcodeFormat, width: number/*int*/, height: number/*int*/): BitMatrix
/*throws WriterException*/
/**
* @param contents The contents to encode in the barcode
* @param format The barcode format to generate
* @param width The preferred width in pixels
* @param height The preferred height in pixels
* @param hints Additional parameters to supply to the encoder
* @return {@link BitMatrix} representing encoded barcode image
* @throws WriterException if contents cannot be encoded legally in a format
*/
encode(contents: string,
format: BarcodeFormat,
width: number/*int*/,
height: number/*int*/,
hints: Map<EncodeHintType, any>): BitMatrix
/*throws WriterException*/
}

View file

@ -0,0 +1,388 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
/*import java.util.Arrays;*/
import System from './../util/System'
import Integer from './../util/Integer'
import Arrays from './../util/Arrays'
import Exception from './../Exception'
/**
* <p>A simple, fast array of bits, represented compactly by an array of ints internally.</p>
*
* @author Sean Owen
*/
export default class BitArray /*implements Cloneable*/ {
private size: number
private bits: Int32Array
// public constructor() {
// this.size = 0
// this.bits = new Int32Array(1)
// }
// public constructor(size?: number/*int*/) {
// if (undefined === size) {
// this.size = 0
// } else {
// this.size = size
// }
// this.bits = this.makeArray(size)
// }
// For testing only
public constructor(size?: number/*int*/, bits?: Int32Array) {
if (undefined === size) {
this.size = 0
this.bits = new Int32Array(1)
} else {
this.size = size
if (undefined === bits || null === bits) {
this.bits = BitArray.makeArray(size)
} else {
this.bits = bits
}
}
}
public getSize(): number/*int*/ {
return this.size
}
public getSizeInBytes(): number/*int*/ {
return Math.floor((this.size + 7) / 8)
}
private ensureCapacity(size: number/*int*/): void {
if (size > this.bits.length * 32) {
const newBits = BitArray.makeArray(size)
System.arraycopy(this.bits, 0, newBits, 0, this.bits.length)
this.bits = newBits
}
}
/**
* @param i bit to get
* @return true iff bit i is set
*/
public get(i: number/*int*/): boolean {
return (this.bits[Math.floor(i / 32)] & (1 << (i & 0x1F))) !== 0
}
/**
* Sets bit i.
*
* @param i bit to set
*/
public set(i: number/*int*/): void {
this.bits[Math.floor(i / 32)] |= 1 << (i & 0x1F)
}
/**
* Flips bit i.
*
* @param i bit to set
*/
public flip(i: number/*int*/): void {
this.bits[Math.floor(i / 32)] ^= 1 << (i & 0x1F)
}
/**
* @param from first bit to check
* @return index of first bit that is set, starting from the given index, or size if none are set
* at or beyond this given index
* @see #getNextUnset(int)
*/
public getNextSet(from: number/*int*/): number/*int*/ {
const size = this.size
if (from >= size) {
return size
}
const bits = this.bits
let bitsOffset = Math.floor(from / 32)
let currentBits = bits[bitsOffset]
// mask off lesser bits first
currentBits &= ~((1 << (from & 0x1F)) - 1)
const length = bits.length
while (currentBits === 0) {
if (++bitsOffset === length) {
return size
}
currentBits = bits[bitsOffset]
}
const result = (bitsOffset * 32) + Integer.numberOfTrailingZeros(currentBits)
return result > size ? size : result
}
/**
* @param from index to start looking for unset bit
* @return index of next unset bit, or {@code size} if none are unset until the end
* @see #getNextSet(int)
*/
public getNextUnset(from: number/*int*/): number/*int*/ {
const size = this.size
if (from >= size) {
return size
}
const bits = this.bits
let bitsOffset = Math.floor(from / 32)
let currentBits = ~bits[bitsOffset]
// mask off lesser bits first
currentBits &= ~((1 << (from & 0x1F)) - 1)
const length = bits.length
while (currentBits === 0) {
if (++bitsOffset === length) {
return size
}
currentBits = ~bits[bitsOffset]
}
const result = (bitsOffset * 32) + Integer.numberOfTrailingZeros(currentBits)
return result > size ? size : result
}
/**
* Sets a block of 32 bits, starting at bit i.
*
* @param i first bit to set
* @param newBits the new value of the next 32 bits. Note again that the least-significant bit
* corresponds to bit i, the next-least-significant to i+1, and so on.
*/
public setBulk(i: number/*int*/, newBits: number/*int*/): void {
this.bits[Math.floor(i / 32)] = newBits
}
/**
* Sets a range of bits.
*
* @param start start of range, inclusive.
* @param end end of range, exclusive
*/
public setRange(start: number/*int*/, end: number/*int*/): void {
if (end < start || start < 0 || end > this.size) {
throw new Exception(Exception.IllegalArgumentException)
}
if (end === start) {
return
}
end-- // will be easier to treat this as the last actually set bit -- inclusive
const firstInt = Math.floor(start / 32)
const lastInt =Math.floor(end / 32)
const bits = this.bits
for (let i = firstInt; i <= lastInt; i++) {
const firstBit = i > firstInt ? 0 : start & 0x1F
const lastBit = i < lastInt ? 31 : end & 0x1F
// Ones from firstBit to lastBit, inclusive
const mask = (2 << lastBit) - (1 << firstBit)
bits[i] |= mask
}
}
/**
* Clears all bits (sets to false).
*/
public clear(): void {
const max = this.bits.length
const bits = this.bits
for (let i = 0; i < max; i++) {
bits[i] = 0
}
}
/**
* Efficient method to check if a range of bits is set, or not set.
*
* @param start start of range, inclusive.
* @param end end of range, exclusive
* @param value if true, checks that bits in range are set, otherwise checks that they are not set
* @return true iff all bits are set or not set in range, according to value argument
* @throws IllegalArgumentException if end is less than start or the range is not contained in the array
*/
public isRange(start: number/*int*/, end: number/*int*/, value: boolean): boolean {
if (end < start || start < 0 || end > this.size) {
throw new Exception(Exception.IllegalArgumentException)
}
if (end === start) {
return true // empty range matches
}
end-- // will be easier to treat this as the last actually set bit -- inclusive
const firstInt = Math.floor(start / 32)
const lastInt = Math.floor(end / 32)
const bits = this.bits
for (let i = firstInt; i <= lastInt; i++) {
const firstBit = i > firstInt ? 0 : start & 0x1F
const lastBit = i < lastInt ? 31 : end & 0x1F
// Ones from firstBit to lastBit, inclusive
const mask = (2 << lastBit) - (1 << firstBit) & 0xFFFFFFFF
// TYPESCRIPTPORT: & 0xFFFFFFFF added to discard anything after 32 bits, as ES has 53 bits
// Return false if we're looking for 1s and the masked bits[i] isn't all 1s (is: that,
// equals the mask, or we're looking for 0s and the masked portion is not all 0s
if ((bits[i] & mask) !== (value ? mask : 0)) {
return false
}
}
return true
}
public appendBit(bit: boolean): void {
this.ensureCapacity(this.size + 1)
if (bit) {
this.bits[Math.floor(this.size / 32)] |= 1 << (this.size & 0x1F)
}
this.size++
}
/**
* Appends the least-significant bits, from value, in order from most-significant to
* least-significant. For example, appending 6 bits from 0x000001E will append the bits
* 0, 1, 1, 1, 1, 0 in that order.
*
* @param value {@code int} containing bits to append
* @param numBits bits from value to append
*/
public appendBits(value: number/*int*/, numBits: number/*int*/): void {
if (numBits < 0 || numBits > 32) {
throw new Exception(Exception.IllegalArgumentException, "Num bits must be between 0 and 32")
}
this.ensureCapacity(this.size + numBits)
const appendBit = this.appendBit
for (let numBitsLeft = numBits; numBitsLeft > 0; numBitsLeft--) {
this.appendBit(((value >> (numBitsLeft - 1)) & 0x01) == 1)
}
}
public appendBitArray(other: BitArray): void {
const otherSize = other.size
this.ensureCapacity(this.size + otherSize)
const appendBit = this.appendBit
for (let i = 0; i < otherSize; i++) {
this.appendBit(other.get(i))
}
}
public xor(other: BitArray): void {
if (this.size != other.size) {
throw new Exception(Exception.IllegalArgumentException, "Sizes don't match")
}
const bits = this.bits;
for (let i = 0, length = bits.length; i < length; i++) {
// The last int could be incomplete (i.e. not have 32 bits in
// it) but there is no problem since 0 XOR 0 == 0.
bits[i] ^= other.bits[i]
}
}
/**
*
* @param bitOffset first bit to start writing
* @param array array to write into. Bytes are written most-significant byte first. This is the opposite
* of the internal representation, which is exposed by {@link #getBitArray()}
* @param offset position in array to start writing
* @param numBytes how many bytes to write
*/
public toBytes(bitOffset: number/*int*/, array: Uint8Array, offset: number/*int*/, numBytes: number/*int*/): void {
for (let i = 0; i < numBytes; i++) {
let theByte = 0
for (let j = 0; j < 8; j++) {
if (this.get(bitOffset)) {
theByte |= 1 << (7 - j)
}
bitOffset++
}
array[offset + i] = /*(byte)*/ theByte
}
}
/**
* @return underlying array of ints. The first element holds the first 32 bits, and the least
* significant bit is bit 0.
*/
public getBitArray(): Int32Array {
return this.bits
}
/**
* Reverses all bits in the array.
*/
public reverse(): void {
const newBits = new Int32Array(this.bits.length)
// reverse all int's first
const len = Math.floor((this.size - 1) / 32)
const oldBitsLen = len + 1
const bits = this.bits
for (let i = 0; i < oldBitsLen; i++) {
let x = bits[i]
x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1)
x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2)
x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4)
x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8)
x = ((x >> 16) & 0x0000ffff) | ((x & 0x0000ffff) << 16)
newBits[len - i] = /*(int)*/ x
}
// now correct the int's if the bit size isn't a multiple of 32
if (this.size !== oldBitsLen * 32) {
const leftOffset = oldBitsLen * 32 - this.size;
let currentInt = newBits[0] >>> leftOffset
for (let i = 1; i < oldBitsLen; i++) {
const nextInt = newBits[i]
currentInt |= nextInt << (32 - leftOffset)
newBits[i - 1] = currentInt
currentInt = nextInt >>> leftOffset
}
newBits[oldBitsLen - 1] = currentInt
}
this.bits = newBits
}
private static makeArray(size: number/*int*/): Int32Array {
return new Int32Array(Math.floor((size + 31) / 32))
}
/*@Override*/
public equals(o: any): boolean {
if (!(o instanceof BitArray)) {
return false
}
const other = <BitArray> o
return this.size === other.size && Arrays.equals(this.bits, other.bits)
}
/*@Override*/
public hashCode(): number/*int*/ {
return 31 * this.size + Arrays.hashCode(this.bits);
}
/*@Override*/
public toString(): string {
let result = ""
for (let i = 0, size = this.size; i < size; i++) {
if ((i & 0x07) === 0) {
result += " "
}
result += this.get(i) ? "X" : "."
}
return result
}
/*@Override*/
public clone(): BitArray {
return new BitArray(this.size, this.bits.slice())
}
}

View file

@ -0,0 +1,511 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
/*import java.util.Arrays;*/
import Exception from './../Exception'
import BitArray from './BitArray'
import System from './../util/System'
import Arrays from './../util/Arrays'
import StringBuilder from './../util/StringBuilder'
/**
* <p>Represents a 2D matrix of bits. In function arguments below, and throughout the common
* module, x is the column position, and y is the row position. The ordering is always x, y.
* The origin is at the top-left.</p>
*
* <p>Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins
* with a new int. This is done intentionally so that we can copy out a row into a BitArray very
* efficiently.</p>
*
* <p>The ordering of bits is row-major. Within each int, the least significant bits are used first,
* meaning they represent lower x values. This is compatible with BitArray's implementation.</p>
*
* @author Sean Owen
* @author dswitkin@google.com (Daniel Switkin)
*/
export default class BitMatrix /*implements Cloneable*/ {
/**
* Creates an empty square {@link BitMatrix}.
*
* @param dimension height and width
*/
// public constructor(dimension: number/*int*/) {
// this(dimension, dimension)
// }
/**
* Creates an empty {@link BitMatrix}.
*
* @param width bit matrix width
* @param height bit matrix height
*/
// public constructor(width: number/*int*/, height: number/*int*/) {
// if (width < 1 || height < 1) {
// throw new Exception(Exception.IllegalArgumentException, "Both dimensions must be greater than 0")
// }
// this.width = width
// this.height = height
// this.rowSize = (width + 31) / 32
// bits = new int[rowSize * height];
// }
public constructor(private width: number/*int*/, private height?: number/*int*/,
private rowSize?: number/*int*/, private bits?: Int32Array) {
if (undefined === height || null === height) {
height = width
}
this.height = height
if (width < 1 || height < 1) {
throw new Exception(Exception.IllegalArgumentException, "Both dimensions must be greater than 0")
}
if (undefined === rowSize || null === rowSize) {
rowSize = Math.floor((width + 31) / 32)
}
this.rowSize = rowSize
if (undefined === bits || null === bits) {
this.bits = new Int32Array(this.rowSize * this.height)
}
}
/**
* Interprets a 2D array of booleans as a {@link BitMatrix}, where "true" means an "on" bit.
*
* @param image bits of the image, as a row-major 2D array. Elements are arrays representing rows
* @return {@link BitMatrix} representation of image
*/
public static parseFromBooleanArray(image: boolean[][]): BitMatrix {
const height = image.length
const width = image[0].length
const bits = new BitMatrix(width, height)
for (let i = 0; i < height; i++) {
const imageI = image[i]
for (let j = 0; j < width; j++) {
if (imageI[j]) {
bits.set(j, i)
}
}
}
return bits
}
public static parseFromString(stringRepresentation: string, setString: string, unsetString: string): BitMatrix {
if (stringRepresentation === null) {
throw new Exception(Exception.IllegalArgumentException, "stringRepresentation cannot be null")
}
const bits = new Array<boolean>(stringRepresentation.length)
let bitsPos = 0
let rowStartPos = 0
let rowLength = -1
let nRows = 0
let pos = 0
while (pos < stringRepresentation.length) {
if (stringRepresentation.charAt(pos) === '\n' ||
stringRepresentation.charAt(pos) === '\r') {
if (bitsPos > rowStartPos) {
if (rowLength === -1) {
rowLength = bitsPos - rowStartPos
} else if (bitsPos - rowStartPos != rowLength) {
throw new Exception(Exception.IllegalArgumentException, "row lengths do not match")
}
rowStartPos = bitsPos
nRows++
}
pos++
} else if (stringRepresentation.substring(pos, pos + setString.length) === setString) {
pos += setString.length
bits[bitsPos] = true
bitsPos++
} else if (stringRepresentation.substring(pos, pos + unsetString.length) === unsetString) {
pos += unsetString.length
bits[bitsPos] = false
bitsPos++
} else {
throw new Exception(Exception.IllegalArgumentException,
"illegal character encountered: " + stringRepresentation.substring(pos))
}
}
// no EOL at end?
if (bitsPos > rowStartPos) {
if (rowLength === -1) {
rowLength = bitsPos - rowStartPos
} else if (bitsPos - rowStartPos !== rowLength) {
throw new Exception(Exception.IllegalArgumentException, "row lengths do not match")
}
nRows++
}
const matrix = new BitMatrix(rowLength, nRows)
for (let i = 0; i < bitsPos; i++) {
if (bits[i]) {
matrix.set(Math.floor(i % rowLength), Math.floor(i / rowLength))
}
}
return matrix
}
/**
* <p>Gets the requested bit, where true means black.</p>
*
* @param x The horizontal component (i.e. which column)
* @param y The vertical component (i.e. which row)
* @return value of given bit in matrix
*/
public get(x: number/*int*/, y: number/*int*/): boolean {
const offset = y * this.rowSize + Math.floor(x / 32)
return ((this.bits[offset] >>> (x & 0x1f)) & 1) != 0
}
/**
* <p>Sets the given bit to true.</p>
*
* @param x The horizontal component (i.e. which column)
* @param y The vertical component (i.e. which row)
*/
public set(x: number/*int*/, y: number/*int*/): void {
const offset = y * this.rowSize + Math.floor(x / 32)
this.bits[offset] |= (1 << (x & 0x1f)) & 0xFFFFFFFF
}
public unset(x: number/*int*/, y: number/*int*/): void {
const offset = y * this.rowSize + Math.floor(x / 32)
this.bits[offset] &= ~((1 << (x & 0x1f)) & 0xFFFFFFFF)
}
/**
* <p>Flips the given bit.</p>
*
* @param x The horizontal component (i.e. which column)
* @param y The vertical component (i.e. which row)
*/
public flip(x: number/*int*/, y: number/*int*/): void {
const offset = y * this.rowSize + Math.floor(x / 32)
this.bits[offset] ^= ((1 << (x & 0x1f)) & 0xFFFFFFFF)
}
/**
* Exclusive-or (XOR): Flip the bit in this {@code BitMatrix} if the corresponding
* mask bit is set.
*
* @param mask XOR mask
*/
public xor(mask: BitMatrix): void {
if (this.width != mask.getWidth() || this.height != mask.getHeight()
|| this.rowSize != mask.getRowSize()) {
throw new Exception(Exception.IllegalArgumentException, "input matrix dimensions do not match")
}
const rowArray = new BitArray(Math.floor(this.width / 32) + 1)
const rowSize = this.rowSize
const bits = this.bits
for (let y = 0, height = this.height; y < height; y++) {
const offset = y * rowSize;
const row = mask.getRow(y, rowArray).getBitArray()
for (let x = 0; x < rowSize; x++) {
bits[offset + x] ^= row[x]
}
}
}
/**
* Clears all bits (sets to false).
*/
public clear(): void {
const bits = this.bits
const max = bits.length
for (let i = 0; i < max; i++) {
bits[i] = 0
}
}
/**
* <p>Sets a square region of the bit matrix to true.</p>
*
* @param left The horizontal position to begin at (inclusive)
* @param top The vertical position to begin at (inclusive)
* @param width The width of the region
* @param height The height of the region
*/
public setRegion(left: number/*int*/, top: number/*int*/, width: number/*int*/, height: number/*int*/): void {
if (top < 0 || left < 0) {
throw new Exception(Exception.IllegalArgumentException, "Left and top must be nonnegative")
}
if (height < 1 || width < 1) {
throw new Exception(Exception.IllegalArgumentException, "Height and width must be at least 1")
}
const right = left + width
const bottom = top + height
if (bottom > this.height || right > this.width) {
throw new Exception(Exception.IllegalArgumentException, "The region must fit inside the matrix")
}
const rowSize = this.rowSize
const bits = this.bits
for (let y = top; y < bottom; y++) {
const offset = y * rowSize;
for (let x = left; x < right; x++) {
bits[offset + Math.floor(x / 32)] |= ((1 << (x & 0x1f)) & 0xFFFFFFFF)
}
}
}
/**
* A fast method to retrieve one row of data from the matrix as a BitArray.
*
* @param y The row to retrieve
* @param row An optional caller-allocated BitArray, will be allocated if null or too small
* @return The resulting BitArray - this reference should always be used even when passing
* your own row
*/
public getRow(y: number/*int*/, row?: BitArray): BitArray {
if (row === null || row === undefined || row.getSize() < this.width) {
row = new BitArray(this.width)
} else {
row.clear()
}
const rowSize = this.rowSize
const bits = this.bits
const offset = y * rowSize;
for (let x = 0; x < rowSize; x++) {
row.setBulk(x * 32, bits[offset + x]);
}
return row
}
/**
* @param y row to set
* @param row {@link BitArray} to copy from
*/
public setRow(y: number/*int*/, row: BitArray): void {
System.arraycopy(row.getBitArray(), 0, this.bits, y * this.rowSize, this.rowSize);
}
/**
* Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees
*/
public rotate180(): void {
const width = this.getWidth();
const height = this.getHeight();
let topRow = new BitArray(width)
let bottomRow = new BitArray(width)
for (let i = 0, length = Math.floor((height + 1) / 2); i < length; i++) {
topRow = this.getRow(i, topRow)
bottomRow = this.getRow(height - 1 - i, bottomRow)
topRow.reverse()
bottomRow.reverse()
this.setRow(i, bottomRow)
this.setRow(height - 1 - i, topRow)
}
}
/**
* This is useful in detecting the enclosing rectangle of a 'pure' barcode.
*
* @return {@code left,top,width,height} enclosing rectangle of all 1 bits, or null if it is all white
*/
public getEnclosingRectangle(): Int32Array {
const width = this.width
const height = this.height
const rowSize = this.rowSize
const bits = this.bits
let left = width
let top = height
let right = -1
let bottom = -1
for (let y = 0; y < height; y++) {
for (let x32 = 0; x32 < rowSize; x32++) {
const theBits = bits[y * rowSize + x32];
if (theBits !== 0) {
if (y < top) {
top = y
}
if (y > bottom) {
bottom = y
}
if (x32 * 32 < left) {
let bit = 0
while (((theBits << (31 - bit)) & 0xFFFFFFFF) === 0) {
bit++
}
if ((x32 * 32 + bit) < left) {
left = x32 * 32 + bit;
}
}
if (x32 * 32 + 31 > right) {
let bit = 31
while ((theBits >>> bit) === 0) {
bit--
}
if ((x32 * 32 + bit) > right) {
right = x32 * 32 + bit;
}
}
}
}
}
if (right < left || bottom < top) {
return null
}
return Int32Array.from([left, top, right - left + 1, bottom - top + 1])
}
/**
* This is useful in detecting a corner of a 'pure' barcode.
*
* @return {@code x,y} coordinate of top-left-most 1 bit, or null if it is all white
*/
public getTopLeftOnBit(): Int32Array {
const rowSize = this.rowSize
const bits = this.bits
let bitsOffset = 0
while (bitsOffset < bits.length && bits[bitsOffset] == 0) {
bitsOffset++
}
if (bitsOffset === bits.length) {
return null
}
const y = bitsOffset / rowSize
let x = (bitsOffset % rowSize) * 32;
const theBits = bits[bitsOffset]
let bit = 0
while (((theBits << (31 - bit)) & 0xFFFFFFFF) === 0) {
bit++
}
x += bit
return Int32Array.from([x, y])
}
public getBottomRightOnBit(): Int32Array {
const rowSize = this.rowSize
const bits = this.bits
let bitsOffset = bits.length - 1
while (bitsOffset >= 0 && bits[bitsOffset] === 0) {
bitsOffset--
}
if (bitsOffset < 0) {
return null
}
const y = Math.floor(bitsOffset / rowSize)
let x = Math.floor(bitsOffset % rowSize) * 32;
const theBits = bits[bitsOffset]
let bit = 31
while ((theBits >>> bit) === 0) {
bit--
}
x += bit
return Int32Array.from([x, y])
}
/**
* @return The width of the matrix
*/
public getWidth(): number/*int*/ {
return this.width
}
/**
* @return The height of the matrix
*/
public getHeight(): number/*int*/ {
return this.height
}
/**
* @return The row size of the matrix
*/
public getRowSize(): number/*int*/ {
return this.rowSize
}
/*@Override*/
public equals(o: Object): boolean {
if (!(o instanceof BitMatrix)) {
return false
}
const other = <BitMatrix> o
return this.width === other.width && this.height === other.height && this.rowSize === other.rowSize &&
Arrays.equals(this.bits, other.bits)
}
/*@Override*/
public hashCode(): number/*int*/ {
let hash = this.width
hash = 31 * hash + this.width
hash = 31 * hash + this.height
hash = 31 * hash + this.rowSize
hash = 31 * hash + Arrays.hashCode(this.bits)
return hash
}
/**
* @return string representation using "X" for set and " " for unset bits
*/
/*@Override*/
// public toString(): string {
// return toString(": "X, " ")
// }
/**
* @param setString representation of a set bit
* @param unsetString representation of an unset bit
* @return string representation of entire matrix utilizing given strings
*/
// public toString(setString: string = "X ", unsetString: string = " "): string {
// return this.buildToString(setString, unsetString, "\n")
// }
/**
* @param setString representation of a set bit
* @param unsetString representation of an unset bit
* @param lineSeparator newline character in string representation
* @return string representation of entire matrix utilizing given strings and line separator
* @deprecated call {@link #toString(String,String)} only, which uses \n line separator always
*/
// @Deprecated
public toString(setString: string = "x", unsetString: string = " ", lineSeparator: string = "\n"): string {
return this.buildToString(setString, unsetString, lineSeparator)
}
private buildToString(setString: string, unsetString: string, lineSeparator: string) {
let result = new StringBuilder()
result.append(lineSeparator)
for (let y = 0, height = this.height; y < height; y++) {
for (let x = 0, width = this.width; x < width; x++) {
result.append(this.get(x, y) ? setString : unsetString)
}
result.append(lineSeparator)
}
return result.toString()
}
/*@Override*/
public clone(): BitMatrix {
return new BitMatrix(this.width, this.height, this.rowSize, this.bits.slice())
}
}

View file

@ -0,0 +1,120 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
import Exception from './../Exception'
/**
* <p>This provides an easy abstraction to read bits at a time from a sequence of bytes, where the
* number of bits read is not often a multiple of 8.</p>
*
* <p>This class is thread-safe but not reentrant -- unless the caller modifies the bytes array
* it passed in, in which case all bets are off.</p>
*
* @author Sean Owen
*/
export default class BitSource {
private byteOffset: number/*int*/
private bitOffset: number/*int*/
/**
* @param bytes bytes from which this will read bits. Bits will be read from the first byte first.
* Bits are read within a byte from most-significant to least-significant bit.
*/
public constructor(private bytes: Uint8Array) {
this.byteOffset = 0
this.bitOffset = 0
}
/**
* @return index of next bit in current byte which would be read by the next call to {@link #readBits(int)}.
*/
public getBitOffset(): number/*int*/ {
return this.bitOffset
}
/**
* @return index of next byte in input byte array which would be read by the next call to {@link #readBits(int)}.
*/
public getByteOffset(): number/*int*/ {
return this.byteOffset
}
/**
* @param numBits number of bits to read
* @return int representing the bits read. The bits will appear as the least-significant
* bits of the int
* @throws IllegalArgumentException if numBits isn't in [1,32] or more than is available
*/
public readBits(numBits: number/*int*/): number/*int*/ {
if (numBits < 1 || numBits > 32 || numBits > this.available()) {
throw new Exception(Exception.IllegalArgumentException, "" + numBits)
}
let result = 0
let bitOffset = this.bitOffset
let byteOffset = this.byteOffset
const bytes = this.bytes
// First, read remainder from current byte
if (bitOffset > 0) {
const bitsLeft = 8 - bitOffset
const toRead = numBits < bitsLeft ? numBits : bitsLeft
const bitsToNotRead = bitsLeft - toRead
const mask = (0xFF >> (8 - toRead)) << bitsToNotRead
result = (bytes[byteOffset] & mask) >> bitsToNotRead
numBits -= toRead
bitOffset += toRead
if (bitOffset == 8) {
bitOffset = 0
byteOffset++
}
}
// Next read whole bytes
if (numBits > 0) {
while (numBits >= 8) {
result = (result << 8) | (bytes[byteOffset] & 0xFF)
byteOffset++
numBits -= 8
}
// Finally read a partial byte
if (numBits > 0) {
const bitsToNotRead = 8 - numBits
const mask = (0xFF >> bitsToNotRead) << bitsToNotRead
result = (result << numBits) | ((bytes[byteOffset] & mask) >> bitsToNotRead)
bitOffset += numBits
}
}
this.bitOffset = bitOffset
this.byteOffset = byteOffset
return result
}
/**
* @return number of bits that can be read successfully
*/
public available(): number/*int*/ {
return 8 * (this.bytes.length - this.byteOffset) - this.bitOffset;
}
}

View file

@ -0,0 +1,186 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
import Exception from './../Exception'
/*import java.util.HashMap;*/
/*import java.util.Map;*/
export const enum CharacterSetValueIdentifiers {
Cp437,
ISO8859_1,
ISO8859_2,
ISO8859_3,
ISO8859_4,
ISO8859_5,
ISO8859_6,
ISO8859_7,
ISO8859_8,
ISO8859_9,
ISO8859_10,
ISO8859_11,
ISO8859_13,
ISO8859_14,
ISO8859_15,
ISO8859_16,
SJIS,
Cp1250,
Cp1251,
Cp1252,
Cp1256,
UnicodeBigUnmarked,
UTF8,
ASCII,
Big5,
GB18030,
EUC_KR
}
/**
* Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1
* of ISO 18004.
*
* @author Sean Owen
*/
export default class CharacterSetECI {
private static VALUE_IDENTIFIER_TO_ECI = new Map<number, CharacterSetECI>()
private static VALUES_TO_ECI = new Map<number, CharacterSetECI>()
private static NAME_TO_ECI = new Map<string, CharacterSetECI>()
// Enum name is a Java encoding valid for java.lang and java.io
// TYPESCRIPTPORT: changed the main label for ISO as the TextEncoder did not recognized them in the form from java (eg ISO8859_1 must be ISO88591 or ISO8859-1 or ISO-8859-1)
// later on: well, except 16 wich does not work with ISO885916 so used ISO-8859-1 form for default
public static Cp437 = new CharacterSetECI(CharacterSetValueIdentifiers.Cp437, Int32Array.from([0,2]), "Cp437")
public static ISO8859_1 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_1, Int32Array.from([1,3]), "ISO-8859-1", "ISO88591", "ISO8859_1")
public static ISO8859_2 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_2, 4, "ISO-8859-2", "ISO88592", "ISO8859_2")
public static ISO8859_3 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_3, 5, "ISO-8859-3", "ISO88593", "ISO8859_3")
public static ISO8859_4 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_4, 6, "ISO-8859-4", "ISO88594", "ISO8859_4")
public static ISO8859_5 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_5, 7, "ISO-8859-5", "ISO88595", "ISO8859_5")
public static ISO8859_6 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_6, 8, "ISO-8859-6", "ISO88596", "ISO8859_6")
public static ISO8859_7 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_7, 9, "ISO-8859-7", "ISO88597", "ISO8859_7")
public static ISO8859_8 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_8, 10, "ISO-8859-8", "ISO88598", "ISO8859_8")
public static ISO8859_9 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_9, 11, "ISO-8859-9", "ISO88599", "ISO8859_9")
public static ISO8859_10 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_10, 12, "ISO-8859-10", "ISO885910", "ISO8859_10")
public static ISO8859_11 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_11, 13, "ISO-8859-11", "ISO885911", "ISO8859_11")
public static ISO8859_13 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_13, 15, "ISO-8859-13", "ISO885913", "ISO8859_13")
public static ISO8859_14 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_14, 16, "ISO-8859-14", "ISO885914", "ISO8859_14")
public static ISO8859_15 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_15, 17, "ISO-8859-15", "ISO885915", "ISO8859_15")
public static ISO8859_16 = new CharacterSetECI(CharacterSetValueIdentifiers.ISO8859_16, 18, "ISO-8859-16", "ISO885916", "ISO8859_16")
public static SJIS = new CharacterSetECI(CharacterSetValueIdentifiers.SJIS, 20, "SJIS", "Shift_JIS")
public static Cp1250 = new CharacterSetECI(CharacterSetValueIdentifiers.Cp1250, 21, "Cp1250", "windows-1250")
public static Cp1251 = new CharacterSetECI(CharacterSetValueIdentifiers.Cp1251, 22, "Cp1251", "windows-1251")
public static Cp1252 = new CharacterSetECI(CharacterSetValueIdentifiers.Cp1252, 23, "Cp1252", "windows-1252")
public static Cp1256 = new CharacterSetECI(CharacterSetValueIdentifiers.Cp1256, 24, "Cp1256", "windows-1256")
public static UnicodeBigUnmarked = new CharacterSetECI(CharacterSetValueIdentifiers.UnicodeBigUnmarked, 25, "UnicodeBigUnmarked", "UTF-16BE", "UnicodeBig")
public static UTF8 = new CharacterSetECI(CharacterSetValueIdentifiers.UTF8, 26, "UTF8", "UTF-8")
public static ASCII = new CharacterSetECI(CharacterSetValueIdentifiers.ASCII, Int32Array.from([27, 170]), "ASCII", "US-ASCII")
public static Big5 = new CharacterSetECI(CharacterSetValueIdentifiers.Big5, 28, "Big5")
public static GB18030 = new CharacterSetECI(CharacterSetValueIdentifiers.GB18030, 29, "GB18030", "GB2312", "EUC_CN", "GBK")
public static EUC_KR = new CharacterSetECI(CharacterSetValueIdentifiers.EUC_KR, 30, "EUC_KR", "EUC-KR")
public values: Int32Array
public otherEncodingNames: string[]
public constructor(public valueIdentifier: CharacterSetValueIdentifiers, valuesParam: Int32Array|number, public name: string, ...otherEncodingNames: string[]) {
if (typeof valuesParam === "number") {
this.values = Int32Array.from([valuesParam])
} else {
this.values = valuesParam
}
this.otherEncodingNames = otherEncodingNames
CharacterSetECI.VALUE_IDENTIFIER_TO_ECI.set(valueIdentifier, this)
CharacterSetECI.NAME_TO_ECI.set(name, this)
const values = this.values
for(let i = 0, length = values.length; i !== length; i++) {
const v = values[i]
CharacterSetECI.VALUES_TO_ECI.set(v, this)
}
for(const otherName of otherEncodingNames) {
CharacterSetECI.NAME_TO_ECI.set(otherName, this)
}
}
// CharacterSetECI(value: number/*int*/) {
// this(new Int32Array {value})
// }
// CharacterSetECI(value: number/*int*/, String... otherEncodingNames) {
// this.values = new Int32Array {value}
// this.otherEncodingNames = otherEncodingNames
// }
// CharacterSetECI(values: Int32Array, String... otherEncodingNames) {
// this.values = values
// this.otherEncodingNames = otherEncodingNames
// }
public getValueIdentifier(): CharacterSetValueIdentifiers/*int*/ {
return this.valueIdentifier
}
public getName(): string {
return this.name
}
public getValue(): number/*int*/ {
return this.values[0]
}
/**
* @param value character set ECI value
* @return {@code CharacterSetECI} representing ECI of given value, or null if it is legal but
* unsupported
* @throws FormatException if ECI value is invalid
*/
public static getCharacterSetECIByValue(value: number/*int*/): CharacterSetECI /*throws FormatException*/ {
if (value < 0 || value >= 900) {
throw new Exception(Exception.FormatException, "incorect value")
}
const characterSet = CharacterSetECI.VALUES_TO_ECI.get(value)
if (undefined === characterSet) {
throw new Exception(Exception.FormatException, "incorect value")
}
return characterSet
}
/**
* @param name character set ECI encoding name
* @return CharacterSetECI representing ECI for character encoding, or null if it is legal
* but unsupported
*/
public static getCharacterSetECIByName(name: string): CharacterSetECI {
const characterSet = CharacterSetECI.NAME_TO_ECI.get(name)
if (undefined === characterSet) {
throw new Exception(Exception.FormatException, "incorect value")
}
return characterSet
}
public equals(o: CharacterSetECI) {
if (!(o instanceof CharacterSetECI)) {
return false
}
const other = <CharacterSetECI> o
return this.getName() === other.getName()
}
}

View file

@ -0,0 +1,140 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
/*import java.util.List;*/
/**
* <p>Encapsulates the result of decoding a matrix of bits. This typically
* applies to 2D barcode formats. For now it contains the raw bytes obtained,
* as well as a String interpretation of those bytes, if applicable.</p>
*
* @author Sean Owen
*/
export default class DecoderResult {
private numBits: number/*int*/
private errorsCorrected: number/*Integer*/
private erasures: number/*Integer*/
private other: any
// public constructor(rawBytes: Uint8Array,
// text: string,
// List<Uint8Array> byteSegments,
// String ecLevel) {
// this(rawBytes, text, byteSegments, ecLevel, -1, -1)
// }
public constructor(private rawBytes: Uint8Array,
private text: string,
private byteSegments: Uint8Array[],
private ecLevel: string,
private structuredAppendSequenceNumber: number/*int*/,
private structuredAppendParity: number/*int*/) {
this.numBits = (rawBytes === undefined || rawBytes === null) ? 0 : 8 * rawBytes.length;
}
/**
* @return raw bytes representing the result, or {@code null} if not applicable
*/
public getRawBytes(): Uint8Array {
return this.rawBytes
}
/**
* @return how many bits of {@link #getRawBytes()} are valid; typically 8 times its length
* @since 3.3.0
*/
public getNumBits(): number/*int*/ {
return this.numBits
}
/**
* @param numBits overrides the number of bits that are valid in {@link #getRawBytes()}
* @since 3.3.0
*/
public setNumBits(numBits: number/*int*/): void {
this.numBits = numBits
}
/**
* @return text representation of the result
*/
public getText(): string {
return this.text
}
/**
* @return list of byte segments in the result, or {@code null} if not applicable
*/
public getByteSegments(): Uint8Array[] {
return this.byteSegments
}
/**
* @return name of error correction level used, or {@code null} if not applicable
*/
public getECLevel(): string {
return this.ecLevel
}
/**
* @return number of errors corrected, or {@code null} if not applicable
*/
public getErrorsCorrected(): number/*Integer*/ {
return this.errorsCorrected
}
public setErrorsCorrected(errorsCorrected: number/*Integer*/): void {
this.errorsCorrected = errorsCorrected
}
/**
* @return number of erasures corrected, or {@code null} if not applicable
*/
public getErasures(): number/*Integer*/ {
return this.erasures
}
public setErasures(erasures: number/*Integer*/): void {
this.erasures = erasures
}
/**
* @return arbitrary additional metadata
*/
public getOther(): any {
return this.other
}
public setOther(other: any): void {
this.other = other
}
public hasStructuredAppend(): boolean {
return this.structuredAppendParity >= 0 && this.structuredAppendSequenceNumber >= 0
}
public getStructuredAppendParity(): number/*int*/ {
return this.structuredAppendParity
}
public getStructuredAppendSequenceNumber(): number/*int*/ {
return this.structuredAppendSequenceNumber
}
}

View file

@ -0,0 +1,91 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
import GridSampler from './GridSampler'
import BitMatrix from './BitMatrix'
import PerspectiveTransform from './PerspectiveTransform'
import Exception from './../Exception'
/**
* @author Sean Owen
*/
export default class DefaultGridSampler extends GridSampler {
/*@Override*/
public sampleGrid(image: BitMatrix,
dimensionX: number/*int*/,
dimensionY: number/*int*/,
p1ToX: number/*float*/, p1ToY: number/*float*/,
p2ToX: number/*float*/, p2ToY: number/*float*/,
p3ToX: number/*float*/, p3ToY: number/*float*/,
p4ToX: number/*float*/, p4ToY: number/*float*/,
p1FromX: number/*float*/, p1FromY: number/*float*/,
p2FromX: number/*float*/, p2FromY: number/*float*/,
p3FromX: number/*float*/, p3FromY: number/*float*/,
p4FromX: number/*float*/, p4FromY: number/*float*/): BitMatrix /*throws NotFoundException*/ {
const transform = PerspectiveTransform.quadrilateralToQuadrilateral(
p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY,
p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY)
return this.sampleGridWithTransform(image, dimensionX, dimensionY, transform)
}
/*@Override*/
public sampleGridWithTransform(image: BitMatrix,
dimensionX: number/*int*/,
dimensionY: number/*int*/,
transform: PerspectiveTransform): BitMatrix /*throws NotFoundException*/ {
if (dimensionX <= 0 || dimensionY <= 0) {
throw new Exception(Exception.NotFoundException)
}
const bits = new BitMatrix(dimensionX, dimensionY)
const points = new Float32Array(2 * dimensionX)
for (let y = 0; y < dimensionY; y++) {
const max = points.length
const iValue: number/*float*/ = y + 0.5
for (let x = 0; x < max; x += 2) {
points[x] = /*(float)*/ (x / 2) + 0.5
points[x + 1] = iValue
}
transform.transformPoints(points)
// Quick check to see if points transformed to something inside the image
// sufficient to check the endpoints
GridSampler.checkAndNudgePoints(image, points)
try {
for (let x = 0; x < max; x += 2) {
if (image.get(Math.floor(points[x]), Math.floor(points[x + 1]))) {
// Black(-ish) pixel
bits.set(x / 2, y)
}
}
} catch (aioobe/*: ArrayIndexOutOfBoundsException*/) {
// This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
// transform gets "twisted" such that it maps a straight line of points to a set of points
// whose endpoints are in bounds, but others are not. There is probably some mathematical
// way to detect this about the transformation that I don't know yet.
// This results in an ugly runtime exception despite our clever checks above -- can't have
// that. We could check each point's coordinates but that feels duplicative. We settle for
// catching and wrapping ArrayIndexOutOfBoundsException.
throw new Exception(Exception.NotFoundException)
}
}
return bits
}
}

View file

@ -0,0 +1,47 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
import ResultPoint from './../ResultPoint'
import BitMatrix from './BitMatrix'
/**
* <p>Encapsulates the result of detecting a barcode in an image. This includes the raw
* matrix of black/white pixels corresponding to the barcode, and possibly points of interest
* in the image, like the location of finder patterns or corners of the barcode in the image.</p>
*
* @author Sean Owen
*/
export default class DetectorResult {
private bits: BitMatrix
private points: Array<ResultPoint>
public constructor(bits: BitMatrix, points: Array<ResultPoint>) {
this.bits = bits
this.points = points
}
public getBits(): BitMatrix {
return this.bits
}
public getPoints(): Array<ResultPoint> {
return this.points
}
}

View file

@ -0,0 +1,206 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
import Binarizer from './../Binarizer'
import LuminanceSource from './../LuminanceSource'
import BitArray from './BitArray'
import BitMatrix from './BitMatrix'
import Exception from './../Exception'
/**
* This Binarizer implementation uses the old ZXing global histogram approach. It is suitable
* for low-end mobile devices which don't have enough CPU or memory to use a local thresholding
* algorithm. However, because it picks a global black point, it cannot handle difficult shadows
* and gradients.
*
* Faster mobile devices and all desktop applications should probably use HybridBinarizer instead.
*
* @author dswitkin@google.com (Daniel Switkin)
* @author Sean Owen
*/
export default class GlobalHistogramBinarizer extends Binarizer {
private static LUMINANCE_BITS = 5;
private static LUMINANCE_SHIFT = 8 - GlobalHistogramBinarizer.LUMINANCE_BITS;
private static LUMINANCE_BUCKETS = 1 << GlobalHistogramBinarizer.LUMINANCE_BITS;
private static EMPTY = Uint8ClampedArray.from([0]);
private luminances: Uint8ClampedArray
private buckets: Int32Array
public constructor(source: LuminanceSource) {
super(source)
this.luminances = GlobalHistogramBinarizer.EMPTY
this.buckets = new Int32Array(GlobalHistogramBinarizer.LUMINANCE_BUCKETS)
}
// Applies simple sharpening to the row data to improve performance of the 1D Readers.
/*@Override*/
public getBlackRow(y: number/*int*/, row: BitArray): BitArray /*throws NotFoundException*/ {
const source = this.getLuminanceSource()
const width = source.getWidth();
if (row === undefined || row === null || row.getSize() < width) {
row = new BitArray(width)
} else {
row.clear()
}
this.initArrays(width)
const localLuminances = source.getRow(y, this.luminances)
const localBuckets = this.buckets
for (let x = 0; x < width; x++) {
localBuckets[(localLuminances[x] & 0xff) >> GlobalHistogramBinarizer.LUMINANCE_SHIFT]++
}
const blackPoint = GlobalHistogramBinarizer.estimateBlackPoint(localBuckets)
if (width < 3) {
// Special case for very small images
for (let x = 0; x < width; x++) {
if ((localLuminances[x] & 0xff) < blackPoint) {
row.set(x)
}
}
} else {
let left = localLuminances[0] & 0xff
let center = localLuminances[1] & 0xff
for (let x = 1; x < width - 1; x++) {
const right = localLuminances[x + 1] & 0xff
// A simple -1 4 -1 box filter with a weight of 2.
if (((center * 4) - left - right) / 2 < blackPoint) {
row.set(x)
}
left = center
center = right
}
}
return row
}
// Does not sharpen the data, as this call is intended to only be used by 2D Readers.
/*@Override*/
public getBlackMatrix(): BitMatrix /*throws NotFoundException*/ {
const source = this.getLuminanceSource()
const width = source.getWidth()
const height = source.getHeight()
const matrix = new BitMatrix(width, height)
// Quickly calculates the histogram by sampling four rows from the image. This proved to be
// more robust on the blackbox tests than sampling a diagonal as we used to do.
this.initArrays(width)
const localBuckets = this.buckets
for (let y = 1; y < 5; y++) {
const row = height * y / 5;
const localLuminances = source.getRow(row, this.luminances)
const right = Math.floor((width * 4) / 5)
for (let x = Math.floor(width / 5); x < right; x++) {
const pixel = localLuminances[x] & 0xff
localBuckets[pixel >> GlobalHistogramBinarizer.LUMINANCE_SHIFT]++
}
}
const blackPoint = GlobalHistogramBinarizer.estimateBlackPoint(localBuckets)
// We delay reading the entire image luminance until the black point estimation succeeds.
// Although we end up reading four rows twice, it is consistent with our motto of
// "fail quickly" which is necessary for continuous scanning.
const localLuminances = source.getMatrix()
for (let y = 0; y < height; y++) {
const offset = y * width;
for (let x = 0; x < width; x++) {
const pixel = localLuminances[offset + x] & 0xff
if (pixel < blackPoint) {
matrix.set(x, y)
}
}
}
return matrix
}
/*@Override*/
public createBinarizer(source: LuminanceSource): Binarizer {
return new GlobalHistogramBinarizer(source)
}
private initArrays(luminanceSize: number/*int*/): void {
if (this.luminances.length < luminanceSize) {
this.luminances = new Uint8ClampedArray(luminanceSize)
}
const buckets = this.buckets
for (let x = 0; x < GlobalHistogramBinarizer.LUMINANCE_BUCKETS; x++) {
buckets[x] = 0
}
}
private static estimateBlackPoint(buckets: Int32Array): number/*int*/ /*throws NotFoundException*/ {
// Find the tallest peak in the histogram.
const numBuckets = buckets.length
let maxBucketCount = 0
let firstPeak = 0
let firstPeakSize = 0
for (let x = 0; x < numBuckets; x++) {
if (buckets[x] > firstPeakSize) {
firstPeak = x
firstPeakSize = buckets[x]
}
if (buckets[x] > maxBucketCount) {
maxBucketCount = buckets[x]
}
}
// Find the second-tallest peak which is somewhat far from the tallest peak.
let secondPeak = 0
let secondPeakScore = 0
for (let x = 0; x < numBuckets; x++) {
const distanceToBiggest = x - firstPeak
// Encourage more distant second peaks by multiplying by square of distance.
const score = buckets[x] * distanceToBiggest * distanceToBiggest;
if (score > secondPeakScore) {
secondPeak = x
secondPeakScore = score
}
}
// Make sure firstPeak corresponds to the black peak.
if (firstPeak > secondPeak) {
const temp = firstPeak
firstPeak = secondPeak
secondPeak = temp
}
// If there is too little contrast in the image to pick a meaningful black point, throw rather
// than waste time trying to decode the image, and risk false positives.
if (secondPeak - firstPeak <= numBuckets / 16) {
throw new Exception(Exception.NotFoundException)
}
// Find a valley between them that is low and closer to the white peak.
let bestValley = secondPeak - 1
let bestValleyScore = -1
for (let x = secondPeak - 1; x > firstPeak; x--) {
const fromFirst = x - firstPeak
const score = fromFirst * fromFirst * (secondPeak - x) * (maxBucketCount - buckets[x]);
if (score > bestValleyScore) {
bestValley = x
bestValleyScore = score
}
}
return bestValley << GlobalHistogramBinarizer.LUMINANCE_SHIFT
}
}

View file

@ -0,0 +1,155 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
import BitMatrix from './BitMatrix'
import PerspectiveTransform from './PerspectiveTransform'
import Exception from './../Exception'
/**
* Implementations of this class can, given locations of finder patterns for a QR code in an
* image, sample the right points in the image to reconstruct the QR code, accounting for
* perspective distortion. It is abstracted since it is relatively expensive and should be allowed
* to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
* Imaging library, but which may not be available in other environments such as J2ME, and vice
* versa.
*
* The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}
* with an instance of a class which implements this interface.
*
* @author Sean Owen
*/
abstract class GridSampler {
/**
* Samples an image for a rectangular matrix of bits of the given dimension. The sampling
* transformation is determined by the coordinates of 4 points, in the original and transformed
* image space.
*
* @param image image to sample
* @param dimensionX width of {@link BitMatrix} to sample from image
* @param dimensionY height of {@link BitMatrix} to sample from image
* @param p1ToX point 1 preimage X
* @param p1ToY point 1 preimage Y
* @param p2ToX point 2 preimage X
* @param p2ToY point 2 preimage Y
* @param p3ToX point 3 preimage X
* @param p3ToY point 3 preimage Y
* @param p4ToX point 4 preimage X
* @param p4ToY point 4 preimage Y
* @param p1FromX point 1 image X
* @param p1FromY point 1 image Y
* @param p2FromX point 2 image X
* @param p2FromY point 2 image Y
* @param p3FromX point 3 image X
* @param p3FromY point 3 image Y
* @param p4FromX point 4 image X
* @param p4FromY point 4 image Y
* @return {@link BitMatrix} representing a grid of points sampled from the image within a region
* defined by the "from" parameters
* @throws NotFoundException if image can't be sampled, for example, if the transformation defined
* by the given points is invalid or results in sampling outside the image boundaries
*/
public abstract sampleGrid(image: BitMatrix,
dimensionX: number/*int*/,
dimensionY: number/*int*/,
p1ToX: number/*float*/, p1ToY: number/*float*/,
p2ToX: number/*float*/, p2ToY: number/*float*/,
p3ToX: number/*float*/, p3ToY: number/*float*/,
p4ToX: number/*float*/, p4ToY: number/*float*/,
p1FromX: number/*float*/, p1FromY: number/*float*/,
p2FromX: number/*float*/, p2FromY: number/*float*/,
p3FromX: number/*float*/, p3FromY: number/*float*/,
p4FromX: number/*float*/, p4FromY: number/*float*/): BitMatrix /*throws NotFoundException*/
public abstract sampleGridWithTransform(image: BitMatrix,
dimensionX: number/*int*/,
dimensionY: number/*int*/,
transform: PerspectiveTransform): BitMatrix /*throws NotFoundException*/
/**
* <p>Checks a set of points that have been transformed to sample points on an image against
* the image's dimensions to see if the point are even within the image.</p>
*
* <p>This method will actually "nudge" the endpoints back onto the image if they are found to be
* barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
* patterns in an image where the QR Code runs all the way to the image border.</p>
*
* <p>For efficiency, the method will check points from either end of the line until one is found
* to be within the image. Because the set of points are assumed to be linear, this is valid.</p>
*
* @param image image into which the points should map
* @param points actual points in x1,y1,...,xn,yn form
* @throws NotFoundException if an endpoint is lies outside the image boundaries
*/
protected static checkAndNudgePoints(image: BitMatrix,
points: Float32Array): void /*throws NotFoundException*/ {
const width: number/*int*/ = image.getWidth();
const height: number/*int*/ = image.getHeight();
// Check and nudge points from start until we see some that are OK:
let nudged: boolean = true
for (let offset = 0; offset < points.length && nudged; offset += 2) {
const x = Math.floor(points[offset])
const y = Math.floor(points[offset + 1])
if (x < -1 || x > width || y < -1 || y > height) {
throw new Exception(Exception.NotFoundException)
}
nudged = false
if (x == -1) {
points[offset] = 0.0
nudged = true
} else if (x == width) {
points[offset] = width - 1
nudged = true
}
if (y == -1) {
points[offset + 1] = 0.0
nudged = true
} else if (y == height) {
points[offset + 1] = height - 1
nudged = true
}
}
// Check and nudge points from end:
nudged = true
for (let offset = points.length - 2; offset >= 0 && nudged; offset -= 2) {
const x = Math.floor(points[offset])
const y = Math.floor(points[offset + 1])
if (x < -1 || x > width || y < -1 || y > height) {
throw new Exception(Exception.NotFoundException)
}
nudged = false
if (x == -1) {
points[offset] = 0.0
nudged = true
} else if (x == width) {
points[offset] = width - 1
nudged = true
}
if (y == -1) {
points[offset + 1] = 0.0
nudged = true
} else if (y == height) {
points[offset + 1] = height - 1
nudged = true
}
}
}
}
export default GridSampler

View file

@ -0,0 +1,28 @@
import GridSampler from './GridSampler'
import DefaultGridSampler from './DefaultGridSampler'
export default class GridSamplerInstance {
private static gridSampler: GridSampler = new DefaultGridSampler()
/**
* Sets the implementation of GridSampler used by the library. One global
* instance is stored, which may sound problematic. But, the implementation provided
* ought to be appropriate for the entire platform, and all uses of this library
* in the whole lifetime of the JVM. For instance, an Android activity can swap in
* an implementation that takes advantage of native platform libraries.
*
* @param newGridSampler The platform-specific object to install.
*/
public static setGridSampler(newGridSampler: GridSampler): void {
GridSamplerInstance.gridSampler = newGridSampler
}
/**
* @return the current implementation of GridSampler
*/
public static getInstance(): GridSampler {
return GridSamplerInstance.gridSampler
}
}

View file

@ -0,0 +1,239 @@
/*
* Copyright 2009 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
import Binarizer from './../Binarizer'
import LuminanceSource from './../LuminanceSource'
import GlobalHistogramBinarizer from './GlobalHistogramBinarizer'
import BitMatrix from './BitMatrix'
/**
* This class implements a local thresholding algorithm, which while slower than the
* GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for
* high frequency images of barcodes with black data on white backgrounds. For this application,
* it does a much better job than a global blackpoint with severe shadows and gradients.
* However it tends to produce artifacts on lower frequency images and is therefore not
* a good general purpose binarizer for uses outside ZXing.
*
* This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers,
* and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already
* inherently local, and only fails for horizontal gradients. We can revisit that problem later,
* but for now it was not a win to use local blocks for 1D.
*
* This Binarizer is the default for the unit tests and the recommended class for library users.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
export default class HybridBinarizer extends GlobalHistogramBinarizer {
// This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.
// So this is the smallest dimension in each axis we can accept.
private static BLOCK_SIZE_POWER = 3
private static BLOCK_SIZE = 1 << HybridBinarizer.BLOCK_SIZE_POWER // ...0100...00
private static BLOCK_SIZE_MASK = HybridBinarizer.BLOCK_SIZE - 1 // ...0011...11
private static MINIMUM_DIMENSION = HybridBinarizer.BLOCK_SIZE * 5
private static MIN_DYNAMIC_RANGE = 24
private matrix: BitMatrix|null = null
public constructor(source: LuminanceSource) {
super(source)
}
/**
* Calculates the final BitMatrix once for all requests. This could be called once from the
* constructor instead, but there are some advantages to doing it lazily, such as making
* profiling easier, and not doing heavy lifting when callers don't expect it.
*/
/*@Override*/
public getBlackMatrix(): BitMatrix /*throws NotFoundException*/ {
if (this.matrix !== null) {
return this.matrix
}
const source = this.getLuminanceSource()
const width = source.getWidth()
const height = source.getHeight()
if (width >= HybridBinarizer.MINIMUM_DIMENSION && height >= HybridBinarizer.MINIMUM_DIMENSION) {
const luminances = source.getMatrix()
let subWidth = width >> HybridBinarizer.BLOCK_SIZE_POWER
if ((width & HybridBinarizer.BLOCK_SIZE_MASK) != 0) {
subWidth++
}
let subHeight = height >> HybridBinarizer.BLOCK_SIZE_POWER
if ((height & HybridBinarizer.BLOCK_SIZE_MASK) != 0) {
subHeight++
}
const blackPoints = HybridBinarizer.calculateBlackPoints(luminances, subWidth, subHeight, width, height)
const newMatrix = new BitMatrix(width, height)
HybridBinarizer.calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix)
this.matrix = newMatrix
} else {
// If the image is too small, fall back to the global histogram approach.
this.matrix = super.getBlackMatrix()
}
return this.matrix
}
/*@Override*/
public createBinarizer(source: LuminanceSource): Binarizer {
return new HybridBinarizer(source)
}
/**
* For each block in the image, calculate the average black point using a 5x5 grid
* of the blocks around it. Also handles the corner cases (fractional blocks are computed based
* on the last pixels in the row/column which are also used in the previous block).
*/
private static calculateThresholdForBlock(luminances: Uint8ClampedArray,
subWidth: number/*int*/,
subHeight: number/*int*/,
width: number/*int*/,
height: number/*int*/,
blackPoints: Int32Array[],
matrix: BitMatrix): void {
const maxYOffset = height - HybridBinarizer.BLOCK_SIZE
const maxXOffset = width - HybridBinarizer.BLOCK_SIZE
for (let y = 0; y < subHeight; y++) {
let yoffset = y << HybridBinarizer.BLOCK_SIZE_POWER
if (yoffset > maxYOffset) {
yoffset = maxYOffset
}
const top = HybridBinarizer.cap(y, 2, subHeight - 3)
for (let x = 0; x < subWidth; x++) {
let xoffset = x << HybridBinarizer.BLOCK_SIZE_POWER
if (xoffset > maxXOffset) {
xoffset = maxXOffset
}
const left = HybridBinarizer.cap(x, 2, subWidth - 3)
let sum = 0
for (let z = -2; z <= 2; z++) {
const blackRow = blackPoints[top + z]
sum += blackRow[left - 2] + blackRow[left - 1] + blackRow[left] + blackRow[left + 1] + blackRow[left + 2]
}
const average = sum / 25
HybridBinarizer.thresholdBlock(luminances, xoffset, yoffset, average, width, matrix)
}
}
}
private static cap(value: number/*int*/, min: number/*int*/, max: number/*int*/): number/*int*/ {
return value < min ? min : value > max ? max : value
}
/**
* Applies a single threshold to a block of pixels.
*/
private static thresholdBlock(luminances: Uint8ClampedArray,
xoffset: number/*int*/,
yoffset: number/*int*/,
threshold: number/*int*/,
stride: number/*int*/,
matrix: BitMatrix): void {
for (let y = 0, offset = yoffset * stride + xoffset; y < HybridBinarizer.BLOCK_SIZE; y++, offset += stride) {
for (let x = 0; x < HybridBinarizer.BLOCK_SIZE; x++) {
// Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
if ((luminances[offset + x] & 0xFF) <= threshold) {
matrix.set(xoffset + x, yoffset + y)
}
}
}
}
/**
* Calculates a single black point for each block of pixels and saves it away.
* See the following thread for a discussion of this algorithm:
* http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
*/
private static calculateBlackPoints(luminances: Uint8ClampedArray,
subWidth: number/*int*/,
subHeight: number/*int*/,
width: number/*int*/,
height: number/*int*/): Int32Array[] {
const maxYOffset = height - HybridBinarizer.BLOCK_SIZE
const maxXOffset = width - HybridBinarizer.BLOCK_SIZE
const blackPoints = new Array<Int32Array>(subHeight)//subWidth
for (let y = 0; y < subHeight; y++) {
blackPoints[y] = new Int32Array(subWidth)
let yoffset = y << HybridBinarizer.BLOCK_SIZE_POWER
if (yoffset > maxYOffset) {
yoffset = maxYOffset
}
for (let x = 0; x < subWidth; x++) {
let xoffset = x << HybridBinarizer.BLOCK_SIZE_POWER
if (xoffset > maxXOffset) {
xoffset = maxXOffset
}
let sum = 0
let min = 0xFF
let max = 0
for (let yy = 0, offset = yoffset * width + xoffset; yy < HybridBinarizer.BLOCK_SIZE; yy++, offset += width) {
for (let xx = 0; xx < HybridBinarizer.BLOCK_SIZE; xx++) {
const pixel = luminances[offset + xx] & 0xFF
sum += pixel
// still looking for good contrast
if (pixel < min) {
min = pixel
}
if (pixel > max) {
max = pixel
}
}
// short-circuit min/max tests once dynamic range is met
if (max - min > HybridBinarizer.MIN_DYNAMIC_RANGE) {
// finish the rest of the rows quickly
for (yy++, offset += width; yy < HybridBinarizer.BLOCK_SIZE; yy++, offset += width) {
for (let xx = 0; xx < HybridBinarizer.BLOCK_SIZE; xx++) {
sum += luminances[offset + xx] & 0xFF
}
}
}
}
// The default estimate is the average of the values in the block.
let average = sum >> (HybridBinarizer.BLOCK_SIZE_POWER * 2);
if (max - min <= HybridBinarizer.MIN_DYNAMIC_RANGE) {
// If variation within the block is low, assume this is a block with only light or only
// dark pixels. In that case we do not want to use the average, as it would divide this
// low contrast area into black and white pixels, essentially creating data out of noise.
//
// The default assumption is that the block is light/background. Since no estimate for
// the level of dark pixels exists locally, use half the min for the block.
average = min / 2
if (y > 0 && x > 0) {
// Correct the "white background" assumption for blocks that have neighbors by comparing
// the pixels in this block to the previously calculated black points. This is based on
// the fact that dark barcode symbology is always surrounded by some amount of light
// background for which reasonable black point estimates were made. The bp estimated at
// the boundaries is used for the interior.
// The (min < bp) is arbitrary but works better than other heuristics that were tried.
const averageNeighborBlackPoint =
(blackPoints[y - 1][x] + (2 * blackPoints[y][x - 1]) + blackPoints[y - 1][x - 1]) / 4;
if (min < averageNeighborBlackPoint) {
average = averageNeighborBlackPoint
}
}
}
blackPoints[y][x] = average
}
}
return blackPoints
}
}

View file

@ -0,0 +1,145 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
/**
* <p>This class implements a perspective transform in two dimensions. Given four source and four
* destination points, it will compute the transformation implied between them. The code is based
* directly upon section 3.4.2 of George Wolberg's "Digital Image Warping"; see pages 54-56.</p>
*
* @author Sean Owen
*/
export default class PerspectiveTransform {
private constructor(private a11: number/*float*/, private a21: number/*float*/, private a31: number/*float*/,
private a12: number/*float*/, private a22: number/*float*/, private a32: number/*float*/,
private a13: number/*float*/, private a23: number/*float*/, private a33: number/*float*/) {}
public static quadrilateralToQuadrilateral(x0: number/*float*/, y0: number/*float*/,
x1: number/*float*/, y1: number/*float*/,
x2: number/*float*/, y2: number/*float*/,
x3: number/*float*/, y3: number/*float*/,
x0p: number/*float*/, y0p: number/*float*/,
x1p: number/*float*/, y1p: number/*float*/,
x2p: number/*float*/, y2p: number/*float*/,
x3p: number/*float*/, y3p: number/*float*/): PerspectiveTransform {
const qToS = PerspectiveTransform.quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3)
const sToQ = PerspectiveTransform.squareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p)
return sToQ.times(qToS)
}
public transformPoints(points: Float32Array): void {
const max = points.length
const a11 = this.a11
const a12 = this.a12
const a13 = this.a13
const a21 = this.a21
const a22 = this.a22
const a23 = this.a23
const a31 = this.a31
const a32 = this.a32
const a33 = this.a33
for (let i = 0; i < max; i += 2) {
const x = points[i]
const y = points[i + 1]
const denominator = a13 * x + a23 * y + a33;
points[i] = (a11 * x + a21 * y + a31) / denominator;
points[i + 1] = (a12 * x + a22 * y + a32) / denominator;
}
}
public transformPointsWithValues(xValues: Float32Array, yValues: Float32Array): void {
const a11 = this.a11
const a12 = this.a12
const a13 = this.a13
const a21 = this.a21
const a22 = this.a22
const a23 = this.a23
const a31 = this.a31
const a32 = this.a32
const a33 = this.a33
const n = xValues.length
for (let i = 0; i < n; i ++) {
const x = xValues[i]
const y = yValues[i]
const denominator = a13 * x + a23 * y + a33;
xValues[i] = (a11 * x + a21 * y + a31) / denominator;
yValues[i] = (a12 * x + a22 * y + a32) / denominator;
}
}
public static squareToQuadrilateral(x0: number/*float*/, y0: number/*float*/,
x1: number/*float*/, y1: number/*float*/,
x2: number/*float*/, y2: number/*float*/,
x3: number/*float*/, y3: number/*float*/): PerspectiveTransform {
const dx3 = x0 - x1 + x2 - x3
const dy3 = y0 - y1 + y2 - y3
if (dx3 === 0.0 && dy3 === 0.0) {
// Affine
return new PerspectiveTransform(x1 - x0, x2 - x1, x0,
y1 - y0, y2 - y1, y0,
0.0, 0.0, 1.0)
} else {
const dx1 = x1 - x2
const dx2 = x3 - x2
const dy1 = y1 - y2
const dy2 = y3 - y2
const denominator = dx1 * dy2 - dx2 * dy1;
const a13 = (dx3 * dy2 - dx2 * dy3) / denominator;
const a23 = (dx1 * dy3 - dx3 * dy1) / denominator;
return new PerspectiveTransform(x1 - x0 + a13 * x1, x3 - x0 + a23 * x3, x0,
y1 - y0 + a13 * y1, y3 - y0 + a23 * y3, y0,
a13, a23, 1.0)
}
}
public static quadrilateralToSquare(x0: number/*float*/, y0: number/*float*/,
x1: number/*float*/, y1: number/*float*/,
x2: number/*float*/, y2: number/*float*/,
x3: number/*float*/, y3: number/*float*/): PerspectiveTransform {
// Here, the adjoint serves as the inverse:
return PerspectiveTransform.squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3).buildAdjoint()
}
protected buildAdjoint(): PerspectiveTransform {
// Adjoint is the transpose of the cofactor matrix:
return new PerspectiveTransform(this.a22 * this.a33 - this.a23 * this.a32,
this.a23 * this.a31 - this.a21 * this.a33,
this.a21 * this.a32 - this.a22 * this.a31,
this.a13 * this.a32 - this.a12 * this.a33,
this.a11 * this.a33 - this.a13 * this.a31,
this.a12 * this.a31 - this.a11 * this.a32,
this.a12 * this.a23 - this.a13 * this.a22,
this.a13 * this.a21 - this.a11 * this.a23,
this.a11 * this.a22 - this.a12 * this.a21);
}
protected times(other: PerspectiveTransform): PerspectiveTransform {
return new PerspectiveTransform(this.a11 * other.a11 + this.a21 * other.a12 + this.a31 * other.a13,
this.a11 * other.a21 + this.a21 * other.a22 + this.a31 * other.a23,
this.a11 * other.a31 + this.a21 * other.a32 + this.a31 * other.a33,
this.a12 * other.a11 + this.a22 * other.a12 + this.a32 * other.a13,
this.a12 * other.a21 + this.a22 * other.a22 + this.a32 * other.a23,
this.a12 * other.a31 + this.a22 * other.a32 + this.a32 * other.a33,
this.a13 * other.a11 + this.a23 * other.a12 + this.a33 * other.a13,
this.a13 * other.a21 + this.a23 * other.a22 + this.a33 * other.a23,
this.a13 * other.a31 + this.a23 * other.a32 + this.a33 * other.a33);
}
}

View file

@ -0,0 +1,211 @@
/*
* Copyright (C) 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common {*/
/*import java.nio.charset.Charset;*/
/*import java.util.Map;*/
import DecodeHintType from './../DecodeHintType'
import CharacterSetECI from './CharacterSetECI'
/**
* Common string-related functions.
*
* @author Sean Owen
* @author Alex Dupre
*/
export default class StringUtils {
public static SHIFT_JIS = CharacterSetECI.SJIS.getName()//"SJIS"
public static GB2312 = "GB2312"
private static EUC_JP = "EUC_JP"
private static UTF8 = CharacterSetECI.UTF8.getName()//"UTF8"
private static PLATFORM_DEFAULT_ENCODING = StringUtils.UTF8//"UTF8"//Charset.defaultCharset().name()
private static ISO88591 = CharacterSetECI.ISO8859_1.getName()//"ISO8859_1"
private static ASSUME_SHIFT_JIS = false
// SHIFT_JIS.equalsIgnoreCase(PLATFORM_DEFAULT_ENCODING) ||
// EUC_JP.equalsIgnoreCase(PLATFORM_DEFAULT_ENCODING);
private StringUtils() {}
/**
* @param bytes bytes encoding a string, whose encoding should be guessed
* @param hints decode hints if applicable
* @return name of guessed encoding; at the moment will only guess one of:
* {@link #SHIFT_JIS}, {@link #UTF8}, {@link #ISO88591}, or the platform
* default encoding if none of these can possibly be correct
*/
public static guessEncoding(bytes: Uint8Array, hints: Map<DecodeHintType, any>): string {
if (hints !== null && hints !== undefined && undefined !== hints.get(DecodeHintType.CHARACTER_SET)) {
return hints.get(DecodeHintType.CHARACTER_SET).toString()
}
// For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS,
// which should be by far the most common encodings.
const length = bytes.length
let canBeISO88591 = true
let canBeShiftJIS = true
let canBeUTF8 = true
let utf8BytesLeft = 0
//int utf8LowChars = 0
let utf2BytesChars = 0
let utf3BytesChars = 0
let utf4BytesChars = 0
let sjisBytesLeft = 0
//int sjisLowChars = 0
let sjisKatakanaChars = 0
//int sjisDoubleBytesChars = 0
let sjisCurKatakanaWordLength = 0
let sjisCurDoubleBytesWordLength = 0
let sjisMaxKatakanaWordLength = 0
let sjisMaxDoubleBytesWordLength = 0
//int isoLowChars = 0
//int isoHighChars = 0
let isoHighOther = 0
const utf8bom = bytes.length > 3 &&
bytes[0] == /*(byte) */0xEF &&
bytes[1] == /*(byte) */0xBB &&
bytes[2] == /*(byte) */0xBF
for (let i = 0;
i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8);
i++) {
const value = bytes[i] & 0xFF
// UTF-8 stuff
if (canBeUTF8) {
if (utf8BytesLeft > 0) {
if ((value & 0x80) == 0) {
canBeUTF8 = false
} else {
utf8BytesLeft--
}
} else if ((value & 0x80) != 0) {
if ((value & 0x40) == 0) {
canBeUTF8 = false
} else {
utf8BytesLeft++
if ((value & 0x20) == 0) {
utf2BytesChars++
} else {
utf8BytesLeft++
if ((value & 0x10) == 0) {
utf3BytesChars++
} else {
utf8BytesLeft++
if ((value & 0x08) == 0) {
utf4BytesChars++
} else {
canBeUTF8 = false
}
}
}
}
} //else {
//utf8LowChars++
//}
}
// ISO-8859-1 stuff
if (canBeISO88591) {
if (value > 0x7F && value < 0xA0) {
canBeISO88591 = false
} else if (value > 0x9F) {
if (value < 0xC0 || value == 0xD7 || value == 0xF7) {
isoHighOther++
} //else {
//isoHighChars++
//}
} //else {
//isoLowChars++
//}
}
// Shift_JIS stuff
if (canBeShiftJIS) {
if (sjisBytesLeft > 0) {
if (value < 0x40 || value == 0x7F || value > 0xFC) {
canBeShiftJIS = false
} else {
sjisBytesLeft--
}
} else if (value == 0x80 || value == 0xA0 || value > 0xEF) {
canBeShiftJIS = false
} else if (value > 0xA0 && value < 0xE0) {
sjisKatakanaChars++
sjisCurDoubleBytesWordLength = 0
sjisCurKatakanaWordLength++
if (sjisCurKatakanaWordLength > sjisMaxKatakanaWordLength) {
sjisMaxKatakanaWordLength = sjisCurKatakanaWordLength
}
} else if (value > 0x7F) {
sjisBytesLeft++
//sjisDoubleBytesChars++
sjisCurKatakanaWordLength = 0
sjisCurDoubleBytesWordLength++
if (sjisCurDoubleBytesWordLength > sjisMaxDoubleBytesWordLength) {
sjisMaxDoubleBytesWordLength = sjisCurDoubleBytesWordLength
}
} else {
//sjisLowChars++
sjisCurKatakanaWordLength = 0
sjisCurDoubleBytesWordLength = 0
}
}
}
if (canBeUTF8 && utf8BytesLeft > 0) {
canBeUTF8 = false
}
if (canBeShiftJIS && sjisBytesLeft > 0) {
canBeShiftJIS = false
}
// Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done
if (canBeUTF8 && (utf8bom || utf2BytesChars + utf3BytesChars + utf4BytesChars > 0)) {
return StringUtils.UTF8
}
// Easy -- if assuming Shift_JIS or at least 3 valid consecutive not-ascii characters (and no evidence it can't be), done
if (canBeShiftJIS && (StringUtils.ASSUME_SHIFT_JIS || sjisMaxKatakanaWordLength >= 3 || sjisMaxDoubleBytesWordLength >= 3)) {
return StringUtils.SHIFT_JIS
}
// Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is:
// - If we saw
// - only two consecutive katakana chars in the whole text, or
// - at least 10% of bytes that could be "upper" not-alphanumeric Latin1,
// - then we conclude Shift_JIS, else ISO-8859-1
if (canBeISO88591 && canBeShiftJIS) {
return (sjisMaxKatakanaWordLength == 2 && sjisKatakanaChars == 2) || isoHighOther * 10 >= length
? StringUtils.SHIFT_JIS : StringUtils.ISO88591
}
// Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding
if (canBeISO88591) {
return StringUtils.ISO88591
}
if (canBeShiftJIS) {
return StringUtils.SHIFT_JIS
}
if (canBeUTF8) {
return StringUtils.UTF8
}
// Otherwise, we take a wild guess with platform encoding
return StringUtils.PLATFORM_DEFAULT_ENCODING
}
}

View file

@ -0,0 +1,83 @@
/*
* Copyright 2012 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common.detector {*/
/**
* General math-related and numeric utility functions.
*/
export default class MathUtils {
private MathUtils() {
}
/**
* Ends up being a bit faster than {@link Math#round(float)}. This merely rounds its
* argument to the nearest int, where x.5 rounds up to x+1. Semantics of this shortcut
* differ slightly from {@link Math#round(float)} in that half rounds down for negative
* values. -2.5 rounds to -3, not -2. For purposes here it makes no difference.
*
* @param d real value to round
* @return nearest {@code int}
*/
public static round(d: number/*float*/): number/*int*/ {
if (NaN === d) return 0
if (d <= Number.MIN_SAFE_INTEGER) return Number.MIN_SAFE_INTEGER
if (d >= Number.MAX_SAFE_INTEGER) return Number.MAX_SAFE_INTEGER
return /*(int) */(d + (d < 0.0 ? -0.5 : 0.5)) | 0
}
// TYPESCRIPTPORT: maybe remove round method and call directly Math.round, it looks like it doesn't make sense for js
/**
* @param aX point A x coordinate
* @param aY point A y coordinate
* @param bX point B x coordinate
* @param bY point B y coordinate
* @return Euclidean distance between points A and B
*/
public static distance(aX: number/*float|int*/, aY: number/*float|int*/, bX: number/*float|int*/, bY: number/*float|int*/): number/*float*/ {
const xDiff = aX - bX
const yDiff = aY - bY
return /*(float) */Math.sqrt(xDiff * xDiff + yDiff * yDiff)
}
/**
* @param aX point A x coordinate
* @param aY point A y coordinate
* @param bX point B x coordinate
* @param bY point B y coordinate
* @return Euclidean distance between points A and B
*/
// public static distance(aX: number/*int*/, aY: number/*int*/, bX: number/*int*/, bY: number/*int*/): float {
// const xDiff = aX - bX
// const yDiff = aY - bY
// return (float) Math.sqrt(xDiff * xDiff + yDiff * yDiff);
// }
/**
* @param array values to sum
* @return sum of values in array
*/
public static sum(array: Int32Array): number/*int*/ {
let count = 0
for (let i = 0, length = array.length; i != length; i++) {
const a = array[i]
count += a
}
return count
}
}

View file

@ -0,0 +1,216 @@
// /*
// * Copyright 2009 ZXing authors
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
// /*namespace com.google.zxing.common.detector {*/
// import ResultPoint from './../../ResultPoint'
// import BitMatrix from './../BitMatrix'
// /**
// * <p>A somewhat generic detector that looks for a barcode-like rectangular region within an image.
// * It looks within a mostly white region of an image for a region of black and white, but mostly
// * black. It returns the four corners of the region, as best it can determine.</p>
// *
// * @author Sean Owen
// * @deprecated without replacement since 3.3.0
// */
// @Deprecated
// export default class MonochromeRectangleDetector {
// private 32: static final int MAX_MODULES =
// private image: BitMatrix
// public constructor(image: BitMatrix) {
// this.image = image
// }
// /**
// * <p>Detects a rectangular region of black and white -- mostly black -- with a region of mostly
// * white, in an image.</p>
// *
// * @return {@link ResultPoint}[] describing the corners of the rectangular region. The first and
// * last points are opposed on the diagonal, as are the second and third. The first point will be
// * the topmost point and the last, the bottommost. The second point will be leftmost and the
// * third, the rightmost
// * @throws NotFoundException if no Data Matrix Code can be found
// */
// public detect(): ResultPoInt32Array /*throws NotFoundException*/ {
// height: number/*int*/ = image.getHeight();
// width: number/*int*/ = image.getWidth();
// const halfHeight = height / 2
// const halfWidth = width / 2
// const deltaY = Math.max(1, height / (MAX_MODULES * 8));
// const deltaX = Math.max(1, width / (MAX_MODULES * 8));
// const top = 0
// const bottom = height
// const left = 0
// const right = width
// ResultPoint pointA = findCornerFromCenter(halfWidth, 0, left, right,
// halfHeight, -deltaY, top, bottom, halfWidth / 2)
// top = (int) pointA.getY() - 1
// ResultPoint pointB = findCornerFromCenter(halfWidth, -deltaX, left, right,
// halfHeight, 0, top, bottom, halfHeight / 2)
// left = (int) pointB.getX() - 1
// ResultPoint pointC = findCornerFromCenter(halfWidth, deltaX, left, right,
// halfHeight, 0, top, bottom, halfHeight / 2)
// right = (int) pointC.getX() + 1
// ResultPoint pointD = findCornerFromCenter(halfWidth, 0, left, right,
// halfHeight, deltaY, top, bottom, halfWidth / 2)
// bottom = (int) pointD.getY() + 1
// // Go try to find point A again with better information -- might have been off at first.
// pointA = findCornerFromCenter(halfWidth, 0, left, right,
// halfHeight, -deltaY, top, bottom, halfWidth / 4)
// return new ResultPoInt32Array { pointA, pointB, pointC, pointD }
// }
// /**
// * Attempts to locate a corner of the barcode by scanning up, down, left or right from a center
// * point which should be within the barcode.
// *
// * @param centerX center's x component (horizontal)
// * @param deltaX same as deltaY but change in x per step instead
// * @param left minimum value of x
// * @param right maximum value of x
// * @param centerY center's y component (vertical)
// * @param deltaY change in y per step. If scanning up this is negative; down, positive;
// * left or right, 0
// * @param top minimum value of y to search through (meaningless when di == 0)
// * @param bottom maximum value of y
// * @param maxWhiteRun maximum run of white pixels that can still be considered to be within
// * the barcode
// * @return a {@link ResultPoint} encapsulating the corner that was found
// * @throws NotFoundException if such a point cannot be found
// */
// private ResultPoint findCornerFromCenter(centerX: number/*int*/,
// deltaX: number/*int*/,
// left: number/*int*/,
// right: number/*int*/,
// centerY: number/*int*/,
// deltaY: number/*int*/,
// top: number/*int*/,
// bottom: number/*int*/,
// maxWhiteRun: number/*int*/) /*throws NotFoundException*/ {
// const lastRange: Int32Array = null
// for (let y = centerY, x = centerX
// y < bottom && y >= top && x < right && x >= left
// y += deltaY, x += deltaX) {
// const range: Int32Array
// if (deltaX == 0) {
// // horizontal slices, up and down
// range = blackWhiteRange(y, maxWhiteRun, left, right, true)
// } else {
// // vertical slices, left and right
// range = blackWhiteRange(x, maxWhiteRun, top, bottom, false)
// }
// if (range == null) {
// if (lastRange == null) {
// throw NotFoundException.getNotFoundInstance()
// }
// // lastRange was found
// if (deltaX == 0) {
// const lastY = y - deltaY
// if (lastRange[0] < centerX) {
// if (lastRange[1] > centerX) {
// // straddle, choose one or the other based on direction
// return new ResultPoint(lastRange[deltaY > 0 ? 0 : 1], lastY)
// }
// return new ResultPoint(lastRange[0], lastY)
// } else {
// return new ResultPoint(lastRange[1], lastY)
// }
// } else {
// const lastX = x - deltaX
// if (lastRange[0] < centerY) {
// if (lastRange[1] > centerY) {
// return new ResultPoint(lastX, lastRange[deltaX < 0 ? 0 : 1])
// }
// return new ResultPoint(lastX, lastRange[0])
// } else {
// return new ResultPoint(lastX, lastRange[1])
// }
// }
// }
// lastRange = range
// }
// throw NotFoundException.getNotFoundInstance()
// }
// /**
// * Computes the start and end of a region of pixels, either horizontally or vertically, that could
// * be part of a Data Matrix barcode.
// *
// * @param fixedDimension if scanning horizontally, this is the row (the fixed vertical location)
// * where we are scanning. If scanning vertically it's the column, the fixed horizontal location
// * @param maxWhiteRun largest run of white pixels that can still be considered part of the
// * barcode region
// * @param minDim minimum pixel location, horizontally or vertically, to consider
// * @param maxDim maximum pixel location, horizontally or vertically, to consider
// * @param horizontal if true, we're scanning left-right, instead of up-down
// * @return const with: Int32Array start and end of found range, or null if no such range is found
// * (e.g. only white was found)
// */
// private const blackWhiteRange: Int32Array(fixedDimension: number/*int*/, maxWhiteRun: number/*int*/, minDim: number/*int*/, maxDim: number/*int*/, boolean horizontal) {
// const center = (minDim + maxDim) / 2
// // Scan left/up first
// const start = center
// while (start >= minDim) {
// if (horizontal ? image.get(start, fixedDimension) : image.get(fixedDimension, start)) {
// start--
// } else {
// const whiteRunStart = start
// do {
// start--
// } while (start >= minDim && !(horizontal ? image.get(start, fixedDimension) :
// image.get(fixedDimension, start)))
// const whiteRunSize = whiteRunStart - start
// if (start < minDim || whiteRunSize > maxWhiteRun) {
// start = whiteRunStart
// break
// }
// }
// }
// start++
// // Then try right/down
// const end = center
// while (end < maxDim) {
// if (horizontal ? image.get(end, fixedDimension) : image.get(fixedDimension, end)) {
// end++
// } else {
// const whiteRunStart = end
// do {
// end++
// } while (end < maxDim && !(horizontal ? image.get(end, fixedDimension) :
// image.get(fixedDimension, end)))
// const whiteRunSize = end - whiteRunStart
// if (end >= maxDim || whiteRunSize > maxWhiteRun) {
// end = whiteRunStart
// break
// }
// }
// }
// end--
// return end > start ? new Int32Array{start, end} : null
// }
// }

View file

@ -0,0 +1,347 @@
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common.detector {*/
import ResultPoint from './../../ResultPoint'
import BitMatrix from './../BitMatrix'
import Exception from './../../Exception'
import MathUtils from './MathUtils'
/**
* <p>
* Detects a candidate barcode-like rectangular region within an image. It
* starts around the center of the image, increases the size of the candidate
* region until it finds a white rectangular region. By keeping track of the
* last black points it encountered, it determines the corners of the barcode.
* </p>
*
* @author David Olivier
*/
export default class WhiteRectangleDetector {
private static INIT_SIZE = 10;
private static CORR = 1;
private height: number/*int*/
private width: number/*int*/
private leftInit: number/*int*/
private rightInit: number/*int*/
private downInit: number/*int*/
private upInit: number/*int*/
// public constructor(private image: BitMatrix) /*throws NotFoundException*/ {
// this(image, INIT_SIZE, image.getWidth() / 2, image.getHeight() / 2)
// }
/**
* @param image barcode image to find a rectangle in
* @param initSize initial size of search area around center
* @param x x position of search center
* @param y y position of search center
* @throws NotFoundException if image is too small to accommodate {@code initSize}
*/
public constructor(private image: BitMatrix, initSize?: number/*int*/, x?: number/*int*/, y?: number/*int*/) /*throws NotFoundException*/ {
this.height = image.getHeight()
this.width = image.getWidth()
if (undefined === initSize || null === initSize) {
initSize = WhiteRectangleDetector.INIT_SIZE
}
if (undefined === x || null === x) {
x = image.getWidth() / 2
}
if (undefined === y || null === y) {
y = image.getHeight() / 2
}
const halfsize = initSize / 2
this.leftInit = x - halfsize
this.rightInit = x + halfsize
this.upInit = y - halfsize
this.downInit = y + halfsize
if (this.upInit < 0 || this.leftInit < 0 || this.downInit >= this.height || this.rightInit >= this.width) {
throw new Exception(Exception.NotFoundException)
}
}
/**
* <p>
* Detects a candidate barcode-like rectangular region within an image. It
* starts around the center of the image, increases the size of the candidate
* region until it finds a white rectangular region.
* </p>
*
* @return {@link ResultPoint}[] describing the corners of the rectangular
* region. The first and last points are opposed on the diagonal, as
* are the second and third. The first point will be the topmost
* point and the last, the bottommost. The second point will be
* leftmost and the third, the rightmost
* @throws NotFoundException if no Data Matrix Code can be found
*/
public detect(): Array<ResultPoint> /*throws NotFoundException*/ {
let left = this.leftInit
let right = this.rightInit
let up = this.upInit
let down = this.downInit
let sizeExceeded: boolean = false
let aBlackPointFoundOnBorder: boolean = true
let atLeastOneBlackPointFoundOnBorder: boolean = false
let atLeastOneBlackPointFoundOnRight: boolean = false
let atLeastOneBlackPointFoundOnBottom: boolean = false
let atLeastOneBlackPointFoundOnLeft: boolean = false
let atLeastOneBlackPointFoundOnTop: boolean = false
const width = this.width
const height = this.height
while (aBlackPointFoundOnBorder) {
aBlackPointFoundOnBorder = false
// .....
// . |
// .....
let rightBorderNotWhite: boolean = true
while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) && right < width) {
rightBorderNotWhite = this.containsBlackPoint(up, down, right, false)
if (rightBorderNotWhite) {
right++
aBlackPointFoundOnBorder = true
atLeastOneBlackPointFoundOnRight = true
} else if (!atLeastOneBlackPointFoundOnRight) {
right++
}
}
if (right >= width) {
sizeExceeded = true
break
}
// .....
// . .
// .___.
let bottomBorderNotWhite: boolean = true
while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) && down < height) {
bottomBorderNotWhite = this.containsBlackPoint(left, right, down, true)
if (bottomBorderNotWhite) {
down++
aBlackPointFoundOnBorder = true
atLeastOneBlackPointFoundOnBottom = true
} else if (!atLeastOneBlackPointFoundOnBottom) {
down++
}
}
if (down >= height) {
sizeExceeded = true
break
}
// .....
// | .
// .....
let leftBorderNotWhite: boolean = true
while ((leftBorderNotWhite || !atLeastOneBlackPointFoundOnLeft) && left >= 0) {
leftBorderNotWhite = this.containsBlackPoint(up, down, left, false)
if (leftBorderNotWhite) {
left--
aBlackPointFoundOnBorder = true
atLeastOneBlackPointFoundOnLeft = true
} else if (!atLeastOneBlackPointFoundOnLeft) {
left--
}
}
if (left < 0) {
sizeExceeded = true
break
}
// .___.
// . .
// .....
let topBorderNotWhite: boolean = true
while ((topBorderNotWhite || !atLeastOneBlackPointFoundOnTop) && up >= 0) {
topBorderNotWhite = this.containsBlackPoint(left, right, up, true)
if (topBorderNotWhite) {
up--
aBlackPointFoundOnBorder = true
atLeastOneBlackPointFoundOnTop = true
} else if (!atLeastOneBlackPointFoundOnTop) {
up--
}
}
if (up < 0) {
sizeExceeded = true
break
}
if (aBlackPointFoundOnBorder) {
atLeastOneBlackPointFoundOnBorder = true
}
}
if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) {
const maxSize = right - left
let z: ResultPoint|null = null
for (let i = 1; z === null && i < maxSize; i++) {
z = this.getBlackPointOnSegment(left, down - i, left + i, down)
}
if (z == null) {
throw new Exception(Exception.NotFoundException)
}
let t: ResultPoint|null = null
//go down right
for (let i = 1; t === null && i < maxSize; i++) {
t = this.getBlackPointOnSegment(left, up + i, left + i, up)
}
if (t == null) {
throw new Exception(Exception.NotFoundException)
}
let x: ResultPoint|null = null
//go down left
for (let i = 1; x === null && i < maxSize; i++) {
x = this.getBlackPointOnSegment(right, up + i, right - i, up)
}
if (x == null) {
throw new Exception(Exception.NotFoundException)
}
let y: ResultPoint|null = null
//go up left
for (let i = 1; y === null && i < maxSize; i++) {
y = this.getBlackPointOnSegment(right, down - i, right - i, down)
}
if (y == null) {
throw new Exception(Exception.NotFoundException)
}
return this.centerEdges(y, z, x, t)
} else {
throw new Exception(Exception.NotFoundException)
}
}
private getBlackPointOnSegment(aX: number/*float*/, aY: number/*float*/, bX: number/*float*/, bY: number/*float*/): ResultPoint|null {
const dist = MathUtils.round(MathUtils.distance(aX, aY, bX, bY))
const xStep: number/*float*/ = (bX - aX) / dist
const yStep: number/*float*/ = (bY - aY) / dist
const image = this.image
for (let i = 0; i < dist; i++) {
const x = MathUtils.round(aX + i * xStep);
const y = MathUtils.round(aY + i * yStep);
if (image.get(x, y)) {
return new ResultPoint(x, y)
}
}
return null
}
/**
* recenters the points of a constant distance towards the center
*
* @param y bottom most point
* @param z left most point
* @param x right most point
* @param t top most point
* @return {@link ResultPoint}[] describing the corners of the rectangular
* region. The first and last points are opposed on the diagonal, as
* are the second and third. The first point will be the topmost
* point and the last, the bottommost. The second point will be
* leftmost and the third, the rightmost
*/
private centerEdges(y: ResultPoint, z: ResultPoint,
x: ResultPoint, t: ResultPoint): Array<ResultPoint> {
//
// t t
// z x
// x OR z
// y y
//
const yi: number/*float*/ = y.getX()
const yj: number/*float*/ = y.getY()
const zi: number/*float*/ = z.getX()
const zj: number/*float*/ = z.getY()
const xi: number/*float*/ = x.getX()
const xj: number/*float*/ = x.getY()
const ti: number/*float*/ = t.getX()
const tj: number/*float*/ = t.getY()
const CORR = WhiteRectangleDetector.CORR
if (yi < this.width / 2.0) {
return [
new ResultPoint(ti - CORR, tj + CORR),
new ResultPoint(zi + CORR, zj + CORR),
new ResultPoint(xi - CORR, xj - CORR),
new ResultPoint(yi + CORR, yj - CORR)]
} else {
return [
new ResultPoint(ti + CORR, tj + CORR),
new ResultPoint(zi + CORR, zj - CORR),
new ResultPoint(xi - CORR, xj + CORR),
new ResultPoint(yi - CORR, yj - CORR)]
}
}
/**
* Determines whether a segment contains a black point
*
* @param a min value of the scanned coordinate
* @param b max value of the scanned coordinate
* @param fixed value of fixed coordinate
* @param horizontal set to true if scan must be horizontal, false if vertical
* @return true if a black point has been found, else false.
*/
private containsBlackPoint(a: number/*int*/, b: number/*int*/, fixed: number/*int*/, horizontal: boolean): boolean {
const image = this.image
if (horizontal) {
for (let x = a; x <= b; x++) {
if (image.get(x, fixed)) {
return true
}
}
} else {
for (let y = a; y <= b; y++) {
if (image.get(fixed, y)) {
return true
}
}
}
return false
}
}

View file

@ -0,0 +1,171 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common.reedsolomon {*/
import GenericGFPoly from './GenericGFPoly'
import Exception from './../../Exception'
import Integer from './../../util/Integer'
/**
* <p>This class contains utility methods for performing mathematical operations over
* the Galois Fields. Operations use a given primitive polynomial in calculations.</p>
*
* <p>Throughout this package, elements of the GF are represented as an {@code int}
* for convenience and speed (but at the cost of memory).
* </p>
*
* @author Sean Owen
* @author David Olivier
*/
export default class GenericGF {
public static AZTEC_DATA_12 = new GenericGF(0x1069, 4096, 1); // x^12 + x^6 + x^5 + x^3 + 1
public static AZTEC_DATA_10 = new GenericGF(0x409, 1024, 1); // x^10 + x^3 + 1
public static AZTEC_DATA_6 = new GenericGF(0x43, 64, 1); // x^6 + x + 1
public static AZTEC_PARAM = new GenericGF(0x13, 16, 1); // x^4 + x + 1
public static QR_CODE_FIELD_256 = new GenericGF(0x011D, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1
public static DATA_MATRIX_FIELD_256 = new GenericGF(0x012D, 256, 1); // x^8 + x^5 + x^3 + x^2 + 1
public static AZTEC_DATA_8 = GenericGF.DATA_MATRIX_FIELD_256;
public static MAXICODE_FIELD_64 = GenericGF.AZTEC_DATA_6;
private expTable: Int32Array
private logTable: Int32Array
private zero: GenericGFPoly
private one: GenericGFPoly
/**
* Create a representation of GF(size) using the given primitive polynomial.
*
* @param primitive irreducible polynomial whose coefficients are represented by
* the bits of an int, where the least-significant bit represents the constant
* coefficient
* @param size the size of the field
* @param b the factor b in the generator polynomial can be 0- or 1-based
* (g(x) = (x+a^b)(x+a^(b+1))...(x+a^(b+2t-1))).
* In most cases it should be 1, but for QR code it is 0.
*/
public constructor(private primitive: number/*int*/, private size: number/*int*/, private generatorBase: number/*int*/) {
const expTable = new Int32Array(size)
let x = 1
for (let i = 0; i < size; i++) {
expTable[i] = x
x *= 2; // we're assuming the generator alpha is 2
if (x >= size) {
x ^= primitive
x &= size - 1
}
}
this.expTable = expTable
const logTable = new Int32Array(size)
for (let i = 0; i < size - 1; i++) {
logTable[expTable[i]] = i
}
this.logTable = logTable
// logTable[0] == 0 but this should never be used
this.zero = new GenericGFPoly(this, Int32Array.from([0]))
this.one = new GenericGFPoly(this, Int32Array.from([1]))
}
public getZero(): GenericGFPoly {
return this.zero
}
public getOne(): GenericGFPoly {
return this.one
}
/**
* @return the monomial representing coefficient * x^degree
*/
public buildMonomial(degree: number/*int*/, coefficient: number/*int*/): GenericGFPoly {
if (degree < 0) {
throw new Exception(Exception.IllegalArgumentException)
}
if (coefficient === 0) {
return this.zero
}
const coefficients = new Int32Array(degree + 1)
coefficients[0] = coefficient
return new GenericGFPoly(this, coefficients)
}
/**
* Implements both addition and subtraction -- they are the same in GF(size).
*
* @return sum/difference of a and b
*/
public static addOrSubtract(a: number/*int*/, b: number/*int*/): number/*int*/ {
return a ^ b
}
/**
* @return 2 to the power of a in GF(size)
*/
public exp(a: number/*int*/): number/*int*/ {
return this.expTable[a]
}
/**
* @return base 2 log of a in GF(size)
*/
public log(a: number/*int*/): number/*int*/ {
if (a === 0) {
throw new Exception(Exception.IllegalArgumentException)
}
return this.logTable[a]
}
/**
* @return multiplicative inverse of a
*/
public inverse(a: number/*int*/): number/*int*/ {
if (a === 0) {
throw new Exception(Exception.ArithmeticException)
}
return this.expTable[this.size - this.logTable[a] - 1]
}
/**
* @return product of a and b in GF(size)
*/
public multiply(a: number/*int*/, b: number/*int*/): number/*int*/ {
if (a === 0 || b === 0) {
return 0
}
return this.expTable[(this.logTable[a] + this.logTable[b]) % (this.size - 1)]
}
public getSize(): number/*int*/ {
return this.size
}
public getGeneratorBase(): number/*int*/ {
return this.generatorBase
}
/*@Override*/
public toString(): string {
return "GF(0x" + Integer.toHexString(this.primitive) + ',' + this.size + ')'
}
public equals(o: Object): boolean {
return o === this
}
}

View file

@ -0,0 +1,279 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common.reedsolomon {*/
import GenericGF from './GenericGF'
import Exception from './../../Exception'
import System from './../../util/System'
/**
* <p>Represents a polynomial whose coefficients are elements of a GF.
* Instances of this class are immutable.</p>
*
* <p>Much credit is due to William Rucklidge since portions of this code are an indirect
* port of his C++ Reed-Solomon implementation.</p>
*
* @author Sean Owen
*/
export default class GenericGFPoly {
private field: GenericGF
private coefficients: Int32Array
/**
* @param field the {@link GenericGF} instance representing the field to use
* to perform computations
* @param coefficients coefficients as ints representing elements of GF(size), arranged
* from most significant (highest-power term) coefficient to least significant
* @throws IllegalArgumentException if argument is null or empty,
* or if leading coefficient is 0 and this is not a
* constant polynomial (that is, it is not the monomial "0")
*/
public constructor(field: GenericGF, coefficients: Int32Array) {
if (coefficients.length === 0) {
throw new Exception(Exception.IllegalArgumentException)
}
this.field = field
const coefficientsLength = coefficients.length
if (coefficientsLength > 1 && coefficients[0] === 0) {
// Leading term must be non-zero for anything except the constant polynomial "0"
let firstNonZero = 1
while (firstNonZero < coefficientsLength && coefficients[firstNonZero] === 0) {
firstNonZero++
}
if (firstNonZero === coefficientsLength) {
this.coefficients = Int32Array.from([0])
} else {
this.coefficients = new Int32Array(coefficientsLength - firstNonZero)
System.arraycopy(coefficients,
firstNonZero,
this.coefficients,
0,
this.coefficients.length)
}
} else {
this.coefficients = coefficients
}
}
public getCoefficients(): Int32Array {
return this.coefficients
}
/**
* @return degree of this polynomial
*/
public getDegree(): number {
return this.coefficients.length - 1;
}
/**
* @return true iff this polynomial is the monomial "0"
*/
public isZero(): boolean {
return this.coefficients[0] === 0
}
/**
* @return coefficient of x^degree term in this polynomial
*/
public getCoefficient(degree: number/*int*/): number {
return this.coefficients[this.coefficients.length - 1 - degree]
}
/**
* @return evaluation of this polynomial at a given point
*/
public evaluateAt(a: number/*int*/): number {
if (a === 0) {
// Just return the x^0 coefficient
return this.getCoefficient(0)
}
const coefficients = this.coefficients
let result
if (a === 1) {
// Just the sum of the coefficients
result = 0
for (let i = 0, length = coefficients.length; i !== length; i++) {
const coefficient = coefficients[i]
result = GenericGF.addOrSubtract(result, coefficient)
}
return result
}
result = coefficients[0]
const size = coefficients.length
const field = this.field
for (let i = 1; i < size; i++) {
result = GenericGF.addOrSubtract(field.multiply(a, result), coefficients[i])
}
return result
}
public addOrSubtract(other: GenericGFPoly): GenericGFPoly {
if (!this.field.equals(other.field)) {
throw new Exception(Exception.IllegalArgumentException, "GenericGFPolys do not have same GenericGF field")
}
if (this.isZero()) {
return other
}
if (other.isZero()) {
return this
}
let smallerCoefficients = this.coefficients
let largerCoefficients = other.coefficients
if (smallerCoefficients.length > largerCoefficients.length) {
const temp = smallerCoefficients
smallerCoefficients = largerCoefficients
largerCoefficients = temp
}
let sumDiff = new Int32Array(largerCoefficients.length)
const lengthDiff = largerCoefficients.length - smallerCoefficients.length
// Copy high-order terms only found in higher-degree polynomial's coefficients
System.arraycopy(largerCoefficients, 0, sumDiff, 0, lengthDiff)
for (let i = lengthDiff; i < largerCoefficients.length; i++) {
sumDiff[i] = GenericGF.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i])
}
return new GenericGFPoly(this.field, sumDiff)
}
public multiply(other: GenericGFPoly): GenericGFPoly {
if (!this.field.equals(other.field)) {
throw new Exception(Exception.IllegalArgumentException, "GenericGFPolys do not have same GenericGF field")
}
if (this.isZero() || other.isZero()) {
return this.field.getZero()
}
const aCoefficients = this.coefficients
const aLength = aCoefficients.length
const bCoefficients = other.coefficients
const bLength = bCoefficients.length
const product = new Int32Array(aLength + bLength - 1)
const field = this.field
for (let i = 0; i < aLength; i++) {
const aCoeff = aCoefficients[i]
for (let j = 0; j < bLength; j++) {
product[i + j] = GenericGF.addOrSubtract(product[i + j],
field.multiply(aCoeff, bCoefficients[j]))
}
}
return new GenericGFPoly(field, product)
}
public multiplyScalar(scalar: number/*int*/): GenericGFPoly {
if (scalar === 0) {
return this.field.getZero()
}
if (scalar === 1) {
return this
}
const size = this.coefficients.length
const field = this.field
const product = new Int32Array(size)
const coefficients = this.coefficients
for (let i = 0; i < size; i++) {
product[i] = field.multiply(coefficients[i], scalar)
}
return new GenericGFPoly(field, product)
}
public multiplyByMonomial(degree: number/*int*/, coefficient: number/*int*/): GenericGFPoly {
if (degree < 0) {
throw new Exception(Exception.IllegalArgumentException)
}
if (coefficient === 0) {
return this.field.getZero()
}
const coefficients = this.coefficients
const size = coefficients.length
const product = new Int32Array(size + degree)
const field = this.field
for (let i = 0; i < size; i++) {
product[i] = field.multiply(coefficients[i], coefficient)
}
return new GenericGFPoly(field, product)
}
public divide(other: GenericGFPoly): GenericGFPoly[] {
if (!this.field.equals(other.field)) {
throw new Exception(Exception.IllegalArgumentException, "GenericGFPolys do not have same GenericGF field")
}
if (other.isZero()) {
throw new Exception(Exception.IllegalArgumentException, "Divide by 0")
}
const field = this.field
let quotient: GenericGFPoly = field.getZero()
let remainder: GenericGFPoly = this
const denominatorLeadingTerm = other.getCoefficient(other.getDegree())
const inverseDenominatorLeadingTerm = field.inverse(denominatorLeadingTerm)
while (remainder.getDegree() >= other.getDegree() && !remainder.isZero()) {
const degreeDifference = remainder.getDegree() - other.getDegree()
const scale = field.multiply(remainder.getCoefficient(remainder.getDegree()), inverseDenominatorLeadingTerm)
const term = other.multiplyByMonomial(degreeDifference, scale)
const iterationQuotient = field.buildMonomial(degreeDifference, scale)
quotient = quotient.addOrSubtract(iterationQuotient)
remainder = remainder.addOrSubtract(term)
}
return [ quotient, remainder ]
}
/*@Override*/
public toString(): string {
let result = ""
for (let degree = this.getDegree(); degree >= 0; degree--) {
let coefficient = this.getCoefficient(degree)
if (coefficient != 0) {
if (coefficient < 0) {
result += " - "
coefficient = -coefficient
} else {
if (result.length > 0) {
result += " + "
}
}
if (degree === 0 || coefficient != 1) {
const alphaPower = this.field.log(coefficient)
if (alphaPower === 0) {
result += '1'
} else if (alphaPower === 1) {
result += 'a'
} else {
result += "a^"
result += alphaPower
}
}
if (degree != 0) {
if (degree === 1) {
result += 'x'
} else {
result += "x^"
result += degree
}
}
}
}
return result
}
}

View file

@ -0,0 +1,194 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common.reedsolomon {*/
import GenericGF from './GenericGF'
import GenericGFPoly from './GenericGFPoly'
import Exception from './../../Exception'
/**
* <p>Implements Reed-Solomon decoding, as the name implies.</p>
*
* <p>The algorithm will not be explained here, but the following references were helpful
* in creating this implementation:</p>
*
* <ul>
* <li>Bruce Maggs.
* <a href="http://www.cs.cmu.edu/afs/cs.cmu.edu/project/pscico-guyb/realworld/www/rs_decode.ps">
* "Decoding Reed-Solomon Codes"</a> (see discussion of Forney's Formula)</li>
* <li>J.I. Hall. <a href="www.mth.msu.edu/~jhall/classes/codenotes/GRS.pdf">
* "Chapter 5. Generalized Reed-Solomon Codes"</a>
* (see discussion of Euclidean algorithm)</li>
* </ul>
*
* <p>Much credit is due to William Rucklidge since portions of this code are an indirect
* port of his C++ Reed-Solomon implementation.</p>
*
* @author Sean Owen
* @author William Rucklidge
* @author sanfordsquires
*/
export default class ReedSolomonDecoder {
public constructor(private field: GenericGF) {}
/**
* <p>Decodes given set of received codewords, which include both data and error-correction
* codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place,
* in the input.</p>
*
* @param received data and error-correction codewords
* @param twoS number of error-correction codewords available
* @throws ReedSolomonException if decoding fails for any reason
*/
public decode(received: Int32Array, twoS: number/*int*/): void /*throws ReedSolomonException*/ {
const field = this.field
const poly = new GenericGFPoly(field, received)
const syndromeCoefficients = new Int32Array(twoS)
let noError: boolean = true
for (let i = 0; i < twoS; i++) {
const evalResult = poly.evaluateAt(field.exp(i + field.getGeneratorBase()))
syndromeCoefficients[syndromeCoefficients.length - 1 - i] = evalResult
if (evalResult !== 0) {
noError = false
}
}
if (noError) {
return
}
const syndrome = new GenericGFPoly(field, syndromeCoefficients)
const sigmaOmega = this.runEuclideanAlgorithm(field.buildMonomial(twoS, 1), syndrome, twoS)
const sigma = sigmaOmega[0]
const omega = sigmaOmega[1]
const errorLocations = this.findErrorLocations(sigma)
const errorMagnitudes = this.findErrorMagnitudes(omega, errorLocations)
for (let i = 0; i < errorLocations.length; i++) {
const position = received.length - 1 - field.log(errorLocations[i])
if (position < 0) {
throw new Exception(Exception.ReedSolomonException, "Bad error location")
}
received[position] = GenericGF.addOrSubtract(received[position], errorMagnitudes[i])
}
}
private runEuclideanAlgorithm(a: GenericGFPoly, b: GenericGFPoly, R: number/*int*/): GenericGFPoly[]
/*throws ReedSolomonException*/ {
// Assume a's degree is >= b's
if (a.getDegree() < b.getDegree()) {
const temp = a
a = b
b = temp
}
const field = this.field
let rLast = a
let r = b
let tLast = field.getZero()
let t = field.getOne()
// Run Euclidean algorithm until r's degree is less than R/2
while (r.getDegree() >= R / 2) {
let rLastLast = rLast
let tLastLast = tLast
rLast = r
tLast = t
// Divide rLastLast by rLast, with quotient in q and remainder in r
if (rLast.isZero()) {
// Oops, Euclidean algorithm already terminated?
throw new Exception(Exception.ReedSolomonException, "r_{i-1} was zero")
}
r = rLastLast
let q = field.getZero()
const denominatorLeadingTerm = rLast.getCoefficient(rLast.getDegree())
const dltInverse = field.inverse(denominatorLeadingTerm)
while (r.getDegree() >= rLast.getDegree() && !r.isZero()) {
const degreeDiff = r.getDegree() - rLast.getDegree()
const scale = field.multiply(r.getCoefficient(r.getDegree()), dltInverse)
q = q.addOrSubtract(field.buildMonomial(degreeDiff, scale))
r = r.addOrSubtract(rLast.multiplyByMonomial(degreeDiff, scale))
}
t = q.multiply(tLast).addOrSubtract(tLastLast)
if (r.getDegree() >= rLast.getDegree()) {
throw new Exception(Exception.IllegalStateException, "Division algorithm failed to reduce polynomial?")
}
}
const sigmaTildeAtZero = t.getCoefficient(0)
if (sigmaTildeAtZero === 0) {
throw new Exception(Exception.ReedSolomonException, "sigmaTilde(0) was zero")
}
const inverse = field.inverse(sigmaTildeAtZero)
const sigma = t.multiplyScalar(inverse)
const omega = r.multiplyScalar(inverse)
return [sigma, omega]
}
private findErrorLocations(errorLocator: GenericGFPoly): Int32Array /*throws ReedSolomonException*/ {
// This is a direct application of Chien's search
const numErrors = errorLocator.getDegree()
if (numErrors === 1) { // shortcut
return Int32Array.from([errorLocator.getCoefficient(1)])
}
const result = new Int32Array(numErrors)
let e = 0
const field = this.field
for (let i = 1; i < field.getSize() && e < numErrors; i++) {
if (errorLocator.evaluateAt(i) === 0) {
result[e] = field.inverse(i)
e++
}
}
if (e !== numErrors) {
throw new Exception(Exception.ReedSolomonException, "Error locator degree does not match number of roots")
}
return result
}
private findErrorMagnitudes(errorEvaluator: GenericGFPoly, errorLocations: Int32Array): Int32Array {
// This is directly applying Forney's Formula
const s = errorLocations.length
const result = new Int32Array(s)
const field = this.field
for (let i = 0; i < s; i++) {
const xiInverse = field.inverse(errorLocations[i])
let denominator = 1
for (let j = 0; j < s; j++) {
if (i !== j) {
//denominator = field.multiply(denominator,
// GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse)))
// Above should work but fails on some Apple and Linux JDKs due to a Hotspot bug.
// Below is a funny-looking workaround from Steven Parkes
const term = field.multiply(errorLocations[j], xiInverse)
const termPlus1 = (term & 0x1) == 0 ? term | 1 : term & ~1
denominator = field.multiply(denominator, termPlus1)
}
}
result[i] = field.multiply(errorEvaluator.evaluateAt(xiInverse),
field.inverse(denominator))
if (field.getGeneratorBase() != 0) {
result[i] = field.multiply(result[i], xiInverse)
}
}
return result
}
}

View file

@ -0,0 +1,81 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.common.reedsolomon {*/
/*import java.util.ArrayList;*/
/*import java.util.List;*/
import GenericGF from './GenericGF'
import GenericGFPoly from './GenericGFPoly'
import Exception from './../../Exception'
import System from './../../util/System'
/**
* <p>Implements Reed-Solomon encoding, as the name implies.</p>
*
* @author Sean Owen
* @author William Rucklidge
*/
export default class ReedSolomonEncoder {
private field: GenericGF
private cachedGenerators: GenericGFPoly[]
public constructor(field: GenericGF) {
this.field = field
this.cachedGenerators = []
this.cachedGenerators.push(new GenericGFPoly(field, Int32Array.from([1])));
}
private buildGenerator(degree: number/*int*/): GenericGFPoly {
const cachedGenerators = this.cachedGenerators
if (degree >= cachedGenerators.length) {
let lastGenerator = cachedGenerators[cachedGenerators.length - 1]
const field = this.field
for (let d = cachedGenerators.length; d <= degree; d++) {
const nextGenerator = lastGenerator.multiply(
new GenericGFPoly(field, Int32Array.from([1, field.exp(d - 1 + field.getGeneratorBase()) ])));
cachedGenerators.push(nextGenerator)
lastGenerator = nextGenerator
}
}
return cachedGenerators[degree]
}
public encode(toEncode: Int32Array, ecBytes: number/*int*/): void {
if (ecBytes === 0) {
throw new Exception(Exception.IllegalArgumentException, "No error correction bytes")
}
const dataBytes = toEncode.length - ecBytes
if (dataBytes <= 0) {
throw new Exception(Exception.IllegalArgumentException, "No data bytes provided")
}
const generator = this.buildGenerator(ecBytes)
const infoCoefficients: Int32Array = new Int32Array(dataBytes)
System.arraycopy(toEncode, 0, infoCoefficients, 0, dataBytes)
let info = new GenericGFPoly(this.field, infoCoefficients)
info = info.multiplyByMonomial(ecBytes, 1)
const remainder = info.divide(generator)[1]
const coefficients = remainder.getCoefficients()
const numZeroCoefficients = ecBytes - coefficients.length
for (let i = 0; i < numZeroCoefficients; i++) {
toEncode[dataBytes + i] = 0
}
System.arraycopy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.length)
}
}

View file

@ -0,0 +1,220 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode {*/
import BarcodeFormat from './../BarcodeFormat'
import BinaryBitmap from './../BinaryBitmap'
import DecodeHintType from './../DecodeHintType'
import Reader from './../Reader'
import Result from './../Result'
import ResultMetadataType from './../ResultMetadataType'
import ResultPoint from './../ResultPoint'
import BitMatrix from './../common/BitMatrix'
import DecoderResult from './../common/DecoderResult'
import DetectorResult from './../common/DetectorResult'
import Decoder from './decoder/Decoder'
import QRCodeDecoderMetaData from './decoder/QRCodeDecoderMetaData'
import Detector from './detector/Detector'
import Exception from './../Exception'
/*import java.util.List;*/
/*import java.util.Map;*/
/**
* This implementation can detect and decode QR Codes in an image.
*
* @author Sean Owen
*/
export default class QRCodeReader implements Reader {
private static NO_POINTS = new Array<ResultPoint>()
private decoder = new Decoder()
protected getDecoder(): Decoder {
return this.decoder
}
/**
* Locates and decodes a QR code in an image.
*
* @return a representing: string the content encoded by the QR code
* @throws NotFoundException if a QR code cannot be found
* @throws FormatException if a QR code cannot be decoded
* @throws ChecksumException if error correction fails
*/
/*@Override*/
// public decode(image: BinaryBitmap): Result /*throws NotFoundException, ChecksumException, FormatException */ {
// return this.decode(image, null)
// }
/*@Override*/
public decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any>): Result
/*throws NotFoundException, ChecksumException, FormatException */ {
let decoderResult: DecoderResult
let points: Array<ResultPoint>
if (hints !== undefined && hints !== null && undefined !== hints.get(DecodeHintType.PURE_BARCODE)) {
const bits = QRCodeReader.extractPureBits(image.getBlackMatrix())
decoderResult = this.decoder.decodeBitMatrix(bits, hints)
points = QRCodeReader.NO_POINTS
} else {
const detectorResult = new Detector(image.getBlackMatrix()).detect(hints)
decoderResult = this.decoder.decodeBitMatrix(detectorResult.getBits(), hints)
points = detectorResult.getPoints()
}
// If the code was mirrored: swap the bottom-left and the top-right points.
if (decoderResult.getOther() instanceof QRCodeDecoderMetaData) {
(<QRCodeDecoderMetaData> decoderResult.getOther()).applyMirroredCorrection(points)
}
const result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), undefined, points, BarcodeFormat.QR_CODE, undefined)
const byteSegments: Array<Uint8Array> = decoderResult.getByteSegments()
if (byteSegments !== null) {
result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegments)
}
const ecLevel: string = decoderResult.getECLevel()
if (ecLevel !== null) {
result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel)
}
if (decoderResult.hasStructuredAppend()) {
result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE,
decoderResult.getStructuredAppendSequenceNumber())
result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_PARITY,
decoderResult.getStructuredAppendParity())
}
return result
}
/*@Override*/
public reset(): void {
// do nothing
}
/**
* This method detects a code in a "pure" image -- that is, pure monochrome image
* which contains only an unrotated, unskewed, image of a code, with some white border
* around it. This is a specialized method that works exceptionally fast in this special
* case.
*
* @see com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix)
*/
private static extractPureBits(image: BitMatrix): BitMatrix /*throws NotFoundException */ {
const leftTopBlack: Int32Array = image.getTopLeftOnBit()
const rightBottomBlack: Int32Array = image.getBottomRightOnBit()
if (leftTopBlack === null || rightBottomBlack === null) {
throw new Exception(Exception.NotFoundException)
}
const moduleSize: number/*float*/ = this.moduleSize(leftTopBlack, image)
let top = leftTopBlack[1]
let bottom = rightBottomBlack[1]
let left = leftTopBlack[0]
let right = rightBottomBlack[0]
// Sanity check!
if (left >= right || top >= bottom) {
throw new Exception(Exception.NotFoundException)
}
if (bottom - top !== right - left) {
// Special case, where bottom-right module wasn't black so we found something else in the last row
// Assume it's a square, so use height as the width
right = left + (bottom - top)
if (right >= image.getWidth()) {
// Abort if that would not make sense -- off image
throw new Exception(Exception.NotFoundException)
}
}
const matrixWidth = Math.round((right - left + 1) / moduleSize)
const matrixHeight = Math.round((bottom - top + 1) / moduleSize)
if (matrixWidth <= 0 || matrixHeight <= 0) {
throw new Exception(Exception.NotFoundException)
}
if (matrixHeight != matrixWidth) {
// Only possibly decode square regions
throw new Exception(Exception.NotFoundException)
}
// Push in the "border" by half the module width so that we start
// sampling in the middle of the module. Just in case the image is a
// little off, this will help recover.
const nudge = /*(int) */Math.floor(moduleSize / 2.0)
top += nudge
left += nudge
// But careful that this does not sample off the edge
// "right" is the farthest-right valid pixel location -- right+1 is not necessarily
// This is positive by how much the inner x loop below would be too large
const nudgedTooFarRight = left + /*(int) */Math.floor((matrixWidth - 1) * moduleSize) - right;
if (nudgedTooFarRight > 0) {
if (nudgedTooFarRight > nudge) {
// Neither way fits; abort
throw new Exception(Exception.NotFoundException)
}
left -= nudgedTooFarRight
}
// See logic above
const nudgedTooFarDown = top + /*(int) */Math.floor((matrixHeight - 1) * moduleSize) - bottom;
if (nudgedTooFarDown > 0) {
if (nudgedTooFarDown > nudge) {
// Neither way fits; abort
throw new Exception(Exception.NotFoundException)
}
top -= nudgedTooFarDown
}
// Now just read off the bits
const bits = new BitMatrix(matrixWidth, matrixHeight)
for (let y = 0; y < matrixHeight; y++) {
const iOffset = top + /*(int) */Math.floor(y * moduleSize);
for (let x = 0; x < matrixWidth; x++) {
if (image.get(left + /*(int) */Math.floor(x * moduleSize), iOffset)) {
bits.set(x, y)
}
}
}
return bits
}
private static moduleSize(leftTopBlack: Int32Array, image: BitMatrix): number/*float*/ /*throws NotFoundException */ {
const height: number/*int*/ = image.getHeight()
const width: number/*int*/ = image.getWidth()
let x = leftTopBlack[0]
let y = leftTopBlack[1]
let inBlack: boolean = true
let transitions = 0
while (x < width && y < height) {
if (inBlack !== image.get(x, y)) {
if (++transitions === 5) {
break
}
inBlack = !inBlack
}
x++
y++
}
if (x === width || y === height) {
throw new Exception(Exception.NotFoundException)
}
return (x - leftTopBlack[0]) / 7.0
}
}

View file

@ -0,0 +1,118 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode {*/
import BarcodeFormat from './../BarcodeFormat'
import EncodeHintType from './../EncodeHintType'
import Writer from './../Writer'
import BitMatrix from './../common/BitMatrix'
import ByteMatrix from './encoder/ByteMatrix'
import ErrorCorrectionLevel from './decoder/ErrorCorrectionLevel'
import Encoder from './encoder/Encoder'
import QRCode from './encoder/QRCode'
import Exception from './../Exception'
/*import java.util.Map;*/
/**
* This object renders a QR Code as a BitMatrix 2D array of greyscale values.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
export default class QRCodeWriter implements Writer {
private static QUIET_ZONE_SIZE = 4
/*@Override*/
// public encode(contents: string, format: BarcodeFormat, width: number/*int*/, height: number/*int*/): BitMatrix
// /*throws WriterException */ {
// return encode(contents, format, width, height, null)
// }
/*@Override*/
public encode(contents: string,
format: BarcodeFormat,
width: number/*int*/,
height: number/*int*/,
hints: Map<EncodeHintType, any>): BitMatrix /*throws WriterException */ {
if (contents.length == 0) {
throw new Exception(Exception.IllegalArgumentException, "Found empty contents")
}
if (format != BarcodeFormat.QR_CODE) {
throw new Exception(Exception.IllegalArgumentException, "Can only encode QR_CODE, but got " + format)
}
if (width < 0 || height < 0) {
throw new Exception("IllegalArgumentException", "Requested dimensions are too small: " + width + 'x' +
height)
}
let errorCorrectionLevel = ErrorCorrectionLevel.L
let quietZone = QRCodeWriter.QUIET_ZONE_SIZE
if (hints !== null) {
if (undefined !== hints.get(EncodeHintType.ERROR_CORRECTION)) {
errorCorrectionLevel = ErrorCorrectionLevel.fromString(hints.get(EncodeHintType.ERROR_CORRECTION).toString())
}
if (undefined !== hints.get(EncodeHintType.MARGIN)) {
quietZone = Number.parseInt(hints.get(EncodeHintType.MARGIN).toString(), 10)
}
}
const code: QRCode = Encoder.encode(contents, errorCorrectionLevel, hints)
return QRCodeWriter.renderResult(code, width, height, quietZone)
}
// Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
// 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
private static renderResult(code: QRCode, width: number/*int*/, height: number/*int*/, quietZone: number/*int*/): BitMatrix {
const input = code.getMatrix()
if (input === null) {
throw new Exception(Exception.IllegalStateException)
}
const inputWidth = input.getWidth()
const inputHeight = input.getHeight()
const qrWidth = inputWidth + (quietZone * 2);
const qrHeight = inputHeight + (quietZone * 2);
const outputWidth = Math.max(width, qrWidth)
const outputHeight = Math.max(height, qrHeight)
const multiple = Math.min(Math.floor(outputWidth / qrWidth), Math.floor(outputHeight / qrHeight))
// Padding includes both the quiet zone and the extra white pixels to accommodate the requested
// dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
// If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
// handle all the padding from 100x100 (the actual QR) up to 200x160.
const leftPadding = Math.floor((outputWidth - (inputWidth * multiple)) / 2)
const topPadding = Math.floor((outputHeight - (inputHeight * multiple)) / 2)
const output = new BitMatrix(outputWidth, outputHeight)
for (let inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
// Write the contents of this row of the barcode
for (let inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
if (input.get(inputX, inputY) === 1) {
output.setRegion(outputX, outputY, multiple, multiple)
}
}
}
return output
}
}

View file

@ -0,0 +1,248 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.decoder {*/
import BitMatrix from './../../common/BitMatrix'
import Version from './Version'
import FormatInformation from './FormatInformation'
import Exception from './../../Exception'
import DataMask from './DataMask'
/**
* @author Sean Owen
*/
export default class BitMatrixParser {
private bitMatrix: BitMatrix
private parsedVersion: Version
private parsedFormatInfo: FormatInformation
private isMirror: boolean
/**
* @param bitMatrix {@link BitMatrix} to parse
* @throws FormatException if dimension is not >= 21 and 1 mod 4
*/
public constructor(bitMatrix: BitMatrix) /*throws FormatException*/ {
const dimension = bitMatrix.getHeight()
if (dimension < 21 || (dimension & 0x03) != 1) {
throw new Exception(Exception.FormatException)
}
this.bitMatrix = bitMatrix
}
/**
* <p>Reads format information from one of its two locations within the QR Code.</p>
*
* @return {@link FormatInformation} encapsulating the QR Code's format info
* @throws FormatException if both format information locations cannot be parsed as
* the valid encoding of format information
*/
public readFormatInformation(): FormatInformation /*throws FormatException*/ {
if (this.parsedFormatInfo !== null && this.parsedFormatInfo !== undefined) {
return this.parsedFormatInfo
}
// Read top-left format info bits
let formatInfoBits1 = 0
for (let i = 0; i < 6; i++) {
formatInfoBits1 = this.copyBit(i, 8, formatInfoBits1)
}
// .. and skip a bit in the timing pattern ...
formatInfoBits1 = this.copyBit(7, 8, formatInfoBits1)
formatInfoBits1 = this.copyBit(8, 8, formatInfoBits1)
formatInfoBits1 = this.copyBit(8, 7, formatInfoBits1)
// .. and skip a bit in the timing pattern ...
for (let j = 5; j >= 0; j--) {
formatInfoBits1 = this.copyBit(8, j, formatInfoBits1)
}
// Read the top-right/bottom-left pattern too
const dimension = this.bitMatrix.getHeight()
let formatInfoBits2 = 0
const jMin = dimension - 7
for (let j = dimension - 1; j >= jMin; j--) {
formatInfoBits2 = this.copyBit(8, j, formatInfoBits2)
}
for (let i = dimension - 8; i < dimension; i++) {
formatInfoBits2 = this.copyBit(i, 8, formatInfoBits2)
}
this.parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits1, formatInfoBits2)
if (this.parsedFormatInfo !== null) {
return this.parsedFormatInfo
}
throw new Exception(Exception.FormatException)
}
/**
* <p>Reads version information from one of its two locations within the QR Code.</p>
*
* @return {@link Version} encapsulating the QR Code's version
* @throws FormatException if both version information locations cannot be parsed as
* the valid encoding of version information
*/
public readVersion(): Version /*throws FormatException*/ {
if (this.parsedVersion !== null && this.parsedVersion !== undefined) {
return this.parsedVersion
}
const dimension = this.bitMatrix.getHeight()
const provisionalVersion = Math.floor((dimension - 17) / 4)
if (provisionalVersion <= 6) {
return Version.getVersionForNumber(provisionalVersion)
}
// Read top-right version info: 3 wide by 6 tall
let versionBits = 0
const ijMin = dimension - 11
for (let j = 5; j >= 0; j--) {
for (let i = dimension - 9; i >= ijMin; i--) {
versionBits = this.copyBit(i, j, versionBits)
}
}
let theParsedVersion = Version.decodeVersionInformation(versionBits)
if (theParsedVersion !== null && theParsedVersion.getDimensionForVersion() == dimension) {
this.parsedVersion = theParsedVersion
return theParsedVersion
}
// Hmm, failed. Try bottom left: 6 wide by 3 tall
versionBits = 0
for (let i = 5; i >= 0; i--) {
for (let j = dimension - 9; j >= ijMin; j--) {
versionBits = this.copyBit(i, j, versionBits)
}
}
theParsedVersion = Version.decodeVersionInformation(versionBits)
if (theParsedVersion !== null && theParsedVersion.getDimensionForVersion() == dimension) {
this.parsedVersion = theParsedVersion
return theParsedVersion
}
throw new Exception(Exception.FormatException)
}
private copyBit(i: number/*int*/, j: number/*int*/, versionBits: number/*int*/): number/*int*/ {
const bit: boolean = this.isMirror ? this.bitMatrix.get(j, i) : this.bitMatrix.get(i, j)
return bit ? (versionBits << 1) | 0x1 : versionBits << 1
}
/**
* <p>Reads the bits in the {@link BitMatrix} representing the finder pattern in the
* correct order in order to reconstruct the codewords bytes contained within the
* QR Code.</p>
*
* @return bytes encoded within the QR Code
* @throws FormatException if the exact number of bytes expected is not read
*/
public readCodewords(): Uint8Array /*throws FormatException*/ {
const formatInfo = this.readFormatInformation()
const version = this.readVersion()
// Get the data mask for the format used in this QR Code. This will exclude
// some bits from reading as we wind through the bit matrix.
const dataMask = DataMask.values.get(formatInfo.getDataMask())
const dimension = this.bitMatrix.getHeight()
dataMask.unmaskBitMatrix(this.bitMatrix, dimension)
const functionPattern = version.buildFunctionPattern()
let readingUp: boolean = true
const result = new Uint8Array(version.getTotalCodewords())
let resultOffset = 0
let currentByte = 0
let bitsRead = 0
// Read columns in pairs, from right to left
for (let j = dimension - 1; j > 0; j -= 2) {
if (j == 6) {
// Skip whole column with vertical alignment pattern
// saves time and makes the other code proceed more cleanly
j--
}
// Read alternatingly from bottom to top then top to bottom
for (let count = 0; count < dimension; count++) {
const i = readingUp ? dimension - 1 - count : count
for (let col = 0; col < 2; col++) {
// Ignore bits covered by the function pattern
if (!functionPattern.get(j - col, i)) {
// Read a bit
bitsRead++
currentByte <<= 1
if (this.bitMatrix.get(j - col, i)) {
currentByte |= 1
}
// If we've made a whole byte, save it off
if (bitsRead == 8) {
result[resultOffset++] = /*(byte) */currentByte
bitsRead = 0
currentByte = 0
}
}
}
}
readingUp = !readingUp//readingUp ^= true; // readingUp = !readingUp; // switch directions
}
if (resultOffset != version.getTotalCodewords()) {
throw new Exception(Exception.FormatException)
}
return result
}
/**
* Revert the mask removal done while reading the code words. The bit matrix should revert to its original state.
*/
public remask(): void {
if (this.parsedFormatInfo === null) {
return; // We have no format information, and have no data mask
}
const dataMask = DataMask.values.get(this.parsedFormatInfo.getDataMask())
const dimension = this.bitMatrix.getHeight()
dataMask.unmaskBitMatrix(this.bitMatrix, dimension)
}
/**
* Prepare the parser for a mirrored operation.
* This flag has effect only on the {@link #readFormatInformation()} and the
* {@link #readVersion()}. Before proceeding with {@link #readCodewords()} the
* {@link #mirror()} method should be called.
*
* @param mirror Whether to read version and format information mirrored.
*/
public setMirror(isMirror: boolean): void {
this.parsedVersion = null
this.parsedFormatInfo = null
this.isMirror = isMirror
}
/** Mirror the bit matrix in order to attempt a second reading. */
public mirror(): void {
const bitMatrix = this.bitMatrix
for (let x = 0, width = bitMatrix.getWidth(); x < width; x++) {
for (let y = x + 1, height = bitMatrix.getHeight(); y < height; y++) {
if (bitMatrix.get(x, y) !== bitMatrix.get(y, x)) {
bitMatrix.flip(y, x);
bitMatrix.flip(x, y);
}
}
}
}
}

View file

@ -0,0 +1,123 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.decoder {*/
import Version from './Version'
import ECBlocks from './ECBlocks'
import ECB from './ECB'
import ErrorCorrectionLevel from './ErrorCorrectionLevel'
import Exception from './../../Exception'
/**
* <p>Encapsulates a block of data within a QR Code. QR Codes may split their data into
* multiple blocks, each of which is a unit of data and error-correction codewords. Each
* is represented by an instance of this class.</p>
*
* @author Sean Owen
*/
export default class DataBlock {
private constructor(private numDataCodewords: number/*int*/, private codewords: Uint8Array) {}
/**
* <p>When QR Codes use multiple data blocks, they are actually interleaved.
* That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This
* method will separate the data into original blocks.</p>
*
* @param rawCodewords bytes as read directly from the QR Code
* @param version version of the QR Code
* @param ecLevel error-correction level of the QR Code
* @return DataBlocks containing original bytes, "de-interleaved" from representation in the
* QR Code
*/
public static getDataBlocks(rawCodewords: Uint8Array,
version: Version,
ecLevel: ErrorCorrectionLevel): DataBlock[] {
if (rawCodewords.length != version.getTotalCodewords()) {
throw new Exception(Exception.IllegalArgumentException)
}
// Figure out the number and size of data blocks used by this version and
// error correction level
const ecBlocks: ECBlocks = version.getECBlocksForLevel(ecLevel)
// First count the total number of data blocks
let totalBlocks = 0
const ecBlockArray: ECB[] = ecBlocks.getECBlocks()
for (const ecBlock of ecBlockArray) {
totalBlocks += ecBlock.getCount()
}
// Now establish DataBlocks of the appropriate size and number of data codewords
const result = new Array<DataBlock>(totalBlocks)
let numResultBlocks = 0
for (const ecBlock of ecBlockArray) {
for (let i = 0; i < ecBlock.getCount(); i++) {
const numDataCodewords = ecBlock.getDataCodewords()
const numBlockCodewords = ecBlocks.getECCodewordsPerBlock() + numDataCodewords
result[numResultBlocks++] = new DataBlock(numDataCodewords, new Uint8Array(numBlockCodewords))
}
}
// All blocks have the same amount of data, except that the last n
// (where n may be 0) have 1 more byte. Figure out where these start.
const shorterBlocksTotalCodewords = result[0].codewords.length
let longerBlocksStartAt = result.length - 1
// TYPESCRIPTPORT: check length is correct here
while (longerBlocksStartAt >= 0) {
const numCodewords = result[longerBlocksStartAt].codewords.length
if (numCodewords === shorterBlocksTotalCodewords) {
break
}
longerBlocksStartAt--
}
longerBlocksStartAt++
const shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.getECCodewordsPerBlock()
// The last elements of result may be 1 element longer
// first fill out as many elements as all of them have
let rawCodewordsOffset = 0
for (let i = 0; i < shorterBlocksNumDataCodewords; i++) {
for (let j = 0; j < numResultBlocks; j++) {
result[j].codewords[i] = rawCodewords[rawCodewordsOffset++]
}
}
// Fill out the last data block in the longer ones
for (let j = longerBlocksStartAt; j < numResultBlocks; j++) {
result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++]
}
// Now add in error correction blocks
const max = result[0].codewords.length
for (let i = shorterBlocksNumDataCodewords; i < max; i++) {
for (let j = 0; j < numResultBlocks; j++) {
const iOffset = j < longerBlocksStartAt ? i : i + 1
result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++]
}
}
return result
}
public getNumDataCodewords(): number/*int*/ {
return this.numDataCodewords
}
public getCodewords(): Uint8Array {
return this.codewords
}
}

View file

@ -0,0 +1,117 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.decoder {*/
import BitMatrix from './../../common/BitMatrix'
export const enum DataMaskValues {
DATA_MASK_000,
DATA_MASK_001,
DATA_MASK_010,
DATA_MASK_011,
DATA_MASK_100,
DATA_MASK_101,
DATA_MASK_110,
DATA_MASK_111
}
/**
* <p>Encapsulates data masks for the data bits in a QR code, per ISO 18004:2006 6.8. Implementations
* of this class can un-mask a raw BitMatrix. For simplicity, they will unmask the entire BitMatrix,
* including areas used for finder patterns, timing patterns, etc. These areas should be unused
* after the point they are unmasked anyway.</p>
*
* <p>Note that the diagram in section 6.8.1 is misleading since it indicates that i is column position
* and j is row position. In fact, as the text says, i is row position and j is column position.</p>
*
* @author Sean Owen
*/
export default class DataMask {
// See ISO 18004:2006 6.8.1
public constructor(private value: DataMaskValues, private isMasked: (i: number, j: number) => boolean) {
}
public static values = new Map<DataMaskValues, DataMask>([
/**
* 000: mask bits for which (x + y) mod 2 == 0
*/
[DataMaskValues.DATA_MASK_000, new DataMask(DataMaskValues.DATA_MASK_000, (i: number/*int*/, j: number/*int*/) => { return ((i + j) & 0x01) === 0 })],
/**
* 001: mask bits for which x mod 2 == 0
*/
[DataMaskValues.DATA_MASK_001, new DataMask(DataMaskValues.DATA_MASK_001, (i: number/*int*/, j: number/*int*/) => { return (i & 0x01) === 0 })],
/**
* 010: mask bits for which y mod 3 == 0
*/
[DataMaskValues.DATA_MASK_010, new DataMask(DataMaskValues.DATA_MASK_010, (i: number/*int*/, j: number/*int*/) => { return j % 3 === 0 })],
/**
* 011: mask bits for which (x + y) mod 3 == 0
*/
[DataMaskValues.DATA_MASK_011, new DataMask(DataMaskValues.DATA_MASK_011, (i: number/*int*/, j: number/*int*/) => { return (i + j) % 3 === 0 })],
/**
* 100: mask bits for which (x/2 + y/3) mod 2 == 0
*/
[DataMaskValues.DATA_MASK_100, new DataMask(DataMaskValues.DATA_MASK_100, (i: number/*int*/, j: number/*int*/) => { return ((Math.floor(i / 2) + Math.floor(j / 3)) & 0x01) === 0 })],
/**
* 101: mask bits for which xy mod 2 + xy mod 3 == 0
* equivalently, such that xy mod 6 == 0
*/
[DataMaskValues.DATA_MASK_101, new DataMask(DataMaskValues.DATA_MASK_101, (i: number/*int*/, j: number/*int*/) => { return (i * j) % 6 === 0 })],
/**
* 110: mask bits for which (xy mod 2 + xy mod 3) mod 2 == 0
* equivalently, such that xy mod 6 < 3
*/
[DataMaskValues.DATA_MASK_110, new DataMask(DataMaskValues.DATA_MASK_110, (i: number/*int*/, j: number/*int*/) => { return ((i * j) % 6) < 3 })],
/**
* 111: mask bits for which ((x+y)mod 2 + xy mod 3) mod 2 == 0
* equivalently, such that (x + y + xy mod 3) mod 2 == 0
*/
[DataMaskValues.DATA_MASK_111, new DataMask(DataMaskValues.DATA_MASK_111, (i: number/*int*/, j: number/*int*/) => { return ((i + j + ((i * j) % 3)) & 0x01) === 0 })],
])
// End of enum constants.
/**
* <p>Implementations of this method reverse the data masking process applied to a QR Code and
* make its bits ready to read.</p>
*
* @param bits representation of QR Code bits
* @param dimension dimension of QR Code, represented by bits, being unmasked
*/
public unmaskBitMatrix(bits: BitMatrix, dimension: number/*int*/): void {
for (let i = 0; i < dimension; i++) {
for (let j = 0; j < dimension; j++) {
if (this.isMasked(i, j)) {
bits.flip(j, i)
}
}
}
}
// abstract boolean isMasked(i: number/*int*/, j: number/*int*/);
}

View file

@ -0,0 +1,375 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.decoder {*/
import DecodeHintType from './../../DecodeHintType'
import BitSource from './../../common/BitSource'
import CharacterSetECI from './../../common/CharacterSetECI'
import DecoderResult from './../../common/DecoderResult'
import StringUtils from './../../common/StringUtils'
import Version from './Version'
import ErrorCorrectionLevel from './ErrorCorrectionLevel'
import Mode from './Mode'
import Exception from './../../Exception'
import StringBuilder from './../../util/StringBuilder'
import StringEncoding from './../../util/StringEncoding'
/*import java.io.UnsupportedEncodingException;*/
/*import java.util.ArrayList;*/
/*import java.util.Collection;*/
/*import java.util.List;*/
/*import java.util.Map;*/
/**
* <p>QR Codes can encode text as bits in one of several modes, and can use multiple modes
* in one QR Code. This class decodes the bits back into text.</p>
*
* <p>See ISO 18004:2006, 6.4.3 - 6.4.7</p>
*
* @author Sean Owen
*/
export default class DecodedBitStreamParser {
/**
* See ISO 18004:2006, 6.4.4 Table 5
*/
private static ALPHANUMERIC_CHARS =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
private static GB2312_SUBSET = 1;
public static decode(bytes: Uint8Array,
version: Version,
ecLevel: ErrorCorrectionLevel,
hints: Map<DecodeHintType, any>): DecoderResult /*throws FormatException*/ {
const bits = new BitSource(bytes)
let result = new StringBuilder()
const byteSegments = new Array<Uint8Array>()//1
// TYPESCRIPTPORT: I do not use constructor with size 1 as in original Java means capacity and the array length is checked below
let symbolSequence = -1
let parityData = -1
try {
let currentCharacterSetECI: CharacterSetECI = null
let fc1InEffect: boolean = false
let mode: Mode
do {
// While still another segment to read...
if (bits.available() < 4) {
// OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
mode = Mode.TERMINATOR
} else {
const modeBits = bits.readBits(4)
mode = Mode.forBits(modeBits) // mode is encoded by 4 bits
}
switch (mode) {
case Mode.TERMINATOR:
break
case Mode.FNC1_FIRST_POSITION:
case Mode.FNC1_SECOND_POSITION:
// We do little with FNC1 except alter the parsed result a bit according to the spec
fc1InEffect = true
break
case Mode.STRUCTURED_APPEND:
if (bits.available() < 16) {
throw new Exception(Exception.FormatException)
}
// sequence number and parity is added later to the result metadata
// Read next 8 bits (symbol sequence #) and 8 bits (data: parity), then continue
symbolSequence = bits.readBits(8)
parityData = bits.readBits(8)
break
case Mode.ECI:
// Count doesn't apply to ECI
const value = DecodedBitStreamParser.parseECIValue(bits)
currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value)
if (currentCharacterSetECI === null) {
throw new Exception(Exception.FormatException)
}
break
case Mode.HANZI:
// First handle Hanzi mode which does not start with character count
// Chinese mode contains a sub set indicator right after mode indicator
const subset = bits.readBits(4)
const countHanzi = bits.readBits(mode.getCharacterCountBits(version))
if (subset === DecodedBitStreamParser.GB2312_SUBSET) {
DecodedBitStreamParser.decodeHanziSegment(bits, result, countHanzi)
}
break
default:
// "Normal" QR code modes:
// How many characters will follow, encoded in this mode?
const count = bits.readBits(mode.getCharacterCountBits(version))
switch (mode) {
case Mode.NUMERIC:
DecodedBitStreamParser.decodeNumericSegment(bits, result, count)
break
case Mode.ALPHANUMERIC:
DecodedBitStreamParser.decodeAlphanumericSegment(bits, result, count, fc1InEffect)
break
case Mode.BYTE:
DecodedBitStreamParser.decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments, hints)
break
case Mode.KANJI:
DecodedBitStreamParser.decodeKanjiSegment(bits, result, count)
break
default:
throw new Exception(Exception.FormatException)
}
break
}
} while (mode !== Mode.TERMINATOR)
} catch (iae/*: IllegalArgumentException*/) {
// from readBits() calls
throw new Exception(Exception.FormatException)
}
return new DecoderResult(bytes,
result.toString(),
byteSegments.length === 0 ? null : byteSegments,
ecLevel === null ? null : ecLevel.toString(),
symbolSequence,
parityData)
}
/**
* See specification GBT 18284-2000
*/
private static decodeHanziSegment(bits: BitSource,
result: StringBuilder,
count: number/*int*/): void /*throws FormatException*/ {
// Don't crash trying to read more bits than we have available.
if (count * 13 > bits.available()) {
throw new Exception(Exception.FormatException)
}
// Each character will require 2 bytes. Read the characters as 2-byte pairs
// and decode as GB2312 afterwards
const buffer = new Uint8Array(2 * count)
let offset = 0
while (count > 0) {
// Each 13 bits encodes a 2-byte character
const twoBytes = bits.readBits(13)
let assembledTwoBytes = (((twoBytes / 0x060) << 8) & 0xFFFFFFFF) | (twoBytes % 0x060)
if (assembledTwoBytes < 0x003BF) {
// In the 0xA1A1 to 0xAAFE range
assembledTwoBytes += 0x0A1A1
} else {
// In the 0xB0A1 to 0xFAFE range
assembledTwoBytes += 0x0A6A1
}
buffer[offset] = /*(byte) */((assembledTwoBytes >> 8) & 0xFF)
buffer[offset + 1] = /*(byte) */(assembledTwoBytes & 0xFF)
offset += 2
count--
}
try {
result.append(StringEncoding.decode(buffer, StringUtils.GB2312))
// TYPESCRIPTPORT: TODO: implement GB2312 decode. StringView from MDN could be a starting point
} catch (ignored/*: UnsupportedEncodingException*/) {
throw new Exception(Exception.FormatException, ignored)
}
}
private static decodeKanjiSegment(bits: BitSource,
result: StringBuilder,
count: number/*int*/): void /*throws FormatException*/ {
// Don't crash trying to read more bits than we have available.
if (count * 13 > bits.available()) {
throw new Exception(Exception.FormatException)
}
// Each character will require 2 bytes. Read the characters as 2-byte pairs
// and decode as Shift_JIS afterwards
const buffer = new Uint8Array(2 * count)
let offset = 0
while (count > 0) {
// Each 13 bits encodes a 2-byte character
const twoBytes = bits.readBits(13)
let assembledTwoBytes = (((twoBytes / 0x0C0) << 8) & 0xFFFFFFFF) | (twoBytes % 0x0C0)
if (assembledTwoBytes < 0x01F00) {
// In the 0x8140 to 0x9FFC range
assembledTwoBytes += 0x08140
} else {
// In the 0xE040 to 0xEBBF range
assembledTwoBytes += 0x0C140
}
buffer[offset] = /*(byte) */(assembledTwoBytes >> 8)
buffer[offset + 1] = /*(byte) */assembledTwoBytes
offset += 2
count--
}
// Shift_JIS may not be supported in some environments:
try {
result.append(StringEncoding.decode(buffer, StringUtils.SHIFT_JIS))
// TYPESCRIPTPORT: TODO: implement SHIFT_JIS decode. StringView from MDN could be a starting point
} catch (ignored/*: UnsupportedEncodingException*/) {
throw new Exception(Exception.FormatException, ignored)
}
}
private static decodeByteSegment(bits: BitSource,
result: StringBuilder,
count: number/*int*/,
currentCharacterSetECI: CharacterSetECI,
byteSegments: Uint8Array[],
hints: Map<DecodeHintType, any>): void /*throws FormatException*/ {
// Don't crash trying to read more bits than we have available.
if (8 * count > bits.available()) {
throw new Exception(Exception.FormatException)
}
const readBytes = new Uint8Array(count)
for (let i = 0; i < count; i++) {
readBytes[i] = /*(byte) */bits.readBits(8)
}
let encoding: string
if (currentCharacterSetECI === null) {
// The spec isn't clear on this mode; see
// section 6.4.5: t does not say which encoding to assuming
// upon decoding. I have seen ISO-8859-1 used as well as
// Shift_JIS -- without anything like an ECI designator to
// give a hint.
encoding = StringUtils.guessEncoding(readBytes, hints)
} else {
encoding = currentCharacterSetECI.getName()
}
try {
result.append(StringEncoding.decode(readBytes, encoding))
} catch (ignored/*: UnsupportedEncodingException*/) {
throw new Exception(Exception.FormatException, ignored)
}
byteSegments.push(readBytes)
}
private static toAlphaNumericChar(value: number/*int*/): string /*throws FormatException*/ {
if (value >= DecodedBitStreamParser.ALPHANUMERIC_CHARS.length) {
throw new Exception(Exception.FormatException)
}
return DecodedBitStreamParser.ALPHANUMERIC_CHARS[value]
}
private static decodeAlphanumericSegment(bits: BitSource,
result: StringBuilder,
count: number/*int*/,
fc1InEffect: boolean): void /*throws FormatException*/ {
// Read two characters at a time
const start = result.length()
while (count > 1) {
if (bits.available() < 11) {
throw new Exception(Exception.FormatException)
}
const nextTwoCharsBits = bits.readBits(11)
result.append(DecodedBitStreamParser.toAlphaNumericChar(Math.floor(nextTwoCharsBits / 45)))
result.append(DecodedBitStreamParser.toAlphaNumericChar(nextTwoCharsBits % 45))
count -= 2
}
if (count == 1) {
// special case: one character left
if (bits.available() < 6) {
throw new Exception(Exception.FormatException)
}
result.append(DecodedBitStreamParser.toAlphaNumericChar(bits.readBits(6)))
}
// See section 6.4.8.1, 6.4.8.2
if (fc1InEffect) {
// We need to massage the result a bit if in an FNC1 mode:
for (let i = start; i < result.length(); i++) {
if (result.charAt(i) === '%') {
if (i < result.length() - 1 && result.charAt(i + 1) === '%') {
// %% is rendered as %
result.deleteCharAt(i + 1)
} else {
// In alpha mode, % should be converted to FNC1 separator 0x1D
result.setCharAt(i, String.fromCharCode(0x1D))
}
}
}
}
}
private static decodeNumericSegment(bits: BitSource,
result: StringBuilder,
count: number/*int*/): void /*throws FormatException*/ {
// Read three digits at a time
while (count >= 3) {
// Each 10 bits encodes three digits
if (bits.available() < 10) {
throw new Exception(Exception.FormatException)
}
const threeDigitsBits = bits.readBits(10)
if (threeDigitsBits >= 1000) {
throw new Exception(Exception.FormatException)
}
result.append(DecodedBitStreamParser.toAlphaNumericChar(Math.floor(threeDigitsBits / 100)))
result.append(DecodedBitStreamParser.toAlphaNumericChar(Math.floor(threeDigitsBits / 10) % 10))
result.append(DecodedBitStreamParser.toAlphaNumericChar(threeDigitsBits % 10))
count -= 3
}
if (count == 2) {
// Two digits left over to read, encoded in 7 bits
if (bits.available() < 7) {
throw new Exception(Exception.FormatException)
}
const twoDigitsBits = bits.readBits(7)
if (twoDigitsBits >= 100) {
throw new Exception(Exception.FormatException)
}
result.append(DecodedBitStreamParser.toAlphaNumericChar(Math.floor(twoDigitsBits / 10)))
result.append(DecodedBitStreamParser.toAlphaNumericChar(twoDigitsBits % 10))
} else if (count == 1) {
// One digit left over to read
if (bits.available() < 4) {
throw new Exception(Exception.FormatException)
}
const digitBits = bits.readBits(4)
if (digitBits >= 10) {
throw new Exception(Exception.FormatException)
}
result.append(DecodedBitStreamParser.toAlphaNumericChar(digitBits))
}
}
private static parseECIValue(bits: BitSource): number/*int*/ /*throws FormatException*/ {
const firstByte = bits.readBits(8)
if ((firstByte & 0x80) === 0) {
// just one byte
return firstByte & 0x7F
}
if ((firstByte & 0xC0) === 0x80) {
// two bytes
const secondByte = bits.readBits(8)
return (((firstByte & 0x3F) << 8) & 0xFFFFFFFF) | secondByte
}
if ((firstByte & 0xE0) === 0xC0) {
// three bytes
const secondThirdBytes = bits.readBits(16)
return (((firstByte & 0x1F) << 16) & 0xFFFFFFFF) | secondThirdBytes
}
throw new Exception(Exception.FormatException)
}
}
function Uint8ArrayToString(a: Uint8Array): string {
const CHUNK_SZ = 0x8000;
const c = new StringBuilder()
for (let i = 0, length = a.length; i < length; i += CHUNK_SZ) {
c.append(String.fromCharCode.apply(null, a.subarray(i, i + CHUNK_SZ)))
}
return c.toString()
}

View file

@ -0,0 +1,191 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.decoder {*/
import DecodeHintType from './../../DecodeHintType'
import BitMatrix from './../../common/BitMatrix'
import DecoderResult from './../../common/DecoderResult'
import GenericGF from './../../common/reedsolomon/GenericGF'
import ReedSolomonDecoder from './../../common/reedsolomon/ReedSolomonDecoder'
import BitMatrixParser from './BitMatrixParser'
import QRCodeDecoderMetaData from './QRCodeDecoderMetaData'
import DataBlock from './DataBlock'
import DecodedBitStreamParser from './DecodedBitStreamParser'
import Exception from './../../Exception'
/*import java.util.Map;*/
/**
* <p>The main class which implements QR Code decoding -- as opposed to locating and extracting
* the QR Code from an image.</p>
*
* @author Sean Owen
*/
export default class Decoder {
private rsDecoder: ReedSolomonDecoder
public constructor() {
this.rsDecoder = new ReedSolomonDecoder(GenericGF.QR_CODE_FIELD_256)
}
// public decode(image: boolean[][]): DecoderResult /*throws ChecksumException, FormatException*/ {
// return decode(image, null)
// }
/**
* <p>Convenience method that can decode a QR Code represented as a 2D array of booleans.
* "true" is taken to mean a black module.</p>
*
* @param image booleans representing white/black QR Code modules
* @param hints decoding hints that should be used to influence decoding
* @return text and bytes encoded within the QR Code
* @throws FormatException if the QR Code cannot be decoded
* @throws ChecksumException if error correction fails
*/
public decodeBooleanArray(image: boolean[][], hints?: Map<DecodeHintType, any>): DecoderResult
/*throws ChecksumException, FormatException*/ {
return this.decodeBitMatrix(BitMatrix.parseFromBooleanArray(image), hints)
}
// public decodeBitMatrix(bits: BitMatrix): DecoderResult /*throws ChecksumException, FormatException*/ {
// return decode(bits, null)
// }
/**
* <p>Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.</p>
*
* @param bits booleans representing white/black QR Code modules
* @param hints decoding hints that should be used to influence decoding
* @return text and bytes encoded within the QR Code
* @throws FormatException if the QR Code cannot be decoded
* @throws ChecksumException if error correction fails
*/
public decodeBitMatrix(bits: BitMatrix, hints?: Map<DecodeHintType, any>): DecoderResult
/*throws FormatException, ChecksumException*/ {
// Construct a parser and read version, error-correction level
const parser = new BitMatrixParser(bits)
let ex = null
try {
return this.decodeBitMatrixParser(parser, hints)
} catch (e/*: FormatException, ChecksumException*/) {
ex = e
}
try {
// Revert the bit matrix
parser.remask()
// Will be attempting a mirrored reading of the version and format info.
parser.setMirror(true)
// Preemptively read the version.
parser.readVersion()
// Preemptively read the format information.
parser.readFormatInformation()
/*
* Since we're here, this means we have successfully detected some kind
* of version and format information when mirrored. This is a good sign,
* that the QR code may be mirrored, and we should try once more with a
* mirrored content.
*/
// Prepare for a mirrored reading.
parser.mirror()
const result = this.decodeBitMatrixParser(parser, hints)
// Success! Notify the caller that the code was mirrored.
result.setOther(new QRCodeDecoderMetaData(true))
return result
} catch (e/*FormatException | ChecksumException*/) {
// Throw the exception from the original reading
if (ex !== null) {
throw ex
}
throw e
}
}
private decodeBitMatrixParser(parser: BitMatrixParser, hints: Map<DecodeHintType, any>): DecoderResult
/*throws FormatException, ChecksumException*/ {
const version = parser.readVersion()
const ecLevel = parser.readFormatInformation().getErrorCorrectionLevel()
// Read codewords
const codewords = parser.readCodewords()
// Separate into data blocks
const dataBlocks = DataBlock.getDataBlocks(codewords, version, ecLevel)
// Count total number of data bytes
let totalBytes = 0
for (const dataBlock of dataBlocks) {
totalBytes += dataBlock.getNumDataCodewords()
}
const resultBytes = new Uint8Array(totalBytes)
let resultOffset = 0
// Error-correct and copy data blocks together into a stream of bytes
for (const dataBlock of dataBlocks) {
const codewordBytes = dataBlock.getCodewords()
const numDataCodewords = dataBlock.getNumDataCodewords()
this.correctErrors(codewordBytes, numDataCodewords)
for (let i = 0; i < numDataCodewords; i++) {
resultBytes[resultOffset++] = codewordBytes[i]
}
}
// Decode the contents of that stream of bytes
return DecodedBitStreamParser.decode(resultBytes, version, ecLevel, hints)
}
/**
* <p>Given data and error-correction codewords received, possibly corrupted by errors, attempts to
* correct the errors in-place using Reed-Solomon error correction.</p>
*
* @param codewordBytes data and error correction codewords
* @param numDataCodewords number of codewords that are data bytes
* @throws ChecksumException if error correction fails
*/
private correctErrors(codewordBytes: Uint8Array, numDataCodewords: number/*int*/): void /*throws ChecksumException*/ {
const numCodewords = codewordBytes.length
// First read into an array of ints
const codewordsInts = new Int32Array(codewordBytes)
// TYPESCRIPTPORT: not realy necessary to transform to ints? could redesign everything to work with unsigned bytes?
// const codewordsInts = new Int32Array(numCodewords)
// for (let i = 0; i < numCodewords; i++) {
// codewordsInts[i] = codewordBytes[i] & 0xFF
// }
try {
this.rsDecoder.decode(codewordsInts, codewordBytes.length - numDataCodewords)
} catch (ignored/*: ReedSolomonException*/) {
throw new Exception(Exception.ChecksumException)
}
// Copy back into array of bytes -- only need to worry about the bytes that were data
// We don't care about errors in the error-correction codewords
for (let i = 0; i < numDataCodewords; i++) {
codewordBytes[i] = /*(byte) */codewordsInts[i]
}
}
}

View file

@ -0,0 +1,22 @@
/**
* <p>Encapsulates the parameters for one error-correction block in one symbol version.
* This includes the number of data codewords, and the number of times a block with these
* parameters is used consecutively in the QR code version's format.</p>
*/
export default class ECB {
private count: number/*int*/
private dataCodewords: number/*int*/
public constructor(count: number/*int*/, dataCodewords: number/*int*/) {
this.count = count
this.dataCodewords = dataCodewords
}
public getCount(): number/*int*/ {
return this.count
}
public getDataCodewords(): number/*int*/ {
return this.dataCodewords
}
}

View file

@ -0,0 +1,36 @@
import ECB from './ECB'
/**
* <p>Encapsulates a set of error-correction blocks in one symbol version. Most versions will
* use blocks of differing sizes within one version, so, this encapsulates the parameters for
* each set of blocks. It also holds the number of error-correction codewords per block since it
* will be the same across all blocks within one version.</p>
*/
export default class ECBlocks {
private ecBlocks: ECB[]
public constructor(private ecCodewordsPerBlock: number/*int*/, ...ecBlocks: ECB[]) {
this.ecBlocks = ecBlocks
}
public getECCodewordsPerBlock(): number/*int*/ {
return this.ecCodewordsPerBlock
}
public getNumBlocks(): number/*int*/ {
let total = 0
const ecBlocks = this.ecBlocks
for (const ecBlock of ecBlocks) {
total += ecBlock.getCount()
}
return total
}
public getTotalECCodewords(): number/*int*/ {
return this.ecCodewordsPerBlock * this.getNumBlocks();
}
public getECBlocks(): ECB[] {
return this.ecBlocks
}
}

View file

@ -0,0 +1,93 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.decoder {*/
import Exception from './../../Exception'
export const enum ErrorCorrectionLevelValues {
L,
M,
Q,
H
}
/**
* <p>See ISO 18004:2006, 6.5.1. This enum encapsulates the four error correction levels
* defined by the QR code standard.</p>
*
* @author Sean Owen
*/
export default class ErrorCorrectionLevel {
private static FOR_BITS = new Map<number, ErrorCorrectionLevel>()
private static FOR_VALUE = new Map<ErrorCorrectionLevelValues, ErrorCorrectionLevel>()
/** L = ~7% correction */
public static L = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.L, "L", 0x01)
/** M = ~15% correction */
public static M = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.M, "M", 0x00)
/** Q = ~25% correction */
public static Q = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.Q, "Q", 0x03)
/** H = ~30% correction */
public static H = new ErrorCorrectionLevel(ErrorCorrectionLevelValues.H, "H", 0x02)
private constructor(private value: ErrorCorrectionLevelValues, private stringValue: string, private bits: number/*int*/) {
ErrorCorrectionLevel.FOR_BITS.set(bits, this)
ErrorCorrectionLevel.FOR_VALUE.set(value, this)
}
public getValue(): ErrorCorrectionLevelValues/*int*/ {
return this.value
}
public getBits(): number/*int*/ {
return this.bits
}
public static fromString(s: string): ErrorCorrectionLevel {
switch(s) {
case "L": return ErrorCorrectionLevel.L
case "M": return ErrorCorrectionLevel.M
case "Q": return ErrorCorrectionLevel.Q
case "H": return ErrorCorrectionLevel.H
default: throw new Exception(Exception.ArgumentException, s + "not available")
}
}
public toString(): string {
return this.stringValue
}
public equals(o: any): boolean {
if (!(o instanceof ErrorCorrectionLevel)) {
return false
}
const other = <ErrorCorrectionLevel> o
return this.value === other.value
}
/**
* @param bits int containing the two bits encoding a QR Code's error correction level
* @return ErrorCorrectionLevel representing the encoded error correction level
*/
public static forBits(bits: number/*int*/): ErrorCorrectionLevel {
if (bits < 0 || bits >= ErrorCorrectionLevel.FOR_BITS.size) {
throw new Exception(Exception.IllegalArgumentException)
}
return ErrorCorrectionLevel.FOR_BITS.get(bits)
}
}

View file

@ -0,0 +1,161 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.decoder {*/
import ErrorCorrectionLevel from './ErrorCorrectionLevel'
import Integer from './../../util/Integer'
import { DataMaskValues } from './DataMask';
/**
* <p>Encapsulates a QR Code's format information, including the data mask used and
* error correction level.</p>
*
* @author Sean Owen
* @see DataMask
* @see ErrorCorrectionLevel
*/
export default class FormatInformation {
private static FORMAT_INFO_MASK_QR = 0x5412
/**
* See ISO 18004:2006, Annex C, Table C.1
*/
private static FORMAT_INFO_DECODE_LOOKUP = [
Int32Array.from([0x5412, 0x00]),
Int32Array.from([0x5125, 0x01]),
Int32Array.from([0x5E7C, 0x02]),
Int32Array.from([0x5B4B, 0x03]),
Int32Array.from([0x45F9, 0x04]),
Int32Array.from([0x40CE, 0x05]),
Int32Array.from([0x4F97, 0x06]),
Int32Array.from([0x4AA0, 0x07]),
Int32Array.from([0x77C4, 0x08]),
Int32Array.from([0x72F3, 0x09]),
Int32Array.from([0x7DAA, 0x0A]),
Int32Array.from([0x789D, 0x0B]),
Int32Array.from([0x662F, 0x0C]),
Int32Array.from([0x6318, 0x0D]),
Int32Array.from([0x6C41, 0x0E]),
Int32Array.from([0x6976, 0x0F]),
Int32Array.from([0x1689, 0x10]),
Int32Array.from([0x13BE, 0x11]),
Int32Array.from([0x1CE7, 0x12]),
Int32Array.from([0x19D0, 0x13]),
Int32Array.from([0x0762, 0x14]),
Int32Array.from([0x0255, 0x15]),
Int32Array.from([0x0D0C, 0x16]),
Int32Array.from([0x083B, 0x17]),
Int32Array.from([0x355F, 0x18]),
Int32Array.from([0x3068, 0x19]),
Int32Array.from([0x3F31, 0x1A]),
Int32Array.from([0x3A06, 0x1B]),
Int32Array.from([0x24B4, 0x1C]),
Int32Array.from([0x2183, 0x1D]),
Int32Array.from([0x2EDA, 0x1E]),
Int32Array.from([0x2BED, 0x1F]),
]
private errorCorrectionLevel: ErrorCorrectionLevel
private dataMask: DataMaskValues/*byte*/
private constructor(formatInfo: number/*int*/) {
// Bits 3,4
this.errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03)
// Bottom 3 bits
this.dataMask = /*(byte) */(formatInfo & 0x07)
}
public static numBitsDiffering(a: number/*int*/, b: number/*int*/): number/*int*/ {
return Integer.bitCount(a ^ b)
}
/**
* @param maskedFormatInfo1 format info indicator, with mask still applied
* @param maskedFormatInfo2 second copy of same info; both are checked at the same time
* to establish best match
* @return information about the format it specifies, or {@code null}
* if doesn't seem to match any known pattern
*/
public static decodeFormatInformation(maskedFormatInfo1: number/*int*/, maskedFormatInfo2: number/*int*/): FormatInformation {
const formatInfo = FormatInformation.doDecodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2)
if (formatInfo !== null) {
return formatInfo
}
// Should return null, but, some QR codes apparently
// do not mask this info. Try again by actually masking the pattern
// first
return FormatInformation.doDecodeFormatInformation(maskedFormatInfo1 ^ FormatInformation.FORMAT_INFO_MASK_QR,
maskedFormatInfo2 ^ FormatInformation.FORMAT_INFO_MASK_QR)
}
private static doDecodeFormatInformation(maskedFormatInfo1: number/*int*/, maskedFormatInfo2: number/*int*/): FormatInformation {
// Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
let bestDifference = Number.MAX_SAFE_INTEGER
let bestFormatInfo = 0
for (const decodeInfo of FormatInformation.FORMAT_INFO_DECODE_LOOKUP) {
const targetInfo = decodeInfo[0]
if (targetInfo === maskedFormatInfo1 || targetInfo === maskedFormatInfo2) {
// Found an exact match
return new FormatInformation(decodeInfo[1])
}
let bitsDifference = FormatInformation.numBitsDiffering(maskedFormatInfo1, targetInfo)
if (bitsDifference < bestDifference) {
bestFormatInfo = decodeInfo[1]
bestDifference = bitsDifference
}
if (maskedFormatInfo1 != maskedFormatInfo2) {
// also try the other option
bitsDifference = FormatInformation.numBitsDiffering(maskedFormatInfo2, targetInfo)
if (bitsDifference < bestDifference) {
bestFormatInfo = decodeInfo[1]
bestDifference = bitsDifference
}
}
}
// Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits
// differing means we found a match
if (bestDifference <= 3) {
return new FormatInformation(bestFormatInfo)
}
return null
}
public getErrorCorrectionLevel(): ErrorCorrectionLevel {
return this.errorCorrectionLevel
}
public getDataMask(): DataMaskValues/*byte*/ {
return this.dataMask
}
/*@Override*/
public hashCode(): number/*int*/ {
return (this.errorCorrectionLevel.getBits() << 3) | this.dataMask
}
/*@Override*/
public equals(o: Object): boolean {
if (!(o instanceof FormatInformation)) {
return false
}
const other = <FormatInformation>o
return this.errorCorrectionLevel === other.errorCorrectionLevel &&
this.dataMask === other.dataMask
}
}

View file

@ -0,0 +1,114 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.decoder {*/
import Exception from './../../Exception'
import Version from './Version'
export const enum ModeValues {
TERMINATOR, // Not really a mode...
NUMERIC,
ALPHANUMERIC,
STRUCTURED_APPEND, // Not supported
BYTE,
ECI, // character counts don't apply
KANJI,
FNC1_FIRST_POSITION,
FNC1_SECOND_POSITION,
/** See GBT 18284-2000; "Hanzi" is a transliteration of this mode name. */
HANZI
}
/**
* <p>See ISO 18004:2006, 6.4.1, Tables 2 and 3. This enum encapsulates the various modes in which
* data can be encoded to bits in the QR code standard.</p>
*
* @author Sean Owen
*/
export default class Mode {
private static FOR_BITS = new Map<number, Mode>()
private static FOR_VALUE = new Map<ModeValues, Mode>()
public static TERMINATOR = new Mode(ModeValues.TERMINATOR, "TERMINATOR", Int32Array.from([0, 0, 0]), 0x00) // Not really a mode...
public static NUMERIC = new Mode(ModeValues.NUMERIC, "NUMERIC", Int32Array.from([10, 12, 14]), 0x01)
public static ALPHANUMERIC = new Mode(ModeValues.ALPHANUMERIC, "ALPHANUMERIC", Int32Array.from([9, 11, 13]), 0x02)
public static STRUCTURED_APPEND = new Mode(ModeValues.STRUCTURED_APPEND, "STRUCTURED_APPEND", Int32Array.from([0, 0, 0]), 0x03) // Not supported
public static BYTE = new Mode(ModeValues.BYTE, "BYTE", Int32Array.from([8, 16, 16]), 0x04)
public static ECI = new Mode(ModeValues.ECI, "ECI", Int32Array.from([0, 0, 0]), 0x07) // character counts don't apply
public static KANJI = new Mode(ModeValues.KANJI, "KANJI", Int32Array.from([8, 10, 12]), 0x08)
public static FNC1_FIRST_POSITION = new Mode(ModeValues.FNC1_FIRST_POSITION, "FNC1_FIRST_POSITION", Int32Array.from([0, 0, 0]), 0x05)
public static FNC1_SECOND_POSITION = new Mode(ModeValues.FNC1_SECOND_POSITION, "FNC1_SECOND_POSITION", Int32Array.from([0, 0, 0]), 0x09)
/** See GBT 18284-2000; "Hanzi" is a transliteration of this mode name. */
public static HANZI = new Mode(ModeValues.HANZI, "HANZI", Int32Array.from([8, 10, 12]), 0x0D)
private constructor(private value: ModeValues, private stringValue: string, private characterCountBitsForVersions: Int32Array, private bits: number/*int*/) {
Mode.FOR_BITS.set(bits, this)
Mode.FOR_VALUE.set(value, this)
}
/**
* @param bits four bits encoding a QR Code data mode
* @return Mode encoded by these bits
* @throws IllegalArgumentException if bits do not correspond to a known mode
*/
public static forBits(bits: number/*int*/): Mode {
const mode = Mode.FOR_BITS.get(bits)
if (undefined === mode) {
throw new Exception(Exception.IllegalArgumentException)
}
return mode
}
/**
* @param version version in question
* @return number of bits used, in this QR Code symbol {@link Version}, to encode the
* count of characters that will follow encoded in this Mode
*/
public getCharacterCountBits(version: Version): number/*int*/ {
const number = version.getVersionNumber()
let offset
if (number <= 9) {
offset = 0
} else if (number <= 26) {
offset = 1
} else {
offset = 2
}
return this.characterCountBitsForVersions[offset]
}
public getValue(): ModeValues/*int*/ {
return this.value
}
public getBits(): number/*int*/ {
return this.bits
}
public equals(o: any): boolean {
if (!(o instanceof Mode)) {
return false
}
const other = <Mode> o
return this.value === other.value
}
public toString(): string {
return this.stringValue
}
}

View file

@ -0,0 +1,54 @@
/*
* Copyright 2013 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.decoder {*/
import ResultPoint from './../../ResultPoint'
/**
* Meta-data container for QR Code decoding. Instances of this class may be used to convey information back to the
* decoding caller. Callers are expected to process this.
*
* @see com.google.zxing.common.DecoderResult#getOther()
*/
export default class QRCodeDecoderMetaData {
public constructor(private mirrored: boolean) {}
/**
* @return true if the QR Code was mirrored.
*/
public isMirrored(): boolean {
return this.mirrored
}
/**
* Apply the result points' order correction due to mirroring.
*
* @param points Array of points to apply mirror correction to.
*/
public applyMirroredCorrection(points: Array<ResultPoint>): void {
if (!this.mirrored || points === null || points.length < 3) {
return
}
const bottomLeft = points[0]
points[0] = points[2]
points[2] = bottomLeft
// No need to 'fix' top-left and alignment pattern.
}
}

View file

@ -0,0 +1,515 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.decoder {*/
import BitMatrix from './../../common/BitMatrix'
import Exception from './../../Exception'
import ErrorCorrectionLevel from './ErrorCorrectionLevel'
import FormatInformation from './FormatInformation'
import ECBlocks from './ECBlocks'
import ECB from './ECB'
/**
* See ISO 18004:2006 Annex D
*
* @author Sean Owen
*/
export default class Version {
/**
* See ISO 18004:2006 Annex D.
* Element i represents the raw version bits that specify version i + 7
*/
private static VERSION_DECODE_INFO = Int32Array.from([
0x07C94, 0x085BC, 0x09A99, 0x0A4D3, 0x0BBF6,
0x0C762, 0x0D847, 0x0E60D, 0x0F928, 0x10B78,
0x1145D, 0x12A17, 0x13532, 0x149A6, 0x15683,
0x168C9, 0x177EC, 0x18EC4, 0x191E1, 0x1AFAB,
0x1B08E, 0x1CC1A, 0x1D33F, 0x1ED75, 0x1F250,
0x209D5, 0x216F0, 0x228BA, 0x2379F, 0x24B0B,
0x2542E, 0x26A64, 0x27541, 0x28C69])
/**
* See ISO 18004:2006 6.5.1 Table 9
*/
private static VERSIONS: Version[] = [
new Version(1, new Int32Array(0),
new ECBlocks(7, new ECB(1, 19)),
new ECBlocks(10, new ECB(1, 16)),
new ECBlocks(13, new ECB(1, 13)),
new ECBlocks(17, new ECB(1, 9))),
new Version(2, Int32Array.from([6, 18]),
new ECBlocks(10, new ECB(1, 34)),
new ECBlocks(16, new ECB(1, 28)),
new ECBlocks(22, new ECB(1, 22)),
new ECBlocks(28, new ECB(1, 16))),
new Version(3, Int32Array.from([6, 22]),
new ECBlocks(15, new ECB(1, 55)),
new ECBlocks(26, new ECB(1, 44)),
new ECBlocks(18, new ECB(2, 17)),
new ECBlocks(22, new ECB(2, 13))),
new Version(4, Int32Array.from([6, 26]),
new ECBlocks(20, new ECB(1, 80)),
new ECBlocks(18, new ECB(2, 32)),
new ECBlocks(26, new ECB(2, 24)),
new ECBlocks(16, new ECB(4, 9))),
new Version(5, Int32Array.from([6, 30]),
new ECBlocks(26, new ECB(1, 108)),
new ECBlocks(24, new ECB(2, 43)),
new ECBlocks(18, new ECB(2, 15),
new ECB(2, 16)),
new ECBlocks(22, new ECB(2, 11),
new ECB(2, 12))),
new Version(6, Int32Array.from([6, 34]),
new ECBlocks(18, new ECB(2, 68)),
new ECBlocks(16, new ECB(4, 27)),
new ECBlocks(24, new ECB(4, 19)),
new ECBlocks(28, new ECB(4, 15))),
new Version(7, Int32Array.from([6, 22, 38]),
new ECBlocks(20, new ECB(2, 78)),
new ECBlocks(18, new ECB(4, 31)),
new ECBlocks(18, new ECB(2, 14),
new ECB(4, 15)),
new ECBlocks(26, new ECB(4, 13),
new ECB(1, 14))),
new Version(8, Int32Array.from([6, 24, 42]),
new ECBlocks(24, new ECB(2, 97)),
new ECBlocks(22, new ECB(2, 38),
new ECB(2, 39)),
new ECBlocks(22, new ECB(4, 18),
new ECB(2, 19)),
new ECBlocks(26, new ECB(4, 14),
new ECB(2, 15))),
new Version(9, Int32Array.from([6, 26, 46]),
new ECBlocks(30, new ECB(2, 116)),
new ECBlocks(22, new ECB(3, 36),
new ECB(2, 37)),
new ECBlocks(20, new ECB(4, 16),
new ECB(4, 17)),
new ECBlocks(24, new ECB(4, 12),
new ECB(4, 13))),
new Version(10, Int32Array.from([6, 28, 50]),
new ECBlocks(18, new ECB(2, 68),
new ECB(2, 69)),
new ECBlocks(26, new ECB(4, 43),
new ECB(1, 44)),
new ECBlocks(24, new ECB(6, 19),
new ECB(2, 20)),
new ECBlocks(28, new ECB(6, 15),
new ECB(2, 16))),
new Version(11, Int32Array.from([6, 30, 54]),
new ECBlocks(20, new ECB(4, 81)),
new ECBlocks(30, new ECB(1, 50),
new ECB(4, 51)),
new ECBlocks(28, new ECB(4, 22),
new ECB(4, 23)),
new ECBlocks(24, new ECB(3, 12),
new ECB(8, 13))),
new Version(12, Int32Array.from([6, 32, 58]),
new ECBlocks(24, new ECB(2, 92),
new ECB(2, 93)),
new ECBlocks(22, new ECB(6, 36),
new ECB(2, 37)),
new ECBlocks(26, new ECB(4, 20),
new ECB(6, 21)),
new ECBlocks(28, new ECB(7, 14),
new ECB(4, 15))),
new Version(13, Int32Array.from([6, 34, 62]),
new ECBlocks(26, new ECB(4, 107)),
new ECBlocks(22, new ECB(8, 37),
new ECB(1, 38)),
new ECBlocks(24, new ECB(8, 20),
new ECB(4, 21)),
new ECBlocks(22, new ECB(12, 11),
new ECB(4, 12))),
new Version(14, Int32Array.from([6, 26, 46, 66]),
new ECBlocks(30, new ECB(3, 115),
new ECB(1, 116)),
new ECBlocks(24, new ECB(4, 40),
new ECB(5, 41)),
new ECBlocks(20, new ECB(11, 16),
new ECB(5, 17)),
new ECBlocks(24, new ECB(11, 12),
new ECB(5, 13))),
new Version(15, Int32Array.from([6, 26, 48, 70]),
new ECBlocks(22, new ECB(5, 87),
new ECB(1, 88)),
new ECBlocks(24, new ECB(5, 41),
new ECB(5, 42)),
new ECBlocks(30, new ECB(5, 24),
new ECB(7, 25)),
new ECBlocks(24, new ECB(11, 12),
new ECB(7, 13))),
new Version(16, Int32Array.from([6, 26, 50, 74]),
new ECBlocks(24, new ECB(5, 98),
new ECB(1, 99)),
new ECBlocks(28, new ECB(7, 45),
new ECB(3, 46)),
new ECBlocks(24, new ECB(15, 19),
new ECB(2, 20)),
new ECBlocks(30, new ECB(3, 15),
new ECB(13, 16))),
new Version(17, Int32Array.from([6, 30, 54, 78]),
new ECBlocks(28, new ECB(1, 107),
new ECB(5, 108)),
new ECBlocks(28, new ECB(10, 46),
new ECB(1, 47)),
new ECBlocks(28, new ECB(1, 22),
new ECB(15, 23)),
new ECBlocks(28, new ECB(2, 14),
new ECB(17, 15))),
new Version(18, Int32Array.from([6, 30, 56, 82]),
new ECBlocks(30, new ECB(5, 120),
new ECB(1, 121)),
new ECBlocks(26, new ECB(9, 43),
new ECB(4, 44)),
new ECBlocks(28, new ECB(17, 22),
new ECB(1, 23)),
new ECBlocks(28, new ECB(2, 14),
new ECB(19, 15))),
new Version(19, Int32Array.from([6, 30, 58, 86]),
new ECBlocks(28, new ECB(3, 113),
new ECB(4, 114)),
new ECBlocks(26, new ECB(3, 44),
new ECB(11, 45)),
new ECBlocks(26, new ECB(17, 21),
new ECB(4, 22)),
new ECBlocks(26, new ECB(9, 13),
new ECB(16, 14))),
new Version(20, Int32Array.from([6, 34, 62, 90]),
new ECBlocks(28, new ECB(3, 107),
new ECB(5, 108)),
new ECBlocks(26, new ECB(3, 41),
new ECB(13, 42)),
new ECBlocks(30, new ECB(15, 24),
new ECB(5, 25)),
new ECBlocks(28, new ECB(15, 15),
new ECB(10, 16))),
new Version(21, Int32Array.from([6, 28, 50, 72, 94]),
new ECBlocks(28, new ECB(4, 116),
new ECB(4, 117)),
new ECBlocks(26, new ECB(17, 42)),
new ECBlocks(28, new ECB(17, 22),
new ECB(6, 23)),
new ECBlocks(30, new ECB(19, 16),
new ECB(6, 17))),
new Version(22, Int32Array.from([6, 26, 50, 74, 98]),
new ECBlocks(28, new ECB(2, 111),
new ECB(7, 112)),
new ECBlocks(28, new ECB(17, 46)),
new ECBlocks(30, new ECB(7, 24),
new ECB(16, 25)),
new ECBlocks(24, new ECB(34, 13))),
new Version(23, Int32Array.from([6, 30, 54, 78, 102]),
new ECBlocks(30, new ECB(4, 121),
new ECB(5, 122)),
new ECBlocks(28, new ECB(4, 47),
new ECB(14, 48)),
new ECBlocks(30, new ECB(11, 24),
new ECB(14, 25)),
new ECBlocks(30, new ECB(16, 15),
new ECB(14, 16))),
new Version(24, Int32Array.from([6, 28, 54, 80, 106]),
new ECBlocks(30, new ECB(6, 117),
new ECB(4, 118)),
new ECBlocks(28, new ECB(6, 45),
new ECB(14, 46)),
new ECBlocks(30, new ECB(11, 24),
new ECB(16, 25)),
new ECBlocks(30, new ECB(30, 16),
new ECB(2, 17))),
new Version(25, Int32Array.from([6, 32, 58, 84, 110]),
new ECBlocks(26, new ECB(8, 106),
new ECB(4, 107)),
new ECBlocks(28, new ECB(8, 47),
new ECB(13, 48)),
new ECBlocks(30, new ECB(7, 24),
new ECB(22, 25)),
new ECBlocks(30, new ECB(22, 15),
new ECB(13, 16))),
new Version(26, Int32Array.from([6, 30, 58, 86, 114]),
new ECBlocks(28, new ECB(10, 114),
new ECB(2, 115)),
new ECBlocks(28, new ECB(19, 46),
new ECB(4, 47)),
new ECBlocks(28, new ECB(28, 22),
new ECB(6, 23)),
new ECBlocks(30, new ECB(33, 16),
new ECB(4, 17))),
new Version(27, Int32Array.from([6, 34, 62, 90, 118]),
new ECBlocks(30, new ECB(8, 122),
new ECB(4, 123)),
new ECBlocks(28, new ECB(22, 45),
new ECB(3, 46)),
new ECBlocks(30, new ECB(8, 23),
new ECB(26, 24)),
new ECBlocks(30, new ECB(12, 15),
new ECB(28, 16))),
new Version(28, Int32Array.from([6, 26, 50, 74, 98, 122]),
new ECBlocks(30, new ECB(3, 117),
new ECB(10, 118)),
new ECBlocks(28, new ECB(3, 45),
new ECB(23, 46)),
new ECBlocks(30, new ECB(4, 24),
new ECB(31, 25)),
new ECBlocks(30, new ECB(11, 15),
new ECB(31, 16))),
new Version(29, Int32Array.from([6, 30, 54, 78, 102, 126]),
new ECBlocks(30, new ECB(7, 116),
new ECB(7, 117)),
new ECBlocks(28, new ECB(21, 45),
new ECB(7, 46)),
new ECBlocks(30, new ECB(1, 23),
new ECB(37, 24)),
new ECBlocks(30, new ECB(19, 15),
new ECB(26, 16))),
new Version(30, Int32Array.from([6, 26, 52, 78, 104, 130]),
new ECBlocks(30, new ECB(5, 115),
new ECB(10, 116)),
new ECBlocks(28, new ECB(19, 47),
new ECB(10, 48)),
new ECBlocks(30, new ECB(15, 24),
new ECB(25, 25)),
new ECBlocks(30, new ECB(23, 15),
new ECB(25, 16))),
new Version(31, Int32Array.from([6, 30, 56, 82, 108, 134]),
new ECBlocks(30, new ECB(13, 115),
new ECB(3, 116)),
new ECBlocks(28, new ECB(2, 46),
new ECB(29, 47)),
new ECBlocks(30, new ECB(42, 24),
new ECB(1, 25)),
new ECBlocks(30, new ECB(23, 15),
new ECB(28, 16))),
new Version(32, Int32Array.from([6, 34, 60, 86, 112, 138]),
new ECBlocks(30, new ECB(17, 115)),
new ECBlocks(28, new ECB(10, 46),
new ECB(23, 47)),
new ECBlocks(30, new ECB(10, 24),
new ECB(35, 25)),
new ECBlocks(30, new ECB(19, 15),
new ECB(35, 16))),
new Version(33, Int32Array.from([6, 30, 58, 86, 114, 142]),
new ECBlocks(30, new ECB(17, 115),
new ECB(1, 116)),
new ECBlocks(28, new ECB(14, 46),
new ECB(21, 47)),
new ECBlocks(30, new ECB(29, 24),
new ECB(19, 25)),
new ECBlocks(30, new ECB(11, 15),
new ECB(46, 16))),
new Version(34, Int32Array.from([6, 34, 62, 90, 118, 146]),
new ECBlocks(30, new ECB(13, 115),
new ECB(6, 116)),
new ECBlocks(28, new ECB(14, 46),
new ECB(23, 47)),
new ECBlocks(30, new ECB(44, 24),
new ECB(7, 25)),
new ECBlocks(30, new ECB(59, 16),
new ECB(1, 17))),
new Version(35, Int32Array.from([6, 30, 54, 78, 102, 126, 150]),
new ECBlocks(30, new ECB(12, 121),
new ECB(7, 122)),
new ECBlocks(28, new ECB(12, 47),
new ECB(26, 48)),
new ECBlocks(30, new ECB(39, 24),
new ECB(14, 25)),
new ECBlocks(30, new ECB(22, 15),
new ECB(41, 16))),
new Version(36, Int32Array.from([6, 24, 50, 76, 102, 128, 154]),
new ECBlocks(30, new ECB(6, 121),
new ECB(14, 122)),
new ECBlocks(28, new ECB(6, 47),
new ECB(34, 48)),
new ECBlocks(30, new ECB(46, 24),
new ECB(10, 25)),
new ECBlocks(30, new ECB(2, 15),
new ECB(64, 16))),
new Version(37, Int32Array.from([6, 28, 54, 80, 106, 132, 158]),
new ECBlocks(30, new ECB(17, 122),
new ECB(4, 123)),
new ECBlocks(28, new ECB(29, 46),
new ECB(14, 47)),
new ECBlocks(30, new ECB(49, 24),
new ECB(10, 25)),
new ECBlocks(30, new ECB(24, 15),
new ECB(46, 16))),
new Version(38, Int32Array.from([6, 32, 58, 84, 110, 136, 162]),
new ECBlocks(30, new ECB(4, 122),
new ECB(18, 123)),
new ECBlocks(28, new ECB(13, 46),
new ECB(32, 47)),
new ECBlocks(30, new ECB(48, 24),
new ECB(14, 25)),
new ECBlocks(30, new ECB(42, 15),
new ECB(32, 16))),
new Version(39, Int32Array.from([6, 26, 54, 82, 110, 138, 166]),
new ECBlocks(30, new ECB(20, 117),
new ECB(4, 118)),
new ECBlocks(28, new ECB(40, 47),
new ECB(7, 48)),
new ECBlocks(30, new ECB(43, 24),
new ECB(22, 25)),
new ECBlocks(30, new ECB(10, 15),
new ECB(67, 16))),
new Version(40, Int32Array.from([6, 30, 58, 86, 114, 142, 170]),
new ECBlocks(30, new ECB(19, 118),
new ECB(6, 119)),
new ECBlocks(28, new ECB(18, 47),
new ECB(31, 48)),
new ECBlocks(30, new ECB(34, 24),
new ECB(34, 25)),
new ECBlocks(30, new ECB(20, 15),
new ECB(61, 16)))
]
private ecBlocks: ECBlocks[]
private totalCodewords: number/*int*/
private constructor(private versionNumber: number/*int*/,
private alignmentPatternCenters: Int32Array,
...ecBlocks: ECBlocks[]) {
this.ecBlocks = ecBlocks
let total = 0
const ecCodewords = ecBlocks[0].getECCodewordsPerBlock()
const ecbArray: ECB[] = ecBlocks[0].getECBlocks()
for (const ecBlock of ecbArray) {
total += ecBlock.getCount() * (ecBlock.getDataCodewords() + ecCodewords);
}
this.totalCodewords = total
}
public getVersionNumber(): number/*int*/ {
return this.versionNumber
}
public getAlignmentPatternCenters(): Int32Array {
return this.alignmentPatternCenters
}
public getTotalCodewords(): number/*int*/ {
return this.totalCodewords
}
public getDimensionForVersion(): number/*int*/ {
return 17 + 4 * this.versionNumber;
}
public getECBlocksForLevel(ecLevel: ErrorCorrectionLevel): ECBlocks {
return this.ecBlocks[ecLevel.getValue()]
// TYPESCRIPTPORT: original was using ordinal, and using the order of levels as defined in ErrorCorrectionLevel enum (LMQH)
// I will use the direct value from ErrorCorrectionLevelValues enum which in typescript goes to a number
}
/**
* <p>Deduces version information purely from QR Code dimensions.</p>
*
* @param dimension dimension in modules
* @return Version for a QR Code of that dimension
* @throws FormatException if dimension is not 1 mod 4
*/
public static getProvisionalVersionForDimension(dimension: number/*int*/): Version /*throws FormatException */ {
if (dimension % 4 != 1) {
throw new Exception(Exception.FormatException)
}
try {
return this.getVersionForNumber((dimension - 17) / 4)
} catch (ignored/*: IllegalArgumentException*/) {
throw new Exception(Exception.FormatException)
}
}
public static getVersionForNumber(versionNumber: number/*int*/): Version {
if (versionNumber < 1 || versionNumber > 40) {
throw new Exception(Exception.IllegalArgumentException)
}
return Version.VERSIONS[versionNumber - 1]
}
public static decodeVersionInformation(versionBits: number/*int*/): Version {
let bestDifference = Number.MAX_SAFE_INTEGER
let bestVersion = 0
for (let i = 0; i < Version.VERSION_DECODE_INFO.length; i++) {
const targetVersion = Version.VERSION_DECODE_INFO[i]
// Do the version info bits match exactly? done.
if (targetVersion === versionBits) {
return Version.getVersionForNumber(i + 7)
}
// Otherwise see if this is the closest to a real version info bit string
// we have seen so far
const bitsDifference = FormatInformation.numBitsDiffering(versionBits, targetVersion)
if (bitsDifference < bestDifference) {
bestVersion = i + 7
bestDifference = bitsDifference
}
}
// We can tolerate up to 3 bits of error since no two version info codewords will
// differ in less than 8 bits.
if (bestDifference <= 3) {
return Version.getVersionForNumber(bestVersion)
}
// If we didn't find a close enough match, fail
return null
}
/**
* See ISO 18004:2006 Annex E
*/
public buildFunctionPattern(): BitMatrix {
const dimension = this.getDimensionForVersion()
const bitMatrix = new BitMatrix(dimension)
// Top left finder pattern + separator + format
bitMatrix.setRegion(0, 0, 9, 9)
// Top right finder pattern + separator + format
bitMatrix.setRegion(dimension - 8, 0, 8, 9)
// Bottom left finder pattern + separator + format
bitMatrix.setRegion(0, dimension - 8, 9, 8)
// Alignment patterns
const max = this.alignmentPatternCenters.length
for (let x = 0; x < max; x++) {
const i = this.alignmentPatternCenters[x] - 2
for (let y = 0; y < max; y++) {
if ((x == 0 && (y == 0 || y == max - 1)) || (x == max - 1 && y == 0)) {
// No alignment patterns near the three finder patterns
continue
}
bitMatrix.setRegion(this.alignmentPatternCenters[y] - 2, i, 5, 5)
}
}
// Vertical timing pattern
bitMatrix.setRegion(6, 9, 1, dimension - 17)
// Horizontal timing pattern
bitMatrix.setRegion(9, 6, dimension - 17, 1)
if (this.versionNumber > 6) {
// Version info, top right
bitMatrix.setRegion(dimension - 11, 0, 3, 6)
// Version info, bottom left
bitMatrix.setRegion(0, dimension - 11, 6, 3)
}
return bitMatrix
}
/*@Override*/
public toString(): string {
return "" + this.versionNumber
}
}

View file

@ -0,0 +1,56 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.detector {*/
import ResultPoint from './../../ResultPoint'
/**
* <p>Encapsulates an alignment pattern, which are the smaller square patterns found in
* all but the simplest QR Codes.</p>
*
* @author Sean Owen
*/
export default class AlignmentPattern extends ResultPoint {
public constructor(posX: number/*float*/, posY: number/*float*/, private estimatedModuleSize: number/*float*/) {
super(posX, posY)
}
/**
* <p>Determines if this alignment pattern "about equals" an alignment pattern at the stated
* position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
*/
public aboutEquals(moduleSize: number/*float*/, i: number/*float*/, j: number/*float*/): boolean {
if (Math.abs(i - this.getY()) <= moduleSize && Math.abs(j - this.getX()) <= moduleSize) {
const moduleSizeDiff: number/*float*/ = Math.abs(moduleSize - this.estimatedModuleSize)
return moduleSizeDiff <= 1.0 || moduleSizeDiff <= this.estimatedModuleSize
}
return false
}
/**
* Combines this object's current estimate of a finder pattern position and module size
* with a new estimate. It returns a new {@code FinderPattern} containing an average of the two.
*/
public combineEstimate(i: number/*float*/, j: number/*float*/, newModuleSize: number/*float*/): AlignmentPattern {
const combinedX: number/*float*/ = (this.getX() + j) / 2.0
const combinedY: number/*float*/ = (this.getY() + i) / 2.0
const combinedModuleSize: number/*float*/ = (this.estimatedModuleSize + newModuleSize) / 2.0
return new AlignmentPattern(combinedX, combinedY, combinedModuleSize)
}
}

View file

@ -0,0 +1,267 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.detector {*/
import ResultPointCallback from './../../ResultPointCallback'
import BitMatrix from './../../common/BitMatrix'
import AlignmentPattern from './AlignmentPattern'
import Exception from './../../Exception'
/*import java.util.ArrayList;*/
/*import java.util.List;*/
/**
* <p>This class attempts to find alignment patterns in a QR Code. Alignment patterns look like finder
* patterns but are smaller and appear at regular intervals throughout the image.</p>
*
* <p>At the moment this only looks for the bottom-right alignment pattern.</p>
*
* <p>This is mostly a simplified copy of {@link FinderPatternFinder}. It is copied,
* pasted and stripped down here for maximum performance but does unfortunately duplicate
* some code.</p>
*
* <p>This class is thread-safe but not reentrant. Each thread must allocate its own object.</p>
*
* @author Sean Owen
*/
export default class AlignmentPatternFinder {
private possibleCenters: AlignmentPattern[]
private crossCheckStateCount: Int32Array
/**
* <p>Creates a finder that will look in a portion of the whole image.</p>
*
* @param image image to search
* @param startX left column from which to start searching
* @param startY top row from which to start searching
* @param width width of region to search
* @param height height of region to search
* @param moduleSize estimated module size so far
*/
public constructor(private image: BitMatrix,
private startX: number/*int*/,
private startY: number/*int*/,
private width: number/*int*/,
private height: number/*int*/,
private moduleSize: number/*float*/,
private resultPointCallback: ResultPointCallback) {
this.possibleCenters = []//new Array<any>(5)
// TYPESCRIPTPORT: array initialization without size as the length is checked below
this.crossCheckStateCount = new Int32Array(3)
}
/**
* <p>This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since
* it's pretty performance-critical and so is written to be fast foremost.</p>
*
* @return {@link AlignmentPattern} if found
* @throws NotFoundException if not found
*/
public find(): AlignmentPattern /*throws NotFoundException*/ {
const startX = this.startX
const height = this.height
const width = this.width
const maxJ = startX + width
const middleI = this.startY + (height / 2)
// We are looking for black/white/black modules in 1:1:1 ratio
// this tracks the number of black/white/black modules seen so far
const stateCount = new Int32Array(3)
const image = this.image
for (let iGen = 0; iGen < height; iGen++) {
// Search from middle outwards
const i = middleI + ((iGen & 0x01) == 0 ? Math.floor((iGen + 1) / 2) : -Math.floor((iGen + 1) / 2))
stateCount[0] = 0
stateCount[1] = 0
stateCount[2] = 0
let j = startX
// Burn off leading white pixels before anything else; if we start in the middle of
// a white run, it doesn't make sense to count its length, since we don't know if the
// white run continued to the left of the start point
while (j < maxJ && !image.get(j, i)) {
j++
}
let currentState = 0
while (j < maxJ) {
if (image.get(j, i)) {
// Black pixel
if (currentState === 1) { // Counting black pixels
stateCount[1]++
} else { // Counting white pixels
if (currentState === 2) { // A winner?
if (this.foundPatternCross(stateCount)) { // Yes
const confirmed = this.handlePossibleCenter(stateCount, i, j)
if (confirmed !== null) {
return confirmed
}
}
stateCount[0] = stateCount[2]
stateCount[1] = 1
stateCount[2] = 0
currentState = 1
} else {
stateCount[++currentState]++
}
}
} else { // White pixel
if (currentState === 1) { // Counting black pixels
currentState++
}
stateCount[currentState]++
}
j++
}
if (this.foundPatternCross(stateCount)) {
const confirmed = this.handlePossibleCenter(stateCount, i, maxJ)
if (confirmed !== null) {
return confirmed
}
}
}
// Hmm, nothing we saw was observed and confirmed twice. If we had
// any guess at all, return it.
if (this.possibleCenters.length !== 0) {
return this.possibleCenters[0]
}
throw new Exception(Exception.NotFoundException)
}
/**
* Given a count of black/white/black pixels just seen and an end position,
* figures the location of the center of this black/white/black run.
*/
private static centerFromEnd(stateCount: Int32Array, end: number/*int*/): number/*float*/ {
return (end - stateCount[2]) - stateCount[1] / 2.0
}
/**
* @param stateCount count of black/white/black pixels just read
* @return true iff the proportions of the counts is close enough to the 1/1/1 ratios
* used by alignment patterns to be considered a match
*/
private foundPatternCross(stateCount: Int32Array): boolean {
const moduleSize: number/*float*/ = this.moduleSize
const maxVariance: number/*float*/ = moduleSize / 2.0
for (let i = 0; i < 3; i++) {
if (Math.abs(moduleSize - stateCount[i]) >= maxVariance) {
return false
}
}
return true
}
/**
* <p>After a horizontal scan finds a potential alignment pattern, this method
* "cross-checks" by scanning down vertically through the center of the possible
* alignment pattern to see if the same proportion is detected.</p>
*
* @param startI row where an alignment pattern was detected
* @param centerJ center of the section that appears to cross an alignment pattern
* @param maxCount maximum reasonable number of modules that should be
* observed in any reading state, based on the results of the horizontal scan
* @return vertical center of alignment pattern, or {@link Float#NaN} if not found
*/
private crossCheckVertical(startI: number/*int*/, centerJ: number/*int*/, maxCount: number/*int*/,
originalStateCountTotal: number/*int*/): number/*float*/ {
const image = this.image
const maxI = image.getHeight()
const stateCount = this.crossCheckStateCount
stateCount[0] = 0
stateCount[1] = 0
stateCount[2] = 0
// Start counting up from center
let i = startI
while (i >= 0 && image.get(centerJ, i) && stateCount[1] <= maxCount) {
stateCount[1]++
i--
}
// If already too many modules in this state or ran off the edge:
if (i < 0 || stateCount[1] > maxCount) {
return NaN
}
while (i >= 0 && !image.get(centerJ, i) && stateCount[0] <= maxCount) {
stateCount[0]++
i--
}
if (stateCount[0] > maxCount) {
return NaN
}
// Now also count down from center
i = startI + 1
while (i < maxI && image.get(centerJ, i) && stateCount[1] <= maxCount) {
stateCount[1]++
i++
}
if (i == maxI || stateCount[1] > maxCount) {
return NaN
}
while (i < maxI && !image.get(centerJ, i) && stateCount[2] <= maxCount) {
stateCount[2]++
i++
}
if (stateCount[2] > maxCount) {
return NaN
}
const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]
if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) {
return NaN
}
return this.foundPatternCross(stateCount) ? AlignmentPatternFinder.centerFromEnd(stateCount, i) : NaN
}
/**
* <p>This is called when a horizontal scan finds a possible alignment pattern. It will
* cross check with a vertical scan, and if successful, will see if this pattern had been
* found on a previous horizontal scan. If so, we consider it confirmed and conclude we have
* found the alignment pattern.</p>
*
* @param stateCount reading state module counts from horizontal scan
* @param i row where alignment pattern may be found
* @param j end of possible alignment pattern in row
* @return {@link AlignmentPattern} if we have found the same pattern twice, or null if not
*/
private handlePossibleCenter(stateCount: Int32Array, i: number/*int*/, j: number/*int*/): AlignmentPattern {
const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]
const centerJ: number/*float*/ = AlignmentPatternFinder.centerFromEnd(stateCount, j)
const centerI: number/*float*/ = this.crossCheckVertical(i, /*(int) */centerJ, 2 * stateCount[1], stateCountTotal);
if (!isNaN(centerI)) {
const estimatedModuleSize: number/*float*/ = (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0
for (const center of this.possibleCenters) {
// Look for about the same center and module size:
if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) {
return center.combineEstimate(centerI, centerJ, estimatedModuleSize)
}
}
// Hadn't found this before; save it
const point = new AlignmentPattern(centerJ, centerI, estimatedModuleSize)
this.possibleCenters.push(point)
if (this.resultPointCallback !== null && this.resultPointCallback !== undefined) {
this.resultPointCallback.foundPossibleResultPoint(point)
}
}
return null
}
}

View file

@ -0,0 +1,410 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.detector {*/
import DecodeHintType from './../../DecodeHintType'
import ResultPoint from './../../ResultPoint'
import ResultPointCallback from './../../ResultPointCallback'
import BitMatrix from './../../common/BitMatrix'
import DetectorResult from './../../common/DetectorResult'
import GridSampler from './../../common/GridSampler'
import GridSamplerInstance from './../../common/GridSamplerInstance'
import PerspectiveTransform from './../../common/PerspectiveTransform'
import MathUtils from './../../common/detector/MathUtils'
import Version from './../decoder/Version'
import FinderPatternFinder from './FinderPatternFinder'
import FinderPatternInfo from './FinderPatternInfo'
import FinderPattern from './FinderPattern'
import Exception from './../../Exception'
import AlignmentPattern from './AlignmentPattern'
import AlignmentPatternFinder from './AlignmentPatternFinder'
/*import java.util.Map;*/
/**
* <p>Encapsulates logic that can detect a QR Code in an image, even if the QR Code
* is rotated or skewed, or partially obscured.</p>
*
* @author Sean Owen
*/
export default class Detector {
private resultPointCallback: ResultPointCallback
public constructor(private image: BitMatrix) {}
protected getImage(): BitMatrix {
return this.image
}
protected getResultPointCallback(): ResultPointCallback {
return this.resultPointCallback
}
/**
* <p>Detects a QR Code in an image.</p>
*
* @return {@link DetectorResult} encapsulating results of detecting a QR Code
* @throws NotFoundException if QR Code cannot be found
* @throws FormatException if a QR Code cannot be decoded
*/
// public detect(): DetectorResult /*throws NotFoundException, FormatException*/ {
// return detect(null)
// }
/**
* <p>Detects a QR Code in an image.</p>
*
* @param hints optional hints to detector
* @return {@link DetectorResult} encapsulating results of detecting a QR Code
* @throws NotFoundException if QR Code cannot be found
* @throws FormatException if a QR Code cannot be decoded
*/
public detect(hints: Map<DecodeHintType, any>): DetectorResult /*throws NotFoundException, FormatException*/ {
this.resultPointCallback = (hints === null || hints === undefined) ? null :
/*(ResultPointCallback) */hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK)
const finder = new FinderPatternFinder(this.image, this.resultPointCallback)
const info = finder.find(hints)
return this.processFinderPatternInfo(info)
}
protected processFinderPatternInfo(info: FinderPatternInfo): DetectorResult
/*throws NotFoundException, FormatException*/ {
const topLeft: FinderPattern = info.getTopLeft()
const topRight: FinderPattern = info.getTopRight()
const bottomLeft: FinderPattern = info.getBottomLeft()
const moduleSize: number/*float*/ = this.calculateModuleSize(topLeft, topRight, bottomLeft)
if (moduleSize < 1.0) {
throw new Exception(Exception.NotFoundException)
}
const dimension = Detector.computeDimension(topLeft, topRight, bottomLeft, moduleSize)
const provisionalVersion: Version = Version.getProvisionalVersionForDimension(dimension)
const modulesBetweenFPCenters = provisionalVersion.getDimensionForVersion() - 7
let alignmentPattern: AlignmentPattern = null
// Anything above version 1 has an alignment pattern
if (provisionalVersion.getAlignmentPatternCenters().length > 0) {
// Guess where a "bottom right" finder pattern would have been
const bottomRightX: number/*float*/ = topRight.getX() - topLeft.getX() + bottomLeft.getX()
const bottomRightY: number/*float*/ = topRight.getY() - topLeft.getY() + bottomLeft.getY()
// Estimate that alignment pattern is closer by 3 modules
// from "bottom right" to known top left location
const correctionToTopLeft: number/*float*/ = 1.0 - 3.0 / modulesBetweenFPCenters
const estAlignmentX = /*(int) */Math.floor(topLeft.getX() + correctionToTopLeft * (bottomRightX - topLeft.getX()));
const estAlignmentY = /*(int) */Math.floor(topLeft.getY() + correctionToTopLeft * (bottomRightY - topLeft.getY()));
// Kind of arbitrary -- expand search radius before giving up
for (let i = 4; i <= 16; i <<= 1) {
try {
alignmentPattern = this.findAlignmentInRegion(moduleSize,
estAlignmentX,
estAlignmentY,
i)
break
} catch (re/*NotFoundException*/) {
if (!Exception.isOfType(re, Exception.NotFoundException)) {
throw re
}
// try next round
}
}
// If we didn't find alignment pattern... well try anyway without it
}
const transform: PerspectiveTransform =
Detector.createTransform(topLeft, topRight, bottomLeft, alignmentPattern, dimension)
const bits: BitMatrix = Detector.sampleGrid(this.image, transform, dimension)
let points: ResultPoint[]
if (alignmentPattern === null) {
points = [bottomLeft, topLeft, topRight]
} else {
points = [bottomLeft, topLeft, topRight, alignmentPattern]
}
return new DetectorResult(bits, points)
}
private static createTransform(topLeft: ResultPoint,
topRight: ResultPoint,
bottomLeft: ResultPoint,
alignmentPattern: ResultPoint,
dimension: number/*int*/): PerspectiveTransform {
const dimMinusThree: number/*float*/ = dimension - 3.5
let bottomRightX: number/*float*/
let bottomRightY: number/*float*/
let sourceBottomRightX: number/*float*/
let sourceBottomRightY: number/*float*/
if (alignmentPattern !== null) {
bottomRightX = alignmentPattern.getX()
bottomRightY = alignmentPattern.getY()
sourceBottomRightX = dimMinusThree - 3.0
sourceBottomRightY = sourceBottomRightX
} else {
// Don't have an alignment pattern, just make up the bottom-right point
bottomRightX = (topRight.getX() - topLeft.getX()) + bottomLeft.getX()
bottomRightY = (topRight.getY() - topLeft.getY()) + bottomLeft.getY()
sourceBottomRightX = dimMinusThree
sourceBottomRightY = dimMinusThree
}
return PerspectiveTransform.quadrilateralToQuadrilateral(
3.5,
3.5,
dimMinusThree,
3.5,
sourceBottomRightX,
sourceBottomRightY,
3.5,
dimMinusThree,
topLeft.getX(),
topLeft.getY(),
topRight.getX(),
topRight.getY(),
bottomRightX,
bottomRightY,
bottomLeft.getX(),
bottomLeft.getY())
}
private static sampleGrid(image: BitMatrix,
transform: PerspectiveTransform,
dimension: number/*int*/): BitMatrix /*throws NotFoundException*/ {
const sampler = GridSamplerInstance.getInstance()
return sampler.sampleGridWithTransform(image, dimension, dimension, transform)
}
/**
* <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
* of the finder patterns and estimated module size.</p>
*/
private static computeDimension(topLeft: ResultPoint,
topRight: ResultPoint,
bottomLeft: ResultPoint,
moduleSize: number/*float*/): number/*int*/ /*throws NotFoundException*/ {
const tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize)
const tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize)
let dimension = Math.floor((tltrCentersDimension + tlblCentersDimension) / 2) + 7
switch (dimension & 0x03) { // mod 4
case 0:
dimension++
break
// 1? do nothing
case 2:
dimension--
break
case 3:
throw new Exception(Exception.NotFoundException)
}
return dimension
}
/**
* <p>Computes an average estimated module size based on estimated derived from the positions
* of the three finder patterns.</p>
*
* @param topLeft detected top-left finder pattern center
* @param topRight detected top-right finder pattern center
* @param bottomLeft detected bottom-left finder pattern center
* @return estimated module size
*/
protected calculateModuleSize(topLeft: ResultPoint,
topRight: ResultPoint,
bottomLeft: ResultPoint): number/*float*/ {
// Take the average
return (this.calculateModuleSizeOneWay(topLeft, topRight) +
this.calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2.0
}
/**
* <p>Estimates module size based on two finder patterns -- it uses
* {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the
* width of each, measuring along the axis between their centers.</p>
*/
private calculateModuleSizeOneWay(pattern: ResultPoint, otherPattern: ResultPoint): number/*float*/ {
const moduleSizeEst1: number/*float*/ = this.sizeOfBlackWhiteBlackRunBothWays(/*(int) */Math.floor(pattern.getX()),
/*(int) */Math.floor(pattern.getY()),
/*(int) */Math.floor(otherPattern.getX()),
/*(int) */Math.floor(otherPattern.getY()))
const moduleSizeEst2: number/*float*/ = this.sizeOfBlackWhiteBlackRunBothWays(/*(int) */Math.floor(otherPattern.getX()),
/*(int) */Math.floor(otherPattern.getY()),
/*(int) */Math.floor(pattern.getX()),
/*(int) */Math.floor(pattern.getY()))
if (isNaN(moduleSizeEst1)) {
return moduleSizeEst2 / 7.0
}
if (isNaN(moduleSizeEst2)) {
return moduleSizeEst1 / 7.0
}
// Average them, and divide by 7 since we've counted the width of 3 black modules,
// and 1 white and 1 black module on either side. Ergo, divide sum by 14.
return (moduleSizeEst1 + moduleSizeEst2) / 14.0
}
/**
* See {@link #sizeOfBlackWhiteBlackRun(int, int, int, int)}; computes the total width of
* a finder pattern by looking for a black-white-black run from the center in the direction
* of another point (another finder pattern center), and in the opposite direction too.
*/
private sizeOfBlackWhiteBlackRunBothWays(fromX: number/*int*/, fromY: number/*int*/, toX: number/*int*/, toY: number/*int*/): number/*float*/ {
let result: number/*float*/ = this.sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY)
// Now count other way -- don't run off image though of course
let scale: number/*float*/ = 1.0
let otherToX = fromX - (toX - fromX)
if (otherToX < 0) {
scale = fromX / /*(float) */(fromX - otherToX)
otherToX = 0
} else if (otherToX >= this.image.getWidth()) {
scale = (this.image.getWidth() - 1 - fromX) / /*(float) */(otherToX - fromX)
otherToX = this.image.getWidth() - 1
}
let otherToY = /*(int) */Math.floor(fromY - (toY - fromY) * scale);
scale = 1.0
if (otherToY < 0) {
scale = fromY / /*(float) */(fromY - otherToY)
otherToY = 0
} else if (otherToY >= this.image.getHeight()) {
scale = (this.image.getHeight() - 1 - fromY) / /*(float) */(otherToY - fromY)
otherToY = this.image.getHeight() - 1
}
otherToX = /*(int) */Math.floor(fromX + (otherToX - fromX) * scale);
result += this.sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY)
// Middle pixel is double-counted this way; subtract 1
return result - 1.0
}
/**
* <p>This method traces a line from a point in the image, in the direction towards another point.
* It begins in a black region, and keeps going until it finds white, then black, then white again.
* It reports the distance from the start to this point.</p>
*
* <p>This is used when figuring out how wide a finder pattern is, when the finder pattern
* may be skewed or rotated.</p>
*/
private sizeOfBlackWhiteBlackRun(fromX: number/*int*/, fromY: number/*int*/, toX: number/*int*/, toY: number/*int*/): number/*float*/ {
// Mild variant of Bresenham's algorithm
// see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
const steep: boolean = Math.abs(toY - fromY) > Math.abs(toX - fromX)
if (steep) {
let temp = fromX
fromX = fromY
fromY = temp
temp = toX
toX = toY
toY = temp
}
const dx = Math.abs(toX - fromX)
const dy = Math.abs(toY - fromY)
let error = -dx / 2
const xstep = fromX < toX ? 1 : -1
const ystep = fromY < toY ? 1 : -1
// In black pixels, looking for white, first or second time.
let state = 0
// Loop up until x == toX, but not beyond
const xLimit = toX + xstep
for (let x = fromX, y = fromY; x != xLimit; x += xstep) {
const realX = steep ? y : x
const realY = steep ? x : y
// Does current pixel mean we have moved white to black or vice versa?
// Scanning black in state 0,2 and white in state 1, so if we find the wrong
// color, advance to next state or end if we are in state 2 already
if ((state === 1) === this.image.get(realX, realY)) {
if (state === 2) {
return MathUtils.distance(x, y, fromX, fromY)
}
state++
}
error += dy
if (error > 0) {
if (y === toY) {
break
}
y += ystep
error -= dx
}
}
// Found black-white-black; give the benefit of the doubt that the next pixel outside the image
// is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a
// small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
if (state === 2) {
return MathUtils.distance(toX + xstep, toY, fromX, fromY)
}
// else we didn't find even black-white-black; no estimate is really possible
return NaN
}
/**
* <p>Attempts to locate an alignment pattern in a limited region of the image, which is
* guessed to contain it. This method uses {@link AlignmentPattern}.</p>
*
* @param overallEstModuleSize estimated module size so far
* @param estAlignmentX x coordinate of center of area probably containing alignment pattern
* @param estAlignmentY y coordinate of above
* @param allowanceFactor number of pixels in all directions to search from the center
* @return {@link AlignmentPattern} if found, or null otherwise
* @throws NotFoundException if an unexpected error occurs during detection
*/
protected findAlignmentInRegion(overallEstModuleSize: number/*float*/,
estAlignmentX: number/*int*/,
estAlignmentY: number/*int*/,
allowanceFactor: number/*float*/): AlignmentPattern
/*throws NotFoundException*/ {
// Look for an alignment pattern (3 modules in size) around where it
// should be
const allowance = /*(int) */Math.floor(allowanceFactor * overallEstModuleSize);
const alignmentAreaLeftX = Math.max(0, estAlignmentX - allowance)
const alignmentAreaRightX = Math.min(this.image.getWidth() - 1, estAlignmentX + allowance)
if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3) {
throw new Exception(Exception.NotFoundException)
}
const alignmentAreaTopY = Math.max(0, estAlignmentY - allowance)
const alignmentAreaBottomY = Math.min(this.image.getHeight() - 1, estAlignmentY + allowance)
if (alignmentAreaBottomY - alignmentAreaTopY < overallEstModuleSize * 3) {
throw new Exception(Exception.NotFoundException)
}
const alignmentFinder =
new AlignmentPatternFinder(
this.image,
alignmentAreaLeftX,
alignmentAreaTopY,
alignmentAreaRightX - alignmentAreaLeftX,
alignmentAreaBottomY - alignmentAreaTopY,
overallEstModuleSize,
this.resultPointCallback)
return alignmentFinder.find()
}
}

View file

@ -0,0 +1,80 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.detector {*/
import ResultPoint from './../../ResultPoint'
/**
* <p>Encapsulates a finder pattern, which are the three square patterns found in
* the corners of QR Codes. It also encapsulates a count of similar finder patterns,
* as a convenience to the finder's bookkeeping.</p>
*
* @author Sean Owen
*/
export default class FinderPattern extends ResultPoint {
// FinderPattern(posX: number/*float*/, posY: number/*float*/, estimatedModuleSize: number/*float*/) {
// this(posX, posY, estimatedModuleSize, 1)
// }
public constructor(posX: number/*float*/, posY: number/*float*/, private estimatedModuleSize: number/*float*/, private count?: number/*int*/) {
super(posX, posY)
if (undefined === count) {
this.count = 1
}
}
public getEstimatedModuleSize(): number/*float*/ {
return this.estimatedModuleSize
}
public getCount(): number/*int*/ {
return this.count
}
/*
void incrementCount() {
this.count++
}
*/
/**
* <p>Determines if this finder pattern "about equals" a finder pattern at the stated
* position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
*/
public aboutEquals(moduleSize: number/*float*/, i: number/*float*/, j: number/*float*/): boolean {
if (Math.abs(i - this.getY()) <= moduleSize && Math.abs(j - this.getX()) <= moduleSize) {
const moduleSizeDiff: number/*float*/ = Math.abs(moduleSize - this.estimatedModuleSize)
return moduleSizeDiff <= 1.0 || moduleSizeDiff <= this.estimatedModuleSize
}
return false
}
/**
* Combines this object's current estimate of a finder pattern position and module size
* with a new estimate. It returns a new {@code FinderPattern} containing a weighted average
* based on count.
*/
public combineEstimate(i: number/*float*/, j: number/*float*/, newModuleSize: number/*float*/): FinderPattern {
const combinedCount = this.count + 1
const combinedX: number/*float*/ = (this.count * this.getX() + j) / combinedCount;
const combinedY: number/*float*/ = (this.count * this.getY() + i) / combinedCount;
const combinedModuleSize: number/*float*/ = (this.count * this.estimatedModuleSize + newModuleSize) / combinedCount;
return new FinderPattern(combinedX, combinedY, combinedModuleSize, combinedCount)
}
}

View file

@ -0,0 +1,669 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.detector {*/
import DecodeHintType from './../../DecodeHintType'
import ResultPoint from './../../ResultPoint'
import ResultPointCallback from './../../ResultPointCallback'
import BitMatrix from './../../common/BitMatrix'
import FinderPattern from './FinderPattern'
import FinderPatternInfo from './FinderPatternInfo'
import Exception from './../../Exception'
/*import java.io.Serializable;*/
/*import java.util.ArrayList;*/
/*import java.util.Collections;*/
/*import java.util.Comparator;*/
/*import java.util.List;*/
/*import java.util.Map;*/
/**
* <p>This class attempts to find finder patterns in a QR Code. Finder patterns are the square
* markers at three corners of a QR Code.</p>
*
* <p>This class is thread-safe but not reentrant. Each thread must allocate its own object.
*
* @author Sean Owen
*/
export default class FinderPatternFinder {
private static CENTER_QUORUM = 2;
protected static MIN_SKIP = 3; // 1 pixel/module times 3 modules/center
protected static MAX_MODULES = 57; // support up to version 10 for mobile clients
private possibleCenters: FinderPattern[]
private hasSkipped: boolean
private crossCheckStateCount: Int32Array
/**
* <p>Creates a finder that will search the image for three finder patterns.</p>
*
* @param image image to search
*/
// public constructor(image: BitMatrix) {
// this(image, null)
// }
public constructor(private image: BitMatrix, private resultPointCallback: ResultPointCallback) {
this.possibleCenters = []
this.crossCheckStateCount = new Int32Array(5)
this.resultPointCallback = resultPointCallback
}
protected getImage(): BitMatrix {
return this.image
}
protected getPossibleCenters(): FinderPattern[] {
return this.possibleCenters
}
public find(hints: Map<DecodeHintType, any>): FinderPatternInfo /*throws NotFoundException */ {
const tryHarder: boolean = (hints !== null && hints !== undefined) && undefined !== hints.get(DecodeHintType.TRY_HARDER)
const pureBarcode: boolean = (hints !== null && hints !== undefined) && undefined !== hints.get(DecodeHintType.PURE_BARCODE)
const image = this.image
const maxI = image.getHeight()
const maxJ = image.getWidth()
// We are looking for black/white/black/white/black modules in
// 1:1:3:1:1 ratio; this tracks the number of such modules seen so far
// Let's assume that the maximum version QR Code we support takes up 1/4 the height of the
// image, and then account for the center being 3 modules in size. This gives the smallest
// number of pixels the center could be, so skip this often. When trying harder, look for all
// QR versions regardless of how dense they are.
let iSkip = Math.floor((3 * maxI) / (4 * FinderPatternFinder.MAX_MODULES))
if (iSkip < FinderPatternFinder.MIN_SKIP || tryHarder) {
iSkip = FinderPatternFinder.MIN_SKIP
}
let done: boolean = false
const stateCount = new Int32Array(5)
for (let i = iSkip - 1; i < maxI && !done; i += iSkip) {
// Get a row of black/white values
stateCount[0] = 0
stateCount[1] = 0
stateCount[2] = 0
stateCount[3] = 0
stateCount[4] = 0
let currentState = 0
for (let j = 0; j < maxJ; j++) {
if (image.get(j, i)) {
// Black pixel
if ((currentState & 1) === 1) { // Counting white pixels
currentState++
}
stateCount[currentState]++
} else { // White pixel
if ((currentState & 1) === 0) { // Counting black pixels
if (currentState === 4) { // A winner?
if (FinderPatternFinder.foundPatternCross(stateCount)) { // Yes
const confirmed: boolean = this.handlePossibleCenter(stateCount, i, j, pureBarcode)
if (confirmed === true) {
// Start examining every other line. Checking each line turned out to be too
// expensive and didn't improve performance.
iSkip = 2
if (this.hasSkipped === true) {
done = this.haveMultiplyConfirmedCenters()
} else {
const rowSkip = this.findRowSkip()
if (rowSkip > stateCount[2]) {
// Skip rows between row of lower confirmed center
// and top of presumed third confirmed center
// but back up a bit to get a full chance of detecting
// it, entire width of center of finder pattern
// Skip by rowSkip, but back off by stateCount[2] (size of last center
// of pattern we saw) to be conservative, and also back off by iSkip which
// is about to be re-added
i += rowSkip - stateCount[2] - iSkip
j = maxJ - 1
}
}
} else {
stateCount[0] = stateCount[2]
stateCount[1] = stateCount[3]
stateCount[2] = stateCount[4]
stateCount[3] = 1
stateCount[4] = 0
currentState = 3
continue
}
// Clear state to start looking again
currentState = 0
stateCount[0] = 0
stateCount[1] = 0
stateCount[2] = 0
stateCount[3] = 0
stateCount[4] = 0
} else { // No, shift counts back by two
stateCount[0] = stateCount[2]
stateCount[1] = stateCount[3]
stateCount[2] = stateCount[4]
stateCount[3] = 1
stateCount[4] = 0
currentState = 3
}
} else {
stateCount[++currentState]++
}
} else { // Counting white pixels
stateCount[currentState]++
}
}
}
if (FinderPatternFinder.foundPatternCross(stateCount)) {
const confirmed: boolean = this.handlePossibleCenter(stateCount, i, maxJ, pureBarcode)
if (confirmed === true) {
iSkip = stateCount[0]
if (this.hasSkipped) {
// Found a third one
done = this.haveMultiplyConfirmedCenters()
}
}
}
}
const patternInfo: FinderPattern[] = this.selectBestPatterns()
ResultPoint.orderBestPatterns(patternInfo)
return new FinderPatternInfo(patternInfo)
}
/**
* Given a count of black/white/black/white/black pixels just seen and an end position,
* figures the location of the center of this run.
*/
private static centerFromEnd(stateCount: Int32Array, end: number/*int*/): number/*float*/ {
return (end - stateCount[4] - stateCount[3]) - stateCount[2] / 2.0
}
/**
* @param stateCount count of black/white/black/white/black pixels just read
* @return true iff the proportions of the counts is close enough to the 1/1/3/1/1 ratios
* used by finder patterns to be considered a match
*/
protected static foundPatternCross(stateCount: Int32Array): boolean {
let totalModuleSize = 0
for (let i = 0; i < 5; i++) {
const count = stateCount[i]
if (count === 0) {
return false
}
totalModuleSize += count
}
if (totalModuleSize < 7) {
return false
}
const moduleSize: number/*float*/ = totalModuleSize / 7.0
const maxVariance: number/*float*/ = moduleSize / 2.0
// Allow less than 50% variance from 1-1-3-1-1 proportions
return Math.abs(moduleSize - stateCount[0]) < maxVariance &&
Math.abs(moduleSize - stateCount[1]) < maxVariance &&
Math.abs(3.0 * moduleSize - stateCount[2]) < 3 * maxVariance &&
Math.abs(moduleSize - stateCount[3]) < maxVariance &&
Math.abs(moduleSize - stateCount[4]) < maxVariance
}
private getCrossCheckStateCount(): Int32Array {
const crossCheckStateCount = this.crossCheckStateCount
crossCheckStateCount[0] = 0
crossCheckStateCount[1] = 0
crossCheckStateCount[2] = 0
crossCheckStateCount[3] = 0
crossCheckStateCount[4] = 0
return crossCheckStateCount
}
/**
* After a vertical and horizontal scan finds a potential finder pattern, this method
* "cross-cross-cross-checks" by scanning down diagonally through the center of the possible
* finder pattern to see if the same proportion is detected.
*
* @param startI row where a finder pattern was detected
* @param centerJ center of the section that appears to cross a finder pattern
* @param maxCount maximum reasonable number of modules that should be
* observed in any reading state, based on the results of the horizontal scan
* @param originalStateCountTotal The original state count total.
* @return true if proportions are withing expected limits
*/
private crossCheckDiagonal(startI: number/*int*/, centerJ: number/*int*/, maxCount: number/*int*/, originalStateCountTotal: number/*int*/): boolean {
const stateCount: Int32Array = this.getCrossCheckStateCount()
// Start counting up, left from center finding black center mass
let i = 0
const image = this.image
while (startI >= i && centerJ >= i && image.get(centerJ - i, startI - i)) {
stateCount[2]++
i++
}
if (startI < i || centerJ < i) {
return false
}
// Continue up, left finding white space
while (startI >= i && centerJ >= i && !image.get(centerJ - i, startI - i) &&
stateCount[1] <= maxCount) {
stateCount[1]++
i++
}
// If already too many modules in this state or ran off the edge:
if (startI < i || centerJ < i || stateCount[1] > maxCount) {
return false
}
// Continue up, left finding black border
while (startI >= i && centerJ >= i && image.get(centerJ - i, startI - i) &&
stateCount[0] <= maxCount) {
stateCount[0]++
i++
}
if (stateCount[0] > maxCount) {
return false
}
const maxI = image.getHeight()
const maxJ = image.getWidth()
// Now also count down, right from center
i = 1
while (startI + i < maxI && centerJ + i < maxJ && image.get(centerJ + i, startI + i)) {
stateCount[2]++
i++
}
// Ran off the edge?
if (startI + i >= maxI || centerJ + i >= maxJ) {
return false
}
while (startI + i < maxI && centerJ + i < maxJ && !image.get(centerJ + i, startI + i) &&
stateCount[3] < maxCount) {
stateCount[3]++
i++
}
if (startI + i >= maxI || centerJ + i >= maxJ || stateCount[3] >= maxCount) {
return false
}
while (startI + i < maxI && centerJ + i < maxJ && image.get(centerJ + i, startI + i) &&
stateCount[4] < maxCount) {
stateCount[4]++
i++
}
if (stateCount[4] >= maxCount) {
return false
}
// If we found a finder-pattern-like section, but its size is more than 100% different than
// the original, assume it's a false positive
const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]
return Math.abs(stateCountTotal - originalStateCountTotal) < 2 * originalStateCountTotal &&
FinderPatternFinder.foundPatternCross(stateCount)
}
/**
* <p>After a horizontal scan finds a potential finder pattern, this method
* "cross-checks" by scanning down vertically through the center of the possible
* finder pattern to see if the same proportion is detected.</p>
*
* @param startI row where a finder pattern was detected
* @param centerJ center of the section that appears to cross a finder pattern
* @param maxCount maximum reasonable number of modules that should be
* observed in any reading state, based on the results of the horizontal scan
* @return vertical center of finder pattern, or {@link Float#NaN} if not found
*/
private crossCheckVertical(startI: number/*int*/, centerJ: number/*int*/, maxCount: number/*int*/,
originalStateCountTotal: number/*int*/): number/*float*/ {
const image: BitMatrix = this.image
const maxI = image.getHeight()
const stateCount: Int32Array = this.getCrossCheckStateCount()
// Start counting up from center
let i = startI
while (i >= 0 && image.get(centerJ, i)) {
stateCount[2]++
i--
}
if (i < 0) {
return NaN
}
while (i >= 0 && !image.get(centerJ, i) && stateCount[1] <= maxCount) {
stateCount[1]++
i--
}
// If already too many modules in this state or ran off the edge:
if (i < 0 || stateCount[1] > maxCount) {
return NaN
}
while (i >= 0 && image.get(centerJ, i) && stateCount[0] <= maxCount) {
stateCount[0]++
i--
}
if (stateCount[0] > maxCount) {
return NaN
}
// Now also count down from center
i = startI + 1
while (i < maxI && image.get(centerJ, i)) {
stateCount[2]++
i++
}
if (i === maxI) {
return NaN
}
while (i < maxI && !image.get(centerJ, i) && stateCount[3] < maxCount) {
stateCount[3]++
i++
}
if (i === maxI || stateCount[3] >= maxCount) {
return NaN
}
while (i < maxI && image.get(centerJ, i) && stateCount[4] < maxCount) {
stateCount[4]++
i++
}
if (stateCount[4] >= maxCount) {
return NaN
}
// If we found a finder-pattern-like section, but its size is more than 40% different than
// the original, assume it's a false positive
const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +
stateCount[4]
if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) {
return NaN
}
return FinderPatternFinder.foundPatternCross(stateCount) ? FinderPatternFinder.centerFromEnd(stateCount, i) : NaN
}
/**
* <p>Like {@link #crossCheckVertical(int, int, int, int)}, and in fact is basically identical,
* except it reads horizontally instead of vertically. This is used to cross-cross
* check a vertical cross check and locate the real center of the alignment pattern.</p>
*/
private crossCheckHorizontal(startJ: number/*int*/, centerI: number/*int*/, maxCount: number/*int*/,
originalStateCountTotal: number/*int*/): number/*float*/ {
const image: BitMatrix = this.image
const maxJ = image.getWidth()
const stateCount: Int32Array = this.getCrossCheckStateCount()
let j = startJ
while (j >= 0 && image.get(j, centerI)) {
stateCount[2]++
j--
}
if (j < 0) {
return NaN
}
while (j >= 0 && !image.get(j, centerI) && stateCount[1] <= maxCount) {
stateCount[1]++
j--
}
if (j < 0 || stateCount[1] > maxCount) {
return NaN
}
while (j >= 0 && image.get(j, centerI) && stateCount[0] <= maxCount) {
stateCount[0]++
j--
}
if (stateCount[0] > maxCount) {
return NaN
}
j = startJ + 1
while (j < maxJ && image.get(j, centerI)) {
stateCount[2]++
j++
}
if (j == maxJ) {
return NaN
}
while (j < maxJ && !image.get(j, centerI) && stateCount[3] < maxCount) {
stateCount[3]++
j++
}
if (j == maxJ || stateCount[3] >= maxCount) {
return NaN
}
while (j < maxJ && image.get(j, centerI) && stateCount[4] < maxCount) {
stateCount[4]++
j++
}
if (stateCount[4] >= maxCount) {
return NaN
}
// If we found a finder-pattern-like section, but its size is significantly different than
// the original, assume it's a false positive
const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +
stateCount[4]
if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal) {
return NaN
}
return FinderPatternFinder.foundPatternCross(stateCount) ? FinderPatternFinder.centerFromEnd(stateCount, j) : NaN
}
/**
* <p>This is called when a horizontal scan finds a possible alignment pattern. It will
* cross check with a vertical scan, and if successful, will, ah, cross-cross-check
* with another horizontal scan. This is needed primarily to locate the real horizontal
* center of the pattern in cases of extreme skew.
* And then we cross-cross-cross check with another diagonal scan.</p>
*
* <p>If that succeeds the finder pattern location is added to a list that tracks
* the number of times each location has been nearly-matched as a finder pattern.
* Each additional find is more evidence that the location is in fact a finder
* pattern center
*
* @param stateCount reading state module counts from horizontal scan
* @param i row where finder pattern may be found
* @param j end of possible finder pattern in row
* @param pureBarcode true if in "pure barcode" mode
* @return true if a finder pattern candidate was found this time
*/
protected handlePossibleCenter(stateCount: Int32Array, i: number/*int*/, j: number/*int*/, pureBarcode: boolean): boolean {
const stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +
stateCount[4]
let centerJ: number/*float*/ = FinderPatternFinder.centerFromEnd(stateCount, j)
let centerI: number/*float*/ = this.crossCheckVertical(i, /*(int) */Math.floor(centerJ), stateCount[2], stateCountTotal);
if (!isNaN(centerI)) {
// Re-cross check
centerJ = this.crossCheckHorizontal(/*(int) */Math.floor(centerJ), /*(int) */Math.floor(centerI), stateCount[2], stateCountTotal)
if (!isNaN(centerJ) &&
(!pureBarcode || this.crossCheckDiagonal(/*(int) */Math.floor(centerI), /*(int) */Math.floor(centerJ), stateCount[2], stateCountTotal))) {
const estimatedModuleSize: number/*float*/ = stateCountTotal / 7.0
let found: boolean = false
const possibleCenters = this.possibleCenters
for (let index = 0, length = possibleCenters.length; index < length; index++) {
const center: FinderPattern = possibleCenters[index]
// Look for about the same center and module size:
if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) {
possibleCenters[index] = center.combineEstimate(centerI, centerJ, estimatedModuleSize)
found = true
break
}
}
if (!found) {
const point: FinderPattern = new FinderPattern(centerJ, centerI, estimatedModuleSize)
possibleCenters.push(point)
if (this.resultPointCallback !== null && this.resultPointCallback !== undefined) {
this.resultPointCallback.foundPossibleResultPoint(point)
}
}
return true
}
}
return false
}
/**
* @return number of rows we could safely skip during scanning, based on the first
* two finder patterns that have been located. In some cases their position will
* allow us to infer that the third pattern must lie below a certain point farther
* down in the image.
*/
private findRowSkip(): number/*int*/ {
const max = this.possibleCenters.length
if (max <= 1) {
return 0
}
let firstConfirmedCenter: ResultPoint = null
for (const center of this.possibleCenters) {
if (center.getCount() >= FinderPatternFinder.CENTER_QUORUM) {
if (firstConfirmedCenter == null) {
firstConfirmedCenter = center
} else {
// We have two confirmed centers
// How far down can we skip before resuming looking for the next
// pattern? In the worst case, only the difference between the
// difference in the x / y coordinates of the two centers.
// This is the case where you find top left last.
this.hasSkipped = true
return /*(int) */Math.floor((Math.abs(firstConfirmedCenter.getX() - center.getX()) -
Math.abs(firstConfirmedCenter.getY() - center.getY())) / 2)
}
}
}
return 0
}
/**
* @return true iff we have found at least 3 finder patterns that have been detected
* at least {@link #CENTER_QUORUM} times each, and, the estimated module size of the
* candidates is "pretty similar"
*/
private haveMultiplyConfirmedCenters(): boolean {
let confirmedCount = 0
let totalModuleSize: number/*float*/ = 0.0
const max = this.possibleCenters.length
for (const pattern of this.possibleCenters) {
if (pattern.getCount() >= FinderPatternFinder.CENTER_QUORUM) {
confirmedCount++
totalModuleSize += pattern.getEstimatedModuleSize()
}
}
if (confirmedCount < 3) {
return false
}
// OK, we have at least 3 confirmed centers, but, it's possible that one is a "false positive"
// and that we need to keep looking. We detect this by asking if the estimated module sizes
// vary too much. We arbitrarily say that when the total deviation from average exceeds
// 5% of the total module size estimates, it's too much.
const average: number/*float*/ = totalModuleSize / max
let totalDeviation: number/*float*/ = 0.0
for (const pattern of this.possibleCenters) {
totalDeviation += Math.abs(pattern.getEstimatedModuleSize() - average)
}
return totalDeviation <= 0.05 * totalModuleSize;
}
/**
* @return the 3 best {@link FinderPattern}s from our list of candidates. The "best" are
* those that have been detected at least {@link #CENTER_QUORUM} times, and whose module
* size differs from the average among those patterns the least
* @throws NotFoundException if 3 such finder patterns do not exist
*/
private selectBestPatterns(): FinderPattern[] /*throws NotFoundException */ {
const startSize = this.possibleCenters.length
if (startSize < 3) {
// Couldn't find enough finder patterns
throw new Exception(Exception.NotFoundException)
}
const possibleCenters = this.possibleCenters
let average: number/*float*/
// Filter outlier possibilities whose module size is too different
if (startSize > 3) {
// But we can only afford to do so if we have at least 4 possibilities to choose from
let totalModuleSize: number/*float*/ = 0.0
let square: number/*float*/ = 0.0
for (const center of this.possibleCenters) {
const size: number/*float*/ = center.getEstimatedModuleSize()
totalModuleSize += size
square += size * size
}
average = totalModuleSize / startSize
let stdDev: number/*float*/ = /*(float) */Math.sqrt(square / startSize - average * average)
possibleCenters.sort(
/**
* <p>Orders by furthest from average</p>
*/
// FurthestFromAverageComparator implements Comparator<FinderPattern>
(center1: FinderPattern, center2: FinderPattern) => {
const dA: number/*float*/ = Math.abs(center2.getEstimatedModuleSize() - average)
const dB: number/*float*/ = Math.abs(center1.getEstimatedModuleSize() - average)
return dA < dB ? -1 : dA > dB ? 1 : 0
})
const limit: number/*float*/ = Math.max(0.2 * average, stdDev);
for (let i = 0; i < possibleCenters.length && possibleCenters.length > 3; i++) {
const pattern: FinderPattern = possibleCenters[i]
if (Math.abs(pattern.getEstimatedModuleSize() - average) > limit) {
possibleCenters.splice(i, 1)
i--
}
}
}
if (possibleCenters.length > 3) {
// Throw away all but those first size candidate points we found.
let totalModuleSize: number/*float*/ = 0.0
for (const possibleCenter of possibleCenters) {
totalModuleSize += possibleCenter.getEstimatedModuleSize()
}
average = totalModuleSize / possibleCenters.length
possibleCenters.sort(
/**
* <p>Orders by {@link FinderPattern#getCount()}, descending.</p>
*/
// CenterComparator implements Comparator<FinderPattern>
(center1: FinderPattern, center2: FinderPattern) => {
if (center2.getCount() === center1.getCount()) {
const dA: number/*float*/ = Math.abs(center2.getEstimatedModuleSize() - average)
const dB: number/*float*/ = Math.abs(center1.getEstimatedModuleSize() - average)
return dA < dB ? 1 : dA > dB ? -1 : 0
} else {
return center2.getCount() - center1.getCount()
}
})
possibleCenters.splice(3)// this is not realy necessary as we only return first 3 anyway
}
return [
possibleCenters[0],
possibleCenters[1],
possibleCenters[2]
]
}
}

View file

@ -0,0 +1,51 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.detector {*/
import FinderPattern from './FinderPattern'
/**
* <p>Encapsulates information about finder patterns in an image, including the location of
* the three finder patterns, and their estimated module size.</p>
*
* @author Sean Owen
*/
export default class FinderPatternInfo {
private bottomLeft: FinderPattern
private topLeft: FinderPattern
private topRight: FinderPattern
public constructor(patternCenters: FinderPattern[]) {
this.bottomLeft = patternCenters[0]
this.topLeft = patternCenters[1]
this.topRight = patternCenters[2]
}
public getBottomLeft(): FinderPattern {
return this.bottomLeft
}
public getTopLeft(): FinderPattern {
return this.topLeft
}
public getTopRight(): FinderPattern {
return this.topRight
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.encoder {*/
export default class BlockPair {
public constructor(private dataBytes: Uint8Array, private errorCorrectionBytes: Uint8Array) {}
public getDataBytes(): Uint8Array {
return this.dataBytes
}
public getErrorCorrectionBytes(): Uint8Array {
return this.errorCorrectionBytes
}
}

View file

@ -0,0 +1,126 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.encoder {*/
/*import java.util.Arrays;*/
import Arrays from './../../util/Arrays'
import StringBuilder from './../../util/StringBuilder'
/**
* JAVAPORT: The original code was a 2D array of ints, but since it only ever gets assigned
* -1, 0, and 1, I'm going to use less memory and go with bytes.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
export default class ByteMatrix {
private bytes: Array<Uint8Array>
public constructor(private width: number/*int*/, private height: number/*int*/) {
const bytes = new Array<Uint8Array>(height)//[height][width]
for(let i = 0; i != height; i++) {
bytes[i] = new Uint8Array(width)
}
this.bytes = bytes
}
public getHeight(): number/*int*/ {
return this.height
}
public getWidth(): number/*int*/ {
return this.width
}
public get(x: number/*int*/, y: number/*int*/): number/*byte*/ {
return this.bytes[y][x]
}
/**
* @return an internal representation as bytes, in row-major order. array[y][x] represents point (x,y)
*/
public getArray(): Array<Uint8Array> {
return this.bytes
}
// TYPESCRIPTPORT: preffer to let two methods instead of override to avoid type comparison inside
public setNumber(x: number/*int*/, y: number/*int*/, value: number/*byte|int*/): void {
this.bytes[y][x] = value
}
// public set(x: number/*int*/, y: number/*int*/, value: number/*int*/): void {
// bytes[y][x] = (byte) value
// }
public setBoolean(x: number/*int*/, y: number/*int*/, value: boolean): void {
this.bytes[y][x] = /*(byte) */(value ? 1 : 0)
}
public clear(value: number/*byte*/): void {
for (const aByte of this.bytes) {
Arrays.fillUint8Array(aByte, value)
}
}
public equals(o: any) {
if (!(o instanceof ByteMatrix)) {
return false
}
const other = <ByteMatrix> o
if (this.width !== other.width) {
return false
}
if (this.height !== other.height) {
return false
}
for (let y = 0, height = this.height; y < height; ++y) {
const bytesY = this.bytes[y]
const otherBytesY = other.bytes[y]
for (let x = 0, width = this.width; x < width; ++x) {
if (bytesY[x] !== otherBytesY[x]) {
return false
}
}
}
return true
}
/*@Override*/
public toString(): string {
const result = new StringBuilder()//(2 * width * height + 2)
for (let y = 0, height = this.height; y < height; ++y) {
const bytesY = this.bytes[y]
for (let x = 0, width = this.width; x < width; ++x) {
switch (bytesY[x]) {
case 0:
result.append(" 0")
break
case 1:
result.append(" 1")
break
default:
result.append(" ")
break
}
}
result.append('\n')
}
return result.toString()
}
}

View file

@ -0,0 +1,627 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.encoder {*/
import EncodeHintType from './../../EncodeHintType'
import BitArray from './../../common/BitArray'
import CharacterSetECI from './../../common/CharacterSetECI'
import GenericGF from './../../common/reedsolomon/GenericGF'
import ReedSolomonEncoder from './../../common/reedsolomon/ReedSolomonEncoder'
import ErrorCorrectionLevel from './../decoder/ErrorCorrectionLevel'
import Mode from './../decoder/Mode'
import Version from './../decoder/Version'
import MaskUtil from './MaskUtil'
import ByteMatrix from './ByteMatrix'
import QRCode from './QRCode'
import Exception from './../../Exception'
import ECBlocks from './../decoder/ECBlocks'
import MatrixUtil from './MatrixUtil'
import StringEncoding from './../../util/StringEncoding'
import BlockPair from './BlockPair'
/*import java.io.UnsupportedEncodingException;*/
/*import java.util.ArrayList;*/
/*import java.util.Collection;*/
/*import java.util.Map;*/
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author dswitkin@google.com (Daniel Switkin) - ported from C++
*/
export default class Encoder {
// The original table is defined in the table 5 of JISX0510:2004 (p.19).
private static ALPHANUMERIC_TABLE = Int32Array.from([
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x00-0x0f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x10-0x1f
36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, // 0x20-0x2f
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, // 0x30-0x3f
-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x40-0x4f
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, // 0x50-0x5f
])
public static DEFAULT_BYTE_MODE_ENCODING = CharacterSetECI.UTF8.getName()//"ISO-8859-1"
// TYPESCRIPTPORT: changed to UTF8, the default for js
private constructor() {}
// The mask penalty calculation is complicated. See Table 21 of JISX0510:2004 (p.45) for details.
// Basically it applies four rules and summate all penalties.
private static calculateMaskPenalty(matrix: ByteMatrix): number/*int*/ {
return MaskUtil.applyMaskPenaltyRule1(matrix)
+ MaskUtil.applyMaskPenaltyRule2(matrix)
+ MaskUtil.applyMaskPenaltyRule3(matrix)
+ MaskUtil.applyMaskPenaltyRule4(matrix)
}
/**
* @param content text to encode
* @param ecLevel error correction level to use
* @return {@link QRCode} representing the encoded QR code
* @throws WriterException if encoding can't succeed, because of for example invalid content
* or configuration
*/
// public static encode(content: string, ecLevel: ErrorCorrectionLevel): QRCode /*throws WriterException*/ {
// return encode(content, ecLevel, null)
// }
public static encode(content: string,
ecLevel: ErrorCorrectionLevel,
hints: Map<EncodeHintType, any> = null): QRCode /*throws WriterException*/ {
// Determine what character encoding has been specified by the caller, if any
let encoding: string = Encoder.DEFAULT_BYTE_MODE_ENCODING
const hasEncodingHint: boolean = hints !== null && undefined !== hints.get(EncodeHintType.CHARACTER_SET)
if (hasEncodingHint) {
encoding = hints.get(EncodeHintType.CHARACTER_SET).toString()
}
// Pick an encoding mode appropriate for the content. Note that this will not attempt to use
// multiple modes / segments even if that were more efficient. Twould be nice.
const mode: Mode = this.chooseMode(content, encoding)
// This will store the header information, like mode and
// length, as well as "header" segments like an ECI segment.
const headerBits = new BitArray()
// Append ECI segment if applicable
if (mode == Mode.BYTE && (hasEncodingHint || Encoder.DEFAULT_BYTE_MODE_ENCODING !== encoding)) {
const eci = CharacterSetECI.getCharacterSetECIByName(encoding)
if (eci !== undefined) {
this.appendECI(eci, headerBits)
}
}
// (With ECI in place,) Write the mode marker
this.appendModeInfo(mode, headerBits)
// Collect data within the main segment, separately, to count its size if needed. Don't add it to
// main payload yet.
const dataBits = new BitArray()
this.appendBytes(content, mode, dataBits, encoding)
let version: Version
if (hints !== null && undefined !== hints.get(EncodeHintType.QR_VERSION)) {
const versionNumber = Number.parseInt(hints.get(EncodeHintType.QR_VERSION).toString(), 10)
version = Version.getVersionForNumber(versionNumber)
const bitsNeeded = this.calculateBitsNeeded(mode, headerBits, dataBits, version)
if (!this.willFit(bitsNeeded, version, ecLevel)) {
throw new Exception(Exception.WriterException, "Data too big for requested version")
}
} else {
version = this.recommendVersion(ecLevel, mode, headerBits, dataBits)
}
const headerAndDataBits = new BitArray()
headerAndDataBits.appendBitArray(headerBits)
// Find "length" of main segment and write it
const numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length
this.appendLengthInfo(numLetters, version, mode, headerAndDataBits)
// Put data together into the overall payload
headerAndDataBits.appendBitArray(dataBits)
const ecBlocks: ECBlocks = version.getECBlocksForLevel(ecLevel)
const numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords()
// Terminate the bits properly.
this.terminateBits(numDataBytes, headerAndDataBits)
// Interleave data bits with error correction code.
const finalBits: BitArray = this.interleaveWithECBytes(headerAndDataBits,
version.getTotalCodewords(),
numDataBytes,
ecBlocks.getNumBlocks())
const qrCode = new QRCode()
qrCode.setECLevel(ecLevel)
qrCode.setMode(mode)
qrCode.setVersion(version)
// Choose the mask pattern and set to "qrCode".
const dimension = version.getDimensionForVersion()
const matrix: ByteMatrix = new ByteMatrix(dimension, dimension)
const maskPattern = this.chooseMaskPattern(finalBits, ecLevel, version, matrix)
qrCode.setMaskPattern(maskPattern)
// Build the matrix and set it to "qrCode".
MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix)
qrCode.setMatrix(matrix)
return qrCode
}
/**
* Decides the smallest version of QR code that will contain all of the provided data.
*
* @throws WriterException if the data cannot fit in any version
*/
private static recommendVersion(ecLevel: ErrorCorrectionLevel,
mode: Mode,
headerBits: BitArray,
dataBits: BitArray): Version /*throws WriterException*/ {
// Hard part: need to know version to know how many bits length takes. But need to know how many
// bits it takes to know version. First we take a guess at version by assuming version will be
// the minimum, 1:
const provisionalBitsNeeded = this.calculateBitsNeeded(mode, headerBits, dataBits, Version.getVersionForNumber(1))
const provisionalVersion = this.chooseVersion(provisionalBitsNeeded, ecLevel)
// Use that guess to calculate the right version. I am still not sure this works in 100% of cases.
const bitsNeeded = this.calculateBitsNeeded(mode, headerBits, dataBits, provisionalVersion)
return this.chooseVersion(bitsNeeded, ecLevel)
}
private static calculateBitsNeeded(mode: Mode,
headerBits: BitArray,
dataBits: BitArray,
version: Version): number/*int*/ {
return headerBits.getSize() + mode.getCharacterCountBits(version) + dataBits.getSize()
}
/**
* @return the code point of the table used in alphanumeric mode or
* -1 if there is no corresponding code in the table.
*/
public static getAlphanumericCode(code: number/*int*/): number/*int*/ {
if (code < Encoder.ALPHANUMERIC_TABLE.length) {
return Encoder.ALPHANUMERIC_TABLE[code]
}
return -1
}
// public static chooseMode(content: string): Mode {
// return chooseMode(content, null);
// }
/**
* Choose the best mode by examining the content. Note that 'encoding' is used as a hint;
* if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}.
*/
public static chooseMode(content: string, encoding: string = null): Mode {
if (CharacterSetECI.SJIS.getName() === encoding && this.isOnlyDoubleByteKanji(content)) {
// Choose Kanji mode if all input are double-byte characters
return Mode.KANJI
}
let hasNumeric: boolean = false
let hasAlphanumeric: boolean = false
for (let i = 0, length = content.length; i < length; ++i) {
const c: string = content.charAt(i)
if (Encoder.isDigit(c)) {
hasNumeric = true
} else if (this.getAlphanumericCode(c.charCodeAt(0)) != -1) {
hasAlphanumeric = true
} else {
return Mode.BYTE
}
}
if (hasAlphanumeric) {
return Mode.ALPHANUMERIC
}
if (hasNumeric) {
return Mode.NUMERIC
}
return Mode.BYTE
}
private static isOnlyDoubleByteKanji(content: string): boolean {
let bytes: Uint8Array
try {
bytes = StringEncoding.encode(content, CharacterSetECI.SJIS.getName())//content.getBytes("Shift_JIS")
} catch (ignored/*: UnsupportedEncodingException*/) {
return false
}
const length = bytes.length
if (length % 2 != 0) {
return false
}
for (let i = 0; i < length; i += 2) {
const byte1 = bytes[i] & 0xFF
if ((byte1 < 0x81 || byte1 > 0x9F) && (byte1 < 0xE0 || byte1 > 0xEB)) {
return false
}
}
return true
}
private static chooseMaskPattern(bits: BitArray,
ecLevel: ErrorCorrectionLevel,
version: Version,
matrix: ByteMatrix): number/*int*/ /*throws WriterException*/ {
let minPenalty = Number.MAX_SAFE_INTEGER; // Lower penalty is better.
let bestMaskPattern = -1
// We try all mask patterns to choose the best one.
for (let maskPattern = 0; maskPattern < QRCode.NUM_MASK_PATTERNS; maskPattern++) {
MatrixUtil.buildMatrix(bits, ecLevel, version, maskPattern, matrix)
let penalty = this.calculateMaskPenalty(matrix)
if (penalty < minPenalty) {
minPenalty = penalty
bestMaskPattern = maskPattern
}
}
return bestMaskPattern
}
private static chooseVersion(numInputBits: number/*int*/, ecLevel: ErrorCorrectionLevel): Version /*throws WriterException*/ {
for (let versionNum = 1; versionNum <= 40; versionNum++) {
const version = Version.getVersionForNumber(versionNum)
if (Encoder.willFit(numInputBits, version, ecLevel)) {
return version
}
}
throw new Exception(Exception.WriterException, "Data too big")
}
/**
* @return true if the number of input bits will fit in a code with the specified version and
* error correction level.
*/
private static willFit(numInputBits: number/*int*/, version: Version, ecLevel: ErrorCorrectionLevel): boolean {
// In the following comments, we use numbers of Version 7-H.
// numBytes = 196
const numBytes = version.getTotalCodewords()
// getNumECBytes = 130
const ecBlocks = version.getECBlocksForLevel(ecLevel)
const numEcBytes = ecBlocks.getTotalECCodewords()
// getNumDataBytes = 196 - 130 = 66
const numDataBytes = numBytes - numEcBytes
const totalInputBytes = (numInputBits + 7) / 8
return numDataBytes >= totalInputBytes
}
/**
* Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).
*/
public static terminateBits(numDataBytes: number/*int*/, bits: BitArray): void /*throws WriterException*/ {
const capacity = numDataBytes * 8;
if (bits.getSize() > capacity) {
throw new Exception(Exception.WriterException, "data bits cannot fit in the QR Code" + bits.getSize() + " > " +
capacity)
}
for (let i = 0; i < 4 && bits.getSize() < capacity; ++i) {
bits.appendBit(false)
}
// Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details.
// If the last byte isn't 8-bit aligned, we'll add padding bits.
const numBitsInLastByte = bits.getSize() & 0x07;
if (numBitsInLastByte > 0) {
for (let i = numBitsInLastByte; i < 8; i++) {
bits.appendBit(false)
}
}
// If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24).
const numPaddingBytes = numDataBytes - bits.getSizeInBytes()
for (let i = 0; i < numPaddingBytes; ++i) {
bits.appendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8)
}
if (bits.getSize() != capacity) {
throw new Exception(Exception.WriterException, "Bits size does not equal capacity")
}
}
/**
* Get number of data bytes and number of error correction bytes for block id "blockID". Store
* the result in "numDataBytesInBlock", and "numECBytesInBlock". See table 12 in 8.5.1 of
* JISX0510:2004 (p.30)
*/
public static getNumDataBytesAndNumECBytesForBlockID(numTotalBytes: number/*int*/,
numDataBytes: number/*int*/,
numRSBlocks: number/*int*/,
blockID: number/*int*/,
numDataBytesInBlock: Int32Array,
numECBytesInBlock: Int32Array): void /*throws WriterException*/ {
if (blockID >= numRSBlocks) {
throw new Exception(Exception.WriterException, "Block ID too large")
}
// numRsBlocksInGroup2 = 196 % 5 = 1
const numRsBlocksInGroup2 = numTotalBytes % numRSBlocks
// numRsBlocksInGroup1 = 5 - 1 = 4
const numRsBlocksInGroup1 = numRSBlocks - numRsBlocksInGroup2
// numTotalBytesInGroup1 = 196 / 5 = 39
const numTotalBytesInGroup1 = Math.floor(numTotalBytes / numRSBlocks)
// numTotalBytesInGroup2 = 39 + 1 = 40
const numTotalBytesInGroup2 = numTotalBytesInGroup1 + 1
// numDataBytesInGroup1 = 66 / 5 = 13
const numDataBytesInGroup1 = Math.floor(numDataBytes / numRSBlocks)
// numDataBytesInGroup2 = 13 + 1 = 14
const numDataBytesInGroup2 = numDataBytesInGroup1 + 1
// numEcBytesInGroup1 = 39 - 13 = 26
const numEcBytesInGroup1 = numTotalBytesInGroup1 - numDataBytesInGroup1
// numEcBytesInGroup2 = 40 - 14 = 26
const numEcBytesInGroup2 = numTotalBytesInGroup2 - numDataBytesInGroup2
// Sanity checks.
// 26 = 26
if (numEcBytesInGroup1 !== numEcBytesInGroup2) {
throw new Exception(Exception.WriterException, "EC bytes mismatch")
}
// 5 = 4 + 1.
if (numRSBlocks !== numRsBlocksInGroup1 + numRsBlocksInGroup2) {
throw new Exception(Exception.WriterException, "RS blocks mismatch")
}
// 196 = (13 + 26) * 4 + (14 + 26) * 1
if (numTotalBytes !==
((numDataBytesInGroup1 + numEcBytesInGroup1) *
numRsBlocksInGroup1) +
((numDataBytesInGroup2 + numEcBytesInGroup2) *
numRsBlocksInGroup2)) {
throw new Exception(Exception.WriterException, "Total bytes mismatch")
}
if (blockID < numRsBlocksInGroup1) {
numDataBytesInBlock[0] = numDataBytesInGroup1
numECBytesInBlock[0] = numEcBytesInGroup1
} else {
numDataBytesInBlock[0] = numDataBytesInGroup2
numECBytesInBlock[0] = numEcBytesInGroup2
}
}
/**
* Interleave "bits" with corresponding error correction bytes. On success, store the result in
* "result". The interleave rule is complicated. See 8.6 of JISX0510:2004 (p.37) for details.
*/
public static interleaveWithECBytes(bits: BitArray,
numTotalBytes: number/*int*/,
numDataBytes: number/*int*/,
numRSBlocks: number/*int*/): BitArray /*throws WriterException*/ {
// "bits" must have "getNumDataBytes" bytes of data.
if (bits.getSizeInBytes() !== numDataBytes) {
throw new Exception(Exception.WriterException, "Number of bits and data bytes does not match")
}
// Step 1. Divide data bytes into blocks and generate error correction bytes for them. We'll
// store the divided data bytes blocks and error correction bytes blocks into "blocks".
let dataBytesOffset = 0
let maxNumDataBytes = 0
let maxNumEcBytes = 0
// Since, we know the number of reedsolmon blocks, we can initialize the vector with the number.
const blocks = new Array<BlockPair>()//new Array<BlockPair>(numRSBlocks)
for (let i = 0; i < numRSBlocks; ++i) {
const numDataBytesInBlock: Int32Array = new Int32Array(1)
const numEcBytesInBlock: Int32Array = new Int32Array(1)
Encoder.getNumDataBytesAndNumECBytesForBlockID(
numTotalBytes, numDataBytes, numRSBlocks, i,
numDataBytesInBlock, numEcBytesInBlock)
const size = numDataBytesInBlock[0]
const dataBytes = new Uint8Array(size)
bits.toBytes(8 * dataBytesOffset, dataBytes, 0, size);
const ecBytes: Uint8Array = Encoder.generateECBytes(dataBytes, numEcBytesInBlock[0])
blocks.push(new BlockPair(dataBytes, ecBytes))
maxNumDataBytes = Math.max(maxNumDataBytes, size)
maxNumEcBytes = Math.max(maxNumEcBytes, ecBytes.length)
dataBytesOffset += numDataBytesInBlock[0]
}
if (numDataBytes != dataBytesOffset) {
throw new Exception(Exception.WriterException, "Data bytes does not match offset")
}
const result = new BitArray()
// First, place data blocks.
for (let i = 0; i < maxNumDataBytes; ++i) {
for (const block of blocks) {
const dataBytes = block.getDataBytes()
if (i < dataBytes.length) {
result.appendBits(dataBytes[i], 8)
}
}
}
// Then, place error correction blocks.
for (let i = 0; i < maxNumEcBytes; ++i) {
for (const block of blocks) {
const ecBytes = block.getErrorCorrectionBytes()
if (i < ecBytes.length) {
result.appendBits(ecBytes[i], 8)
}
}
}
if (numTotalBytes != result.getSizeInBytes()) { // Should be same.
throw new Exception("WriterException", "Interleaving error: " + numTotalBytes + " and " +
result.getSizeInBytes() + " differ.")
}
return result
}
public static generateECBytes(dataBytes: Uint8Array, numEcBytesInBlock: number/*int*/): Uint8Array {
const numDataBytes = dataBytes.length
const toEncode: Int32Array = new Int32Array(numDataBytes + numEcBytesInBlock)//int[numDataBytes + numEcBytesInBlock]
for (let i = 0; i < numDataBytes; i++) {
toEncode[i] = dataBytes[i] & 0xFF
}
new ReedSolomonEncoder(GenericGF.QR_CODE_FIELD_256).encode(toEncode, numEcBytesInBlock)
const ecBytes = new Uint8Array(numEcBytesInBlock)
for (let i = 0; i < numEcBytesInBlock; i++) {
ecBytes[i] = /*(byte) */toEncode[numDataBytes + i]
}
return ecBytes
}
/**
* Append mode info. On success, store the result in "bits".
*/
public static appendModeInfo(mode: Mode, bits: BitArray): void {
bits.appendBits(mode.getBits(), 4)
}
/**
* Append length info. On success, store the result in "bits".
*/
public static appendLengthInfo(numLetters: number/*int*/, version: Version, mode: Mode, bits: BitArray): void /*throws WriterException*/ {
const numBits = mode.getCharacterCountBits(version)
if (numLetters >= (1 << numBits)) {
throw new Exception(Exception.WriterException, numLetters + " is bigger than " + ((1 << numBits) - 1))
}
bits.appendBits(numLetters, numBits)
}
/**
* Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits".
*/
public static appendBytes(content: string,
mode: Mode,
bits: BitArray,
encoding: string): void /*throws WriterException*/ {
switch (mode) {
case Mode.NUMERIC:
Encoder.appendNumericBytes(content, bits)
break
case Mode.ALPHANUMERIC:
Encoder.appendAlphanumericBytes(content, bits)
break
case Mode.BYTE:
Encoder.append8BitBytes(content, bits, encoding)
break
case Mode.KANJI:
Encoder.appendKanjiBytes(content, bits)
break
default:
throw new Exception(Exception.WriterException, "Invalid mode: " + mode)
}
}
private static getDigit(singleCharacter: string): number {
return singleCharacter.charCodeAt(0) - 48
}
private static isDigit(singleCharacter: string): boolean {
const cn = Encoder.getDigit(singleCharacter)
return cn >= 0 && cn <= 9
}
public static appendNumericBytes(content: string, bits: BitArray): void {
const length = content.length
let i = 0
while (i < length) {
const num1 = Encoder.getDigit(content.charAt(i))
if (i + 2 < length) {
// Encode three numeric letters in ten bits.
const num2 = Encoder.getDigit(content.charAt(i + 1))
const num3 = Encoder.getDigit(content.charAt(i + 2))
bits.appendBits(num1 * 100 + num2 * 10 + num3, 10)
i += 3
} else if (i + 1 < length) {
// Encode two numeric letters in seven bits.
const num2 = Encoder.getDigit(content.charAt(i + 1))
bits.appendBits(num1 * 10 + num2, 7);
i += 2
} else {
// Encode one numeric letter in four bits.
bits.appendBits(num1, 4)
i++
}
}
}
public static appendAlphanumericBytes(content: string, bits: BitArray): void /*throws WriterException*/ {
const length = content.length
let i = 0
while (i < length) {
const code1 = Encoder.getAlphanumericCode(content.charCodeAt(i))
if (code1 == -1) {
throw new Exception(Exception.WriterException)
}
if (i + 1 < length) {
const code2 = Encoder.getAlphanumericCode(content.charCodeAt(i + 1))
if (code2 == -1) {
throw new Exception(Exception.WriterException)
}
// Encode two alphanumeric letters in 11 bits.
bits.appendBits(code1 * 45 + code2, 11);
i += 2
} else {
// Encode one alphanumeric letter in six bits.
bits.appendBits(code1, 6)
i++
}
}
}
public static append8BitBytes(content: string, bits: BitArray, encoding: string): void
/*throws WriterException*/ {
let bytes: Uint8Array
try {
bytes = StringEncoding.encode(content, encoding)
} catch (uee/*: UnsupportedEncodingException*/) {
throw new Exception(Exception.WriterException, uee)
}
for (let i = 0, length = bytes.length; i != length; i++) {
const b = bytes[i]
bits.appendBits(b, 8)
}
}
public static appendKanjiBytes(content: string, bits: BitArray): void /*throws WriterException*/ {
let bytes: Uint8Array
try {
bytes = StringEncoding.encode(content, CharacterSetECI.SJIS.getName())
} catch (uee/*: UnsupportedEncodingException*/) {
throw new Exception(Exception.WriterException, uee)
}
const length = bytes.length
for (let i = 0; i < length; i += 2) {
const byte1 = bytes[i] & 0xFF
const byte2 = bytes[i + 1] & 0xFF
const code = ((byte1 << 8) & 0xFFFFFFFF) | byte2
let subtracted = -1
if (code >= 0x8140 && code <= 0x9ffc) {
subtracted = code - 0x8140
} else if (code >= 0xe040 && code <= 0xebbf) {
subtracted = code - 0xc140
}
if (subtracted === -1) {
throw new Exception(Exception.WriterException, "Invalid byte sequence")
}
const encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
bits.appendBits(encoded, 13)
}
}
private static appendECI(eci: CharacterSetECI, bits: BitArray): void {
bits.appendBits(Mode.ECI.getBits(), 4)
// This is correct for values up to 127, which is all we need now.
bits.appendBits(eci.getValue(), 8)
}
}

View file

@ -0,0 +1,225 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.encoder {*/
import ByteMatrix from './ByteMatrix'
import Exception from './../../Exception'
/**
* @author Satoru Takabayashi
* @author Daniel Switkin
* @author Sean Owen
*/
export default class MaskUtil {
// Penalty weights from section 6.8.2.1
private static N1 = 3;
private static N2 = 3;
private static N3 = 40;
private static N4 = 10;
private constructor() {
// do nothing
}
/**
* Apply mask penalty rule 1 and return the penalty. Find repetitive cells with the same color and
* give penalty to them. Example: 00000 or 11111.
*/
public static applyMaskPenaltyRule1(matrix: ByteMatrix): number/*int*/ {
return MaskUtil.applyMaskPenaltyRule1Internal(matrix, true) + MaskUtil.applyMaskPenaltyRule1Internal(matrix, false)
}
/**
* Apply mask penalty rule 2 and return the penalty. Find 2x2 blocks with the same color and give
* penalty to them. This is actually equivalent to the spec's rule, which is to find MxN blocks and give a
* penalty proportional to (M-1)x(N-1), because this is the number of 2x2 blocks inside such a block.
*/
public static applyMaskPenaltyRule2(matrix: ByteMatrix): number/*int*/ {
let penalty = 0
const array: Array<Uint8Array> = matrix.getArray()
const width: number/*int*/ = matrix.getWidth();
const height: number/*int*/ = matrix.getHeight();
for (let y = 0; y < height - 1; y++) {
const arrayY = array[y]
for (let x = 0; x < width - 1; x++) {
const value = arrayY[x]
if (value === arrayY[x + 1] && value === array[y + 1][x] && value === array[y + 1][x + 1]) {
penalty++
}
}
}
return MaskUtil.N2 * penalty;
}
/**
* Apply mask penalty rule 3 and return the penalty. Find consecutive runs of 1:1:3:1:1:4
* starting with black, or 4:1:1:3:1:1 starting with white, and give penalty to them. If we
* find patterns like 000010111010000, we give penalty once.
*/
public static applyMaskPenaltyRule3(matrix: ByteMatrix): number/*int*/ {
let numPenalties = 0
const array: Array<Uint8Array> = matrix.getArray()
const width: number/*int*/ = matrix.getWidth();
const height: number/*int*/ = matrix.getHeight();
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const arrayY: Uint8Array = array[y]; // We can at least optimize this access
if (x + 6 < width &&
arrayY[x] === 1 &&
arrayY[x + 1] === 0 &&
arrayY[x + 2] === 1 &&
arrayY[x + 3] === 1 &&
arrayY[x + 4] === 1 &&
arrayY[x + 5] === 0 &&
arrayY[x + 6] === 1 &&
(MaskUtil.isWhiteHorizontal(arrayY, x - 4, x) || MaskUtil.isWhiteHorizontal(arrayY, x + 7, x + 11))) {
numPenalties++
}
if (y + 6 < height &&
array[y][x] === 1 &&
array[y + 1][x] === 0 &&
array[y + 2][x] === 1 &&
array[y + 3][x] === 1 &&
array[y + 4][x] === 1 &&
array[y + 5][x] === 0 &&
array[y + 6][x] === 1 &&
(MaskUtil.isWhiteVertical(array, x, y - 4, y) || MaskUtil.isWhiteVertical(array, x, y + 7, y + 11))) {
numPenalties++
}
}
}
return numPenalties * MaskUtil.N3
}
private static isWhiteHorizontal(rowArray: Uint8Array, from: number/*int*/, to: number/*int*/): boolean {
from = Math.max(from, 0)
to = Math.min(to, rowArray.length)
for (let i = from; i < to; i++) {
if (rowArray[i] === 1) {
return false
}
}
return true
}
private static isWhiteVertical(array: Uint8Array[], col: number/*int*/, from: number/*int*/, to: number/*int*/): boolean {
from = Math.max(from, 0)
to = Math.min(to, array.length)
for (let i = from; i < to; i++) {
if (array[i][col] === 1) {
return false
}
}
return true
}
/**
* Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give
* penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance.
*/
public static applyMaskPenaltyRule4(matrix: ByteMatrix): number/*int*/ {
let numDarkCells = 0
const array: Array<Uint8Array> = matrix.getArray()
const width: number/*int*/ = matrix.getWidth();
const height: number/*int*/ = matrix.getHeight();
for (let y = 0; y < height; y++) {
const arrayY: Uint8Array = array[y]
for (let x = 0; x < width; x++) {
if (arrayY[x] === 1) {
numDarkCells++
}
}
}
const numTotalCells = matrix.getHeight() * matrix.getWidth();
const fivePercentVariances = Math.floor(Math.abs(numDarkCells * 2 - numTotalCells) * 10 / numTotalCells)
return fivePercentVariances * MaskUtil.N4;
}
/**
* Return the mask bit for "getMaskPattern" at "x" and "y". See 8.8 of JISX0510:2004 for mask
* pattern conditions.
*/
public static getDataMaskBit(maskPattern: number/*int*/, x: number/*int*/, y: number/*int*/): boolean {
let intermediate: number/*int*/
let temp: number/*int*/
switch (maskPattern) {
case 0:
intermediate = (y + x) & 0x1
break
case 1:
intermediate = y & 0x1
break
case 2:
intermediate = x % 3
break
case 3:
intermediate = (y + x) % 3
break
case 4:
intermediate = (Math.floor(y / 2) + Math.floor(x / 3)) & 0x1
break
case 5:
temp = y * x;
intermediate = (temp & 0x1) + (temp % 3)
break
case 6:
temp = y * x;
intermediate = ((temp & 0x1) + (temp % 3)) & 0x1
break
case 7:
temp = y * x;
intermediate = ((temp % 3) + ((y + x) & 0x1)) & 0x1
break
default:
throw new Exception(Exception.IllegalArgumentException, "Invalid mask pattern: " + maskPattern)
}
return intermediate === 0
}
/**
* Helper function for applyMaskPenaltyRule1. We need this for doing this calculation in both
* vertical and horizontal orders respectively.
*/
private static applyMaskPenaltyRule1Internal(matrix: ByteMatrix, isHorizontal: boolean): number/*int*/ {
let penalty = 0
const iLimit = isHorizontal ? matrix.getHeight() : matrix.getWidth()
const jLimit = isHorizontal ? matrix.getWidth() : matrix.getHeight()
const array: Array<Uint8Array> = matrix.getArray()
for (let i = 0; i < iLimit; i++) {
let numSameBitCells = 0
let prevBit = -1
for (let j = 0; j < jLimit; j++) {
const bit = isHorizontal ? array[i][j] : array[j][i]
if (bit == prevBit) {
numSameBitCells++
} else {
if (numSameBitCells >= 5) {
penalty += MaskUtil.N1 + (numSameBitCells - 5)
}
numSameBitCells = 1; // Include the cell itself.
prevBit = bit
}
}
if (numSameBitCells >= 5) {
penalty += MaskUtil.N1 + (numSameBitCells - 5)
}
}
return penalty
}
}

View file

@ -0,0 +1,483 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.encoder {*/
import BitArray from './../../common/BitArray'
import ErrorCorrectionLevel from './../decoder/ErrorCorrectionLevel'
import Version from './../decoder/Version'
import ByteMatrix from './ByteMatrix'
import Exception from './../../Exception'
import Integer from './../../util/Integer'
import QRCode from './QRCode'
import MaskUtil from './MaskUtil'
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author dswitkin@google.com (Daniel Switkin) - ported from C++
*/
export default class MatrixUtil {
private constructor() {
// do nothing
}
private static POSITION_DETECTION_PATTERN: Array<Int32Array> = Array.from([
Int32Array.from([1, 1, 1, 1, 1, 1, 1]),
Int32Array.from([1, 0, 0, 0, 0, 0, 1]),
Int32Array.from([1, 0, 1, 1, 1, 0, 1]),
Int32Array.from([1, 0, 1, 1, 1, 0, 1]),
Int32Array.from([1, 0, 1, 1, 1, 0, 1]),
Int32Array.from([1, 0, 0, 0, 0, 0, 1]),
Int32Array.from([1, 1, 1, 1, 1, 1, 1]),
])
private static POSITION_ADJUSTMENT_PATTERN: Array<Int32Array> = Array.from([
Int32Array.from([1, 1, 1, 1, 1]),
Int32Array.from([1, 0, 0, 0, 1]),
Int32Array.from([1, 0, 1, 0, 1]),
Int32Array.from([1, 0, 0, 0, 1]),
Int32Array.from([1, 1, 1, 1, 1]),
])
// From Appendix E. Table 1, JIS0510X:2004 (71: p). The table was double-checked by komatsu.
private static POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE: Array<Int32Array> = Array.from([
Int32Array.from([-1, -1, -1, -1, -1, -1, -1]), // Version 1
Int32Array.from([ 6, 18, -1, -1, -1, -1, -1]), // Version 2
Int32Array.from([ 6, 22, -1, -1, -1, -1, -1]), // Version 3
Int32Array.from([ 6, 26, -1, -1, -1, -1, -1]), // Version 4
Int32Array.from([ 6, 30, -1, -1, -1, -1, -1]), // Version 5
Int32Array.from([ 6, 34, -1, -1, -1, -1, -1]), // Version 6
Int32Array.from([ 6, 22, 38, -1, -1, -1, -1]), // Version 7
Int32Array.from([ 6, 24, 42, -1, -1, -1, -1]), // Version 8
Int32Array.from([ 6, 26, 46, -1, -1, -1, -1]), // Version 9
Int32Array.from([ 6, 28, 50, -1, -1, -1, -1]), // Version 10
Int32Array.from([ 6, 30, 54, -1, -1, -1, -1]), // Version 11
Int32Array.from([ 6, 32, 58, -1, -1, -1, -1]), // Version 12
Int32Array.from([ 6, 34, 62, -1, -1, -1, -1]), // Version 13
Int32Array.from([ 6, 26, 46, 66, -1, -1, -1]), // Version 14
Int32Array.from([ 6, 26, 48, 70, -1, -1, -1]), // Version 15
Int32Array.from([ 6, 26, 50, 74, -1, -1, -1]), // Version 16
Int32Array.from([ 6, 30, 54, 78, -1, -1, -1]), // Version 17
Int32Array.from([ 6, 30, 56, 82, -1, -1, -1]), // Version 18
Int32Array.from([ 6, 30, 58, 86, -1, -1, -1]), // Version 19
Int32Array.from([ 6, 34, 62, 90, -1, -1, -1]), // Version 20
Int32Array.from([ 6, 28, 50, 72, 94, -1, -1]), // Version 21
Int32Array.from([ 6, 26, 50, 74, 98, -1, -1]), // Version 22
Int32Array.from([ 6, 30, 54, 78, 102, -1, -1]), // Version 23
Int32Array.from([ 6, 28, 54, 80, 106, -1, -1]), // Version 24
Int32Array.from([ 6, 32, 58, 84, 110, -1, -1]), // Version 25
Int32Array.from([ 6, 30, 58, 86, 114, -1, -1]), // Version 26
Int32Array.from([ 6, 34, 62, 90, 118, -1, -1]), // Version 27
Int32Array.from([ 6, 26, 50, 74, 98, 122, -1]), // Version 28
Int32Array.from([ 6, 30, 54, 78, 102, 126, -1]), // Version 29
Int32Array.from([ 6, 26, 52, 78, 104, 130, -1]), // Version 30
Int32Array.from([ 6, 30, 56, 82, 108, 134, -1]), // Version 31
Int32Array.from([ 6, 34, 60, 86, 112, 138, -1]), // Version 32
Int32Array.from([ 6, 30, 58, 86, 114, 142, -1]), // Version 33
Int32Array.from([ 6, 34, 62, 90, 118, 146, -1]), // Version 34
Int32Array.from([ 6, 30, 54, 78, 102, 126, 150]), // Version 35
Int32Array.from([ 6, 24, 50, 76, 102, 128, 154]), // Version 36
Int32Array.from([ 6, 28, 54, 80, 106, 132, 158]), // Version 37
Int32Array.from([ 6, 32, 58, 84, 110, 136, 162]), // Version 38
Int32Array.from([ 6, 26, 54, 82, 110, 138, 166]), // Version 39
Int32Array.from([ 6, 30, 58, 86, 114, 142, 170]), // Version 40
])
// Type info cells at the left top corner.
private static TYPE_INFO_COORDINATES: Array<Int32Array> = Array.from([
Int32Array.from([8, 0]),
Int32Array.from([8, 1]),
Int32Array.from([8, 2]),
Int32Array.from([8, 3]),
Int32Array.from([8, 4]),
Int32Array.from([8, 5]),
Int32Array.from([8, 7]),
Int32Array.from([8, 8]),
Int32Array.from([7, 8]),
Int32Array.from([5, 8]),
Int32Array.from([4, 8]),
Int32Array.from([3, 8]),
Int32Array.from([2, 8]),
Int32Array.from([1, 8]),
Int32Array.from([0, 8]),
])
// From Appendix D in JISX0510:2004 (p. 67)
private static VERSION_INFO_POLY = 0x1f25 // 1 1111 0010 0101
// From Appendix C in JISX0510:2004 (p.65).
private static TYPE_INFO_POLY = 0x537
private static TYPE_INFO_MASK_PATTERN = 0x5412
// Set all cells to -1 (TYPESCRIPTPORT: 255). -1 (TYPESCRIPTPORT: 255) means that the cell is empty (not set yet).
//
// JAVAPORT: We shouldn't need to do this at all. The code should be rewritten to begin encoding
// with the ByteMatrix initialized all to zero.
public static clearMatrix(matrix: ByteMatrix): void {
// TYPESCRIPTPORT: we use UintArray se changed here from -1 to 255
matrix.clear(/*(byte) *//*-1*/255)
}
// Build 2D matrix of QR Code from "dataBits" with "ecLevel", "version" and "getMaskPattern". On
// success, store the result in "matrix" and return true.
public static buildMatrix(dataBits: BitArray,
ecLevel: ErrorCorrectionLevel,
version: Version,
maskPattern: number/*int*/,
matrix: ByteMatrix): void /*throws WriterException*/ {
MatrixUtil.clearMatrix(matrix)
MatrixUtil.embedBasicPatterns(version, matrix)
// Type information appear with any version.
MatrixUtil.embedTypeInfo(ecLevel, maskPattern, matrix)
// Version info appear if version >= 7.
MatrixUtil.maybeEmbedVersionInfo(version, matrix)
// Data should be embedded at end.
MatrixUtil.embedDataBits(dataBits, maskPattern, matrix)
}
// Embed basic patterns. On success, modify the matrix and return true.
// The basic patterns are:
// - Position detection patterns
// - Timing patterns
// - Dark dot at the left bottom corner
// - Position adjustment patterns, if need be
public static embedBasicPatterns(version: Version, matrix: ByteMatrix): void /*throws WriterException*/ {
// Let's get started with embedding big squares at corners.
MatrixUtil.embedPositionDetectionPatternsAndSeparators(matrix)
// Then, embed the dark dot at the left bottom corner.
MatrixUtil.embedDarkDotAtLeftBottomCorner(matrix)
// Position adjustment patterns appear if version >= 2.
MatrixUtil.maybeEmbedPositionAdjustmentPatterns(version, matrix)
// Timing patterns should be embedded after position adj. patterns.
MatrixUtil.embedTimingPatterns(matrix)
}
// Embed type information. On success, modify the matrix.
public static embedTypeInfo(ecLevel: ErrorCorrectionLevel, maskPattern: number/*int*/, matrix: ByteMatrix): void
/*throws WriterException*/ {
const typeInfoBits: BitArray = new BitArray()
MatrixUtil.makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits)
for (let i = 0, size = typeInfoBits.getSize(); i < size; ++i) {
// Place bits in LSB to MSB order. LSB (least significant bit) is the last value in
// "typeInfoBits".
const bit: boolean = typeInfoBits.get(typeInfoBits.getSize() - 1 - i)
// Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46).
const coordinates: Int32Array = MatrixUtil.TYPE_INFO_COORDINATES[i]
const x1 = coordinates[0]
const y1 = coordinates[1]
matrix.setBoolean(x1, y1, bit)
if (i < 8) {
// Right top corner.
const x2 = matrix.getWidth() - i - 1
const y2 = 8
matrix.setBoolean(x2, y2, bit)
} else {
// Left bottom corner.
const x2 = 8
const y2 = matrix.getHeight() - 7 + (i - 8)
matrix.setBoolean(x2, y2, bit)
}
}
}
// Embed version information if need be. On success, modify the matrix and return true.
// See 8.10 of JISX0510:2004 (p.47) for how to embed version information.
public static maybeEmbedVersionInfo(version: Version, matrix: ByteMatrix): void /*throws WriterException*/ {
if (version.getVersionNumber() < 7) { // Version info is necessary if version >= 7.
return; // Don't need version info.
}
const versionInfoBits = new BitArray()
MatrixUtil.makeVersionInfoBits(version, versionInfoBits)
let bitIndex = 6 * 3 - 1; // It will decrease from 17 to 0.
for (let i = 0; i < 6; ++i) {
for (let j = 0; j < 3; ++j) {
// Place bits in LSB (least significant bit) to MSB order.
const bit: boolean = versionInfoBits.get(bitIndex)
bitIndex--
// Left bottom corner.
matrix.setBoolean(i, matrix.getHeight() - 11 + j, bit)
// Right bottom corner.
matrix.setBoolean(matrix.getHeight() - 11 + j, i, bit)
}
}
}
// Embed "dataBits" using "getMaskPattern". On success, modify the matrix and return true.
// For debugging purposes, it skips masking process if "getMaskPattern" is -1(TYPESCRIPTPORT: 255).
// See 8.7 of JISX0510:2004 (p.38) for how to embed data bits.
public static embedDataBits(dataBits: BitArray, maskPattern: number/*int*/, matrix: ByteMatrix): void
/*throws WriterException*/ {
let bitIndex = 0
let direction = -1
// Start from the right bottom cell.
let x = matrix.getWidth() - 1
let y = matrix.getHeight() - 1
while (x > 0) {
// Skip the vertical timing pattern.
if (x == 6) {
x -= 1
}
while (y >= 0 && y < matrix.getHeight()) {
for (let i = 0; i < 2; ++i) {
const xx = x - i
// Skip the cell if it's not empty.
if (!MatrixUtil.isEmpty(matrix.get(xx, y))) {
continue
}
let bit: boolean
if (bitIndex < dataBits.getSize()) {
bit = dataBits.get(bitIndex)
++bitIndex
} else {
// Padding bit. If there is no bit left, we'll fill the left cells with 0, as described
// in 8.4.9 of JISX0510:2004 (p. 24).
bit = false
}
// Skip masking if mask_pattern is -1 (TYPESCRIPTPORT: 255).
if (maskPattern !== 255 && MaskUtil.getDataMaskBit(maskPattern, xx, y)) {
bit = !bit
}
matrix.setBoolean(xx, y, bit)
}
y += direction
}
direction = -direction; // Reverse the direction.
y += direction
x -= 2; // Move to the left.
}
// All bits should be consumed.
if (bitIndex !== dataBits.getSize()) {
throw new Exception(Exception.WriterException, "Not all bits consumed: " + bitIndex + '/' + dataBits.getSize())
}
}
// Return the position of the most significant bit set (one: to) in the "value". The most
// significant bit is position 32. If there is no bit set, return 0. Examples:
// - findMSBSet(0) => 0
// - findMSBSet(1) => 1
// - findMSBSet(255) => 8
public static findMSBSet(value: number/*int*/): number/*int*/ {
return 32 - Integer.numberOfLeadingZeros(value)
}
// Calculate BCH (Bose-Chaudhuri-Hocquenghem) code for "value" using polynomial "poly". The BCH
// code is used for encoding type information and version information.
// Example: Calculation of version information of 7.
// f(x) is created from 7.
// - 7 = 000111 in 6 bits
// - f(x) = x^2 + x^1 + x^0
// g(x) is given by the standard (p. 67)
// - g(x) = x^12 + x^11 + x^10 + x^9 + x^8 + x^5 + x^2 + 1
// Multiply f(x) by x^(18 - 6)
// - f'(x) = f(x) * x^(18 - 6)
// - f'(x) = x^14 + x^13 + x^12
// Calculate the remainder of f'(x) / g(x)
// x^2
// __________________________________________________
// g(x) )x^14 + x^13 + x^12
// x^14 + x^13 + x^12 + x^11 + x^10 + x^7 + x^4 + x^2
// --------------------------------------------------
// x^11 + x^10 + x^7 + x^4 + x^2
//
// The remainder is x^11 + x^10 + x^7 + x^4 + x^2
// Encode it in binary: 110010010100
// The return value is 0xc94 (1100 1001 0100)
//
// Since all coefficients in the polynomials are 1 or 0, we can do the calculation by bit
// operations. We don't care if coefficients are positive or negative.
public static calculateBCHCode(value: number/*int*/, poly: number/*int*/): number/*int*/ {
if (poly === 0) {
throw new Exception(Exception.IllegalArgumentException, "0 polynomial")
}
// If poly is "1 1111 0010 0101" (version info poly), msbSetInPoly is 13. We'll subtract 1
// from 13 to make it 12.
const msbSetInPoly = MatrixUtil.findMSBSet(poly)
value <<= msbSetInPoly - 1
// Do the division business using exclusive-or operations.
while (MatrixUtil.findMSBSet(value) >= msbSetInPoly) {
value ^= poly << (MatrixUtil.findMSBSet(value) - msbSetInPoly)
}
// Now the "value" is the remainder (i.e. the BCH code)
return value
}
// Make bit vector of type information. On success, store the result in "bits" and return true.
// Encode error correction level and mask pattern. See 8.9 of
// JISX0510:2004 (p.45) for details.
public static makeTypeInfoBits(ecLevel: ErrorCorrectionLevel, maskPattern: number/*int*/, bits: BitArray): void
/*throws WriterException*/ {
if (!QRCode.isValidMaskPattern(maskPattern)) {
throw new Exception(Exception.WriterException, "Invalid mask pattern")
}
const typeInfo = (ecLevel.getBits() << 3) | maskPattern
bits.appendBits(typeInfo, 5)
const bchCode = MatrixUtil.calculateBCHCode(typeInfo, MatrixUtil.TYPE_INFO_POLY)
bits.appendBits(bchCode, 10)
const maskBits = new BitArray()
maskBits.appendBits(MatrixUtil.TYPE_INFO_MASK_PATTERN, 15)
bits.xor(maskBits)
if (bits.getSize() !== 15) { // Just in case.
throw new Exception(Exception.WriterException, "should not happen but we got: " + bits.getSize())
}
}
// Make bit vector of version information. On success, store the result in "bits" and return true.
// See 8.10 of JISX0510:2004 (p.45) for details.
public static makeVersionInfoBits(version: Version, bits: BitArray): void /*throws WriterException*/ {
bits.appendBits(version.getVersionNumber(), 6)
const bchCode = MatrixUtil.calculateBCHCode(version.getVersionNumber(), MatrixUtil.VERSION_INFO_POLY)
bits.appendBits(bchCode, 12)
if (bits.getSize() !== 18) { // Just in case.
throw new Exception(Exception.WriterException, "should not happen but we got: " + bits.getSize())
}
}
// Check if "value" is empty.
private static isEmpty(value: number/*int*/): boolean {
return value === 255//-1
}
private static embedTimingPatterns(matrix: ByteMatrix): void {
// -8 is for skipping position detection patterns (7: size), and two horizontal/vertical
// separation patterns (1: size). Thus, 8 = 7 + 1.
for (let i = 8; i < matrix.getWidth() - 8; ++i) {
const bit = (i + 1) % 2
// Horizontal line.
if (MatrixUtil.isEmpty(matrix.get(i, 6))) {
matrix.setNumber(i, 6, bit)
}
// Vertical line.
if (MatrixUtil.isEmpty(matrix.get(6, i))) {
matrix.setNumber(6, i, bit)
}
}
}
// Embed the lonely dark dot at left bottom corner. JISX0510:2004 (p.46)
private static embedDarkDotAtLeftBottomCorner(matrix: ByteMatrix): void /*throws WriterException*/ {
if (matrix.get(8, matrix.getHeight() - 8) === 0) {
throw new Exception(Exception.WriterException)
}
matrix.setNumber(8, matrix.getHeight() - 8, 1)
}
private static embedHorizontalSeparationPattern(xStart: number/*int*/,
yStart: number/*int*/,
matrix: ByteMatrix): void /*throws WriterException*/ {
for (let x = 0; x < 8; ++x) {
if (!MatrixUtil.isEmpty(matrix.get(xStart + x, yStart))) {
throw new Exception(Exception.WriterException)
}
matrix.setNumber(xStart + x, yStart, 0)
}
}
private static embedVerticalSeparationPattern(xStart: number/*int*/,
yStart: number/*int*/,
matrix: ByteMatrix): void /*throws WriterException*/ {
for (let y = 0; y < 7; ++y) {
if (!MatrixUtil.isEmpty(matrix.get(xStart, yStart + y))) {
throw new Exception(Exception.WriterException)
}
matrix.setNumber(xStart, yStart + y, 0)
}
}
private static embedPositionAdjustmentPattern(xStart: number/*int*/, yStart: number/*int*/, matrix: ByteMatrix): void {
for (let y = 0; y < 5; ++y) {
const patternY: Int32Array = MatrixUtil.POSITION_ADJUSTMENT_PATTERN[y]
for (let x = 0; x < 5; ++x) {
matrix.setNumber(xStart + x, yStart + y, patternY[x])
}
}
}
private static embedPositionDetectionPattern(xStart: number/*int*/, yStart: number/*int*/, matrix: ByteMatrix): void {
for (let y = 0; y < 7; ++y) {
const patternY: Int32Array = MatrixUtil.POSITION_DETECTION_PATTERN[y]
for (let x = 0; x < 7; ++x) {
matrix.setNumber(xStart + x, yStart + y, patternY[x])
}
}
}
// Embed position detection patterns and surrounding vertical/horizontal separators.
private static embedPositionDetectionPatternsAndSeparators(matrix: ByteMatrix): void /*throws WriterException*/ {
// Embed three big squares at corners.
const pdpWidth = MatrixUtil.POSITION_DETECTION_PATTERN[0].length
// Left top corner.
MatrixUtil.embedPositionDetectionPattern(0, 0, matrix)
// Right top corner.
MatrixUtil.embedPositionDetectionPattern(matrix.getWidth() - pdpWidth, 0, matrix)
// Left bottom corner.
MatrixUtil.embedPositionDetectionPattern(0, matrix.getWidth() - pdpWidth, matrix)
// Embed horizontal separation patterns around the squares.
const hspWidth = 8
// Left top corner.
MatrixUtil.embedHorizontalSeparationPattern(0, hspWidth - 1, matrix)
// Right top corner.
MatrixUtil.embedHorizontalSeparationPattern(matrix.getWidth() - hspWidth,
hspWidth - 1, matrix)
// Left bottom corner.
MatrixUtil.embedHorizontalSeparationPattern(0, matrix.getWidth() - hspWidth, matrix)
// Embed vertical separation patterns around the squares.
const vspSize = 7
// Left top corner.
MatrixUtil.embedVerticalSeparationPattern(vspSize, 0, matrix)
// Right top corner.
MatrixUtil.embedVerticalSeparationPattern(matrix.getHeight() - vspSize - 1, 0, matrix)
// Left bottom corner.
MatrixUtil.embedVerticalSeparationPattern(vspSize, matrix.getHeight() - vspSize,
matrix)
}
// Embed position adjustment patterns if need be.
private static maybeEmbedPositionAdjustmentPatterns(version: Version, matrix: ByteMatrix): void {
if (version.getVersionNumber() < 2) { // The patterns appear if version >= 2
return
}
const index = version.getVersionNumber() - 1
const coordinates: Int32Array = MatrixUtil.POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index]
for (let i = 0, length = coordinates.length; i != length; i++) {
const y = coordinates[i]
if (y >= 0) {
for (let j = 0; j != length; j++) {
const x = coordinates[j]
if (x >= 0 && MatrixUtil.isEmpty(matrix.get(x, y))) {
// If the cell is unset, we embed the position adjustment pattern here.
// -2 is necessary since the x/y coordinates point to the center of the pattern, not the
// left top corner.
MatrixUtil.embedPositionAdjustmentPattern(x - 2, y - 2, matrix)
}
}
}
}
}
}

View file

@ -0,0 +1,110 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*namespace com.google.zxing.qrcode.encoder {*/
import ErrorCorrectionLevel from './../decoder/ErrorCorrectionLevel'
import Mode from './../decoder/Mode'
import Version from './../decoder/Version'
import StringBuilder from './../../util/StringBuilder'
import ByteMatrix from './ByteMatrix'
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author dswitkin@google.com (Daniel Switkin) - ported from C++
*/
export default class QRCode {
public static NUM_MASK_PATTERNS = 8
private mode: Mode
private ecLevel: ErrorCorrectionLevel
private version: Version
private maskPattern: number/*int*/
private matrix: ByteMatrix
public constructor() {
this.maskPattern = -1
}
public getMode(): Mode {
return this.mode
}
public getECLevel(): ErrorCorrectionLevel {
return this.ecLevel
}
public getVersion(): Version {
return this.version
}
public getMaskPattern(): number/*int*/ {
return this.maskPattern
}
public getMatrix(): ByteMatrix {
return this.matrix
}
/*@Override*/
public toString(): string {
const result = new StringBuilder()//(200)
result.append("<<\n")
result.append(" mode: ")
result.append(this.mode ? this.mode.toString() : "null")
result.append("\n ecLevel: ")
result.append(this.ecLevel ? this.ecLevel.toString() : "null")
result.append("\n version: ")
result.append(this.version ? this.version.toString() : "null")
result.append("\n maskPattern: ")
result.append(this.maskPattern.toString())
if (this.matrix) {
result.append("\n matrix:\n");
result.append(this.matrix.toString())
} else {
result.append("\n matrix: null\n")
}
result.append(">>\n")
return result.toString()
}
public setMode(value: Mode): void {
this.mode = value
}
public setECLevel(value: ErrorCorrectionLevel): void {
this.ecLevel = value
}
public setVersion(version: Version): void {
this.version = version
}
public setMaskPattern(value: number/*int*/): void {
this.maskPattern = value
}
public setMatrix(value: ByteMatrix): void {
this.matrix = value
}
// Check if "mask_pattern" is valid.
public static isValidMaskPattern(maskPattern: number/*int*/): boolean {
return maskPattern >= 0 && maskPattern < QRCode.NUM_MASK_PATTERNS
}
}

View file

@ -0,0 +1,42 @@
export default class Arrays {
public static equals(first: any, second: any): boolean {
if (!first) {
return false
}
if (!second) {
return false
}
if (!first.length) {
return false
}
if (!second.length) {
return false
}
if (first.length !== second.length) {
return false
}
for (let i = 0, length = first.length; i < length; i++) {
if (first[i] !== second[i]) {
return false
}
}
return true
}
public static hashCode(a: any) {
if (a === null) {
return 0
}
let result = 1
for (const element of a) {
result = 31 * result + element
}
return result
}
public static fillUint8Array(a: Uint8Array, value: number) {
for(let i = 0; i != a.length; i++) {
a[i] = value
}
}
}

View file

@ -0,0 +1,5 @@
export default class Float {
public static floatToIntBits(f: number): number {
return f
}
}

View file

@ -0,0 +1,74 @@
export default class Integer {
public static MIN_VALUE_32_BITS = -2147483648
public static numberOfTrailingZeros(i: number): number {
let y;
if (i === 0) return 32
let n = 31
y = i << 16
if (y !== 0) {
n -= 16
i = y
}
y = i << 8
if (y !== 0) {
n -= 8
i = y
}
y = i << 4
if (y !== 0) {
n -= 4
i = y
}
y = i << 2
if (y !== 0) {
n -= 2
i = y
}
return n - ((i << 1) >>> 31)
}
public static numberOfLeadingZeros(i: number): number {
// HD, Figure 5-6
if (i === 0) {
return 32
}
let n = 1
if (i >>> 16 === 0) {
n += 16
i <<= 16
}
if (i >>> 24 === 0) {
n += 8
i <<= 8
}
if (i >>> 28 === 0) {
n += 4
i <<= 4
}
if (i >>> 30 === 0) {
n += 2
i <<= 2
}
n -= i >>> 31
return n
}
public static toHexString(i: number) {
return i.toString(16)
}
// Returns the number of one-bits in the two's complement binary representation of the specified int value. This function is sometimes referred to as the population count.
// Returns:
// the number of one-bits in the two's complement binary representation of the specified int value.
public static bitCount(i: number): number {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555)
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333)
i = (i + (i >>> 4)) & 0x0f0f0f0f
i = i + (i >>> 8)
i = i + (i >>> 16)
return i & 0x3f
}
}

View file

@ -0,0 +1,29 @@
export default class StringBuilder {
public constructor(private value: string = "") {
}
public append(s: string): StringBuilder {
this.value += s
return this
}
public length(): number {
return this.value.length
}
public charAt(n: number): string {
return this.value.charAt(n)
}
public deleteCharAt(n: number) {
this.value = this.value.substr(0, n) + this.value.substring(n + 1)
}
public setCharAt(n: number, c: string) {
this.value = this.value.substr(0, n) + c + this.value.substr(n + 1)
}
public toString(): string {
return this.value
}
}

View file

@ -0,0 +1,76 @@
// declare require to support dynamic text-encoding module loading in node
declare function require(moduleName: string): any
// declare window to use in browser
declare var window: any
import { TextDecoder as TextDecoderFromTE, TextEncoder as TextEncoderFromTE } from 'text-encoding'
import CharacterSetECI from './../common/CharacterSetECI'
import Exception from './../Exception'
export default class StringEncoding {
public static decode(bytes: Uint8Array, encoding: string): string {
if (StringEncoding.isBrowser()) {
const TextDecoderBrowser = window['TextDecoder']
// use TextEncoder if is available (should be in newer browsers)
if (undefined !== TextDecoderBrowser) {
console.log(TextDecoderBrowser)
return new TextDecoderBrowser(encoding).decode(bytes)
} else {
// fall back to minimal decoding
return StringEncoding.decodeFallBack(bytes, encoding)
}
} else {
const TextDecoderFromTEClass: typeof TextDecoderFromTE = require('text-encoding').TextDecoder
return new TextDecoderFromTEClass(encoding).decode(bytes)
}
}
public static encode(s: string, encoding: string): Uint8Array {
if (StringEncoding.isBrowser()) {
const TextEncoderBrowser = window['TextEncoder']
// use TextEncoder if is available (should be in newer browsers)
const ec = CharacterSetECI.getCharacterSetECIByName(encoding)
if (undefined !== TextEncoderBrowser) {
// TODO: TextEncoder only supports utf-8 encoding as per specs
return new TextEncoderBrowser(encoding).encode(s)
} else {
// fall back to minimal decoding
return StringEncoding.encodeFallBack(s, encoding)
}
} else {
// Note: NONSTANDARD_allowLegacyEncoding is required for other encodings than UTF8
// TextEncoder only encodes to UTF8 by default as specified by encoding.spec.whatwg.org
const TextEncoderFromTEClass: typeof TextEncoderFromTE = require('text-encoding').TextEncoder
return new TextEncoderFromTEClass(encoding, { NONSTANDARD_allowLegacyEncoding: true }).encode(s)
}
}
private static isBrowser(): boolean {
return typeof window !== 'undefined' && ({}).toString.call(window) === '[object Window]'
}
private static decodeFallBack(bytes: Uint8Array, encoding: string): string {
const ec = CharacterSetECI.getCharacterSetECIByName(encoding)
if (ec.equals(CharacterSetECI.UTF8) || ec.equals(CharacterSetECI.ISO8859_1) || ec.equals(CharacterSetECI.ASCII)) {
let s = ''
for(let i = 0, s = ''; i < bytes.length; i++) {
let h = bytes[i].toString(16)
if(h.length < 2) {
h = '0' + h
}
s += '%' + h
}
return decodeURIComponent(s)
} else if (ec.equals(CharacterSetECI.UnicodeBigUnmarked)) {
return String.fromCharCode.apply(null, new Uint16Array(bytes.buffer))
} else {
throw new Exception(Exception.UnsupportedOperationException, `encoding ${encoding} not supported`)
}
}
private static encodeFallBack(s: string, encoding: string): Uint8Array {
// TODO: encode
return null
}
}

View file

@ -0,0 +1,16 @@
export default class System {
//public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
public static arraycopy(src: any, srcPos: number, dest: any, destPos: number, length: number) {
// TODO: better use split or set?
let i = srcPos
let j = destPos
let c = length
while(c--) {
dest[j++] = src[i++]
}
}
public static currentTimeMillis() {
return Date.now()
}
}

View file

@ -26,6 +26,18 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.0.3.tgz#5d8d24e0033fc6393efadc85cb59c1f638095c9a"
integrity sha512-zkOxCS/fA+3SsdA+9Yun0iANxzhQRiNwTvJSr6N95JhuJ/x27z9G2URx1Jpt3zYFfCGUXZGL5UDxt5eyLE7wgw==
"@types/qrcode@^1.3.3":
version "1.3.3"
resolved "https://registry.yarnpkg.com/@types/qrcode/-/qrcode-1.3.3.tgz#589e42514d7054f9dd985a20e0531f79b5b615ba"
integrity sha512-+5vox9KhEPGP+d2ah8V+gnHAaTDvFHssLz8KJS7OgJuessGGybChJYfmo+fwNFzOVUtfcWkTCJqkFDRz15hCYw==
dependencies:
"@types/node" "*"
"@types/text-encoding@^0.0.35":
version "0.0.35"
resolved "https://registry.yarnpkg.com/@types/text-encoding/-/text-encoding-0.0.35.tgz#6f14474e0b232bc70c59677aadc65dcc5a99c3a9"
integrity sha512-jfo/A88XIiAweUa8np+1mPbm3h2w0s425YrI8t3wk5QxhH6UI7w517MboNVnGDeMSuoFwA8Rwmklno+FicvV4g==
"@webassemblyjs/ast@1.8.5":
version "1.8.5"
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.8.5.tgz#51b1c5fe6576a34953bf4b253df9f0d490d9e359"
@ -602,6 +614,13 @@ camelcase@^5.0.0:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320"
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
can-promise@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/can-promise/-/can-promise-0.0.1.tgz#7a7597ad801fb14c8b22341dfec314b6bd6ad8d3"
integrity sha512-gzVrHyyrvgt0YpDm7pn04MQt8gjh0ZAhN4ZDyCRtGl6YnuuK6b4aiUTD7G52r9l4YNmxfTtEscb92vxtAlL6XQ==
dependencies:
window-or-global "^1.0.1"
chalk@^2.3.0, chalk@^2.4.1:
version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
@ -1010,6 +1029,11 @@ diffie-hellman@^5.0.0:
miller-rabin "^4.0.0"
randombytes "^2.0.0"
dijkstrajs@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/dijkstrajs/-/dijkstrajs-1.0.1.tgz#d3cd81221e3ea40742cfcde556d4e99e98ddc71b"
integrity sha1-082BIh4+pAdCz83lVtTpnpjdxxs=
dns-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/dns-equal/-/dns-equal-1.0.0.tgz#b39e7f1da6eb0a75ba9c17324b34753c47e0654d"
@ -1906,6 +1930,11 @@ isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0:
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=
isarray@^2.0.1:
version "2.0.4"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-2.0.4.tgz#38e7bcbb0f3ba1b7933c86ba1894ddfc3781bbb7"
integrity sha512-GMxXOiUirWg1xTKRipM0Ek07rX+ubx4nNVElTJdNLYmNO/2YrDkgJGw9CljXn+r4EWiDQg/8lsRdHyg2PJuUaA==
isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
@ -2684,6 +2713,11 @@ pkg-dir@^3.0.0:
dependencies:
find-up "^3.0.0"
pngjs@^3.3.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/pngjs/-/pngjs-3.4.0.tgz#99ca7d725965fb655814eaf65f38f12bbdbf555f"
integrity sha512-NCrCHhWmnQklfH4MtJMRjZ2a8c80qXeMlQMv2uVp9ISJMTt562SbGd6n2oq0PaPgKm7Z6pL9E2UlLIhC+SHL3w==
portfinder@^1.0.20:
version "1.0.20"
resolved "https://registry.yarnpkg.com/portfinder/-/portfinder-1.0.20.tgz#bea68632e54b2e13ab7b0c4775e9b41bf270e44a"
@ -2778,6 +2812,17 @@ punycode@^2.1.0:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
qrcode@^1.3.3:
version "1.3.3"
resolved "https://registry.yarnpkg.com/qrcode/-/qrcode-1.3.3.tgz#5ef50c0c890cffa1897f452070f0f094936993de"
integrity sha512-SH7V13AcJusH3GT8bMNOGz4w0L+LjcpNOU/NiOgtBhT/5DoWeZE6D5ntMJnJ84AMkoaM4kjJJoHoh9g++8lWFg==
dependencies:
can-promise "0.0.1"
dijkstrajs "^1.0.1"
isarray "^2.0.1"
pngjs "^3.3.0"
yargs "^12.0.5"
qs@6.7.0:
version "6.7.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
@ -3407,6 +3452,11 @@ terser@^4.0.0:
source-map "~0.6.1"
source-map-support "~0.5.10"
text-encoding@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.7.0.tgz#f895e836e45990624086601798ea98e8f36ee643"
integrity sha512-oJQ3f1hrOnbRLOcwKz0Liq2IcrvDeZRHXhd9RgLrsT+DjWY/nty1Hi7v3dtkaEYbPYe0mUoOfzRrMwfXXwgPUA==
through2@^2.0.0:
version "2.0.5"
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.5.tgz#01c1e39eb31d07cb7d03a96a70823260b23132cd"
@ -3786,6 +3836,11 @@ wide-align@^1.1.0:
dependencies:
string-width "^1.0.2 || 2"
window-or-global@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/window-or-global/-/window-or-global-1.0.1.tgz#dbe45ba2a291aabc56d62cf66c45b7fa322946de"
integrity sha1-2+RboqKRqrxW1iz2bEW3+jIpRt4=
worker-farm@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/worker-farm/-/worker-farm-1.7.0.tgz#26a94c5391bbca926152002f69b84a4bf772e5a8"