import * as assert from 'assert' ; import { BarcodeFormat } from '@zxing/library' ; import { EncodeHintType } from '@zxing/library' ; import { Writer } from '@zxing/library' ; import { BitMatrix } from '@zxing/library' ; import { QRCodeDecoderErrorCorrectionLevel } from '@zxing/library' ; import SharpImage from '../util/SharpImage' ; import { QRCodeWriter } from '@zxing/library' ; import { ZXingStringEncoding } from '@zxing/library' ; import { createCustomEncoder } from '../util/textEncodingFactory' ; const path = require ( 'path' ) ; author satorux google author dswitkin google describe ( 'QRCodeWriter' , ( ) => { ZXingStringEncoding . customEncoder = ( b , e ) => createCustomEncoder ( e ) . encode ( b ) ; const BASE_IMAGE_PATH = 'src/test/resources/golden/qrcode/' ; it ( 'testQRCodeWriter' , ( ) => { const bigEnough = 256 ; const writer = new QRCodeWriter ( ) ; let matrix = writer . encode ( 'http://www.google.com/' , BarcodeFormat . QR_CODE , bigEnough , bigEnough , null ) ; assert . strictEqual ( matrix !== null , true ) ; assert . strictEqual ( matrix . getWidth ( ) , bigEnough ) ; assert . strictEqual ( matrix . getHeight ( ) , bigEnough ) ; const tooSmall = 20 ; matrix = writer . encode ( 'http://www.google.com/' , BarcodeFormat . QR_CODE , tooSmall , tooSmall , null ) ; assert . strictEqual ( matrix !== null , true ) ; assert . strictEqual ( tooSmall < matrix . getWidth ( ) , true ) ; assert . strictEqual ( tooSmall < matrix . getHeight ( ) , true ) ; const strangeWidth = 500 ; const strangeHeight = 100 ; matrix = writer . encode ( 'http://www.google.com/' , BarcodeFormat . QR_CODE , strangeWidth , strangeHeight , null ) ; assert . strictEqual ( matrix !== null , true ) ; assert . strictEqual ( matrix . getWidth ( ) , strangeWidth ) ; assert . strictEqual ( matrix . getHeight ( ) , strangeHeight ) ; } ) ; async function compareToGoldenFile ( contents , ecLevel , resolution , fileName ) { const filePath = path . resolve ( BASE_IMAGE_PATH , fileName ) ; let goldenResult ; try { goldenResult = await SharpImage . loadAsBitMatrix ( filePath ) ; } catch ( err ) { assert . ok ( false , err ) ; } const hints = new Map < EncodeHintType , QRCodeDecoderErrorCorrectionLevel > ( ) ; hints . set ( EncodeHintType . ERROR_CORRECTION , ecLevel ) ; const writer = new QRCodeWriter ( ) ; const generatedResult = writer . encode ( contents , BarcodeFormat . QR_CODE , resolution , resolution , hints ) ; assert . strictEqual ( generatedResult . getWidth ( ) , resolution ) ; assert . strictEqual ( generatedResult . getHeight ( ) , resolution ) ; assert . strictEqual ( generatedResult . equals ( goldenResult ) , true ) ; } it ( 'testRegressionTest' , ( ) => { compareToGoldenFile ( 'http://www.google.com/' , QRCodeDecoderErrorCorrectionLevel . M , 99 , 'renderer-test-01.png' ) ; } ) ; } ) ;