import fs from 'fs' ; import binary from './lib/binary' ; import png from './lib/png' ; import EncodingMode from './types/enums/EncodingMode' ; import OutputOption from './types/enums/OutputOption' ; import OutputType from './types/enums/OutputType' ; import SymbologyConfig from './types/SymbologyConfig' ; import SymbologyResult from './types/SymbologyResult' ; /**
 * Default Symbology config, populated with default values
 */ const defaultConfig = { symbology : 20 , height : 50 , whitespaceWidth : 0 , borderWidth : 0 , outputOptions : OutputOption . BARCODE_NO_ASCII , foregroundColor : '000000FF' , backgroundColor : 'FFFFFFFF' , scale : 1.0 , option1 : - 1 , option2 : - 1 , option3 : - 1 , showHumanReadableText : true , encoding : EncodingMode . DATA_MODE , eci : 0 , primary : '' , rotation : 0 , dotSize : 0.8 } ; param { SymbologyConfig } config param { string } barcodeData param { OutputType } outputType returns { Promise < SymbologyResult > } export async function createStream ( config , barcodeData , outputType = OutputType . PNG ) { const symbol = { ... defaultConfig , ... config , fileName : `out.${ outputType }` } ; const res = await binary . invoke ( symbol , barcodeData , outputType ) ; if ( outputType === OutputType . PNG ) { const image = png . render ( res . bitmap , res . width , res . height , symbol . backgroundColor , symbol . foregroundColor ) ; const base64Data = await png . blobToBase64 ( image ) ; return { data : base64Data , width : res . width , height : res . height , message : res . message } ; } return { data : res . encodedData , width : res . width , height : res . height , message : res . message } ; } param { SymbologyConfig } config param { string } barcodeData returns { Promise < SymbologyResult > } export async function createFile ( config , barcodeData ) { const symbol = { ... defaultConfig , ... config } ; if ( ! symbol . fileName ) { return Promise . reject ( 'fileName is required.' ) ; } const outputType = binary . getOutputType ( symbol . fileName ) ; const res = await binary . invoke ( symbol , barcodeData , outputType ) ; if ( outputType === OutputType . PNG ) { const image = png . render ( res . bitmap , res . width , res . height , symbol . backgroundColor , symbol . foregroundColor ) ; const buffer = png . getBuffer ( image ) ; fs . writeFileSync ( symbol . fileName , buffer ) ; } else { fs . writeFileSync ( symbol . fileName , res . encodedData ) ; } return { width : res . width , height : res . height , message : res . message } ; }