const ea = require("./ExamplesAssist")
const aspose_barcode = ea.aspose_barcode
let BarCodeReader = aspose_barcode.BarCodeReader;
let BarcodeGenerator = aspose_barcode.BarcodeGenerator;
let EncodeTypes = aspose_barcode.EncodeTypes;
let QualitySettings = aspose_barcode.QualitySettings;

class BarCodeReaderExamples
{
    subfolder = "resources/recognition/";

    howToReadFromFile()
    {
        console.log("\n---\nfunction '" + this.howToReadFromFile.name + "'\n");
        let file_name = "code11.png";
        let full_path = this.subfolder + file_name;
        let reader = new BarCodeReader(full_path, null, null);
        reader.readBarCodes().forEach(function (result, i, results)
        {
            console.log(result.getCodeText());

            console.log(result.getCodeTypeName());
        });

    }

    howToReadImageBytes()
    {
        let fileName = "code11.png";
        let reader = new BarCodeReader(ea.loadImageByName(this.subfolder, fileName), null, null);
        reader.readBarCodes().forEach(function (result, i, results)
        {
            console.log(result.getCodeText());

            console.log(result.getCodeTypeName());
        });
    }

    howToSetQualitySettings()
    {
        console.log("\n---\nfunction '" + this.howToSetQualitySettings.name + "'\n");
        let file_name = "code11.png";
        let full_path = this.subfolder + file_name;
        let reader = new BarCodeReader(full_path, null, null);
        reader.setQualitySettings(QualitySettings.getHighPerformance());
        reader.getQualitySettings().setAllowMedianSmoothing(true);
        reader.getQualitySettings().setMedianSmoothingWindowSize(5);

        reader.readBarCodes().forEach(function (result, i, results)
        {
            console.log(result.getCodeText());

            console.log(result.getCodeTypeName());
        });

    }


    howToGetCodeBytes()
    {
        console.log("\n---\nfunction '" + this.howToGetCodeBytes.name + "'\n");
        let expectedCodeBytes = ["105", "99", "70", "1", "61", "0", "0", "2", "70", "10", "82", "20", "40", "0", "97", "106"];
        let fileName = "example2.jpg";
        let reader = new BarCodeReader(ea.loadImageByName(this.subfolder, fileName), null, null);
        reader.readBarCodes().forEach(function (result, i, results)
        {
            let actualCodeBytes = result.getCodeBytes();
            console.log("expected code bytes  : " + expectedCodeBytes.length);

            console.log("actual code bytes  : " + actualCodeBytes.length);
        });
    }

    howToRecognitionCode128()
    {
        console.log("\n---\nfunction '" + this.howToRecognitionCode128.name + "'\n");
        try
        {
            let fileName = "code128.jpg";
            let reader = new BarCodeReader(ea.loadImageByName(this.subfolder, fileName), null, DecodeType.CODE_128);
            reader.readBarCodes().forEach(function (result, i, results)
            {
                console.log("Code Text : " + result.getCodeTypeName());

                console.log("Code Type : " + result.getCodeText());
            });

        } catch (e)
        {
            console.log(e.getMessage());
        }
    }

    howToRecognitionCode11()
    {
        console.log("\n---\nfunction '" + this.howToRecognitionCode11.name + "'\n");
        try
        {
            let fileName = "code11.png";
            let reader = new BarCodeReader(ea.loadImageByName(this.subfolder, fileName), null, DecodeType.CODE_11);
            reader.readBarCodes().forEach(function (result, i, results)
            {
                console.log("Code Text : " + result.getCodeTypeName());

                console.log("Code Type : " + result.getCodeText());
            });

        } catch (e)
        {
            console.log(e.getMessage());
        }
    }

    howToRecognitionCodeAllSupportedTypes()
    {
        console.log("\n---\nfunction '" + this.howToRecognitionCodeAllSupportedTypes.name + "'\n");
        try
        {
            let fileName = "example2.jpg";
            let reader = new BarCodeReader(ea.loadImageByName(this.subfolder, fileName), null, DecodeType.ALL_SUPPORTED_TYPES);
            reader.readBarCodes().forEach(function (result, i, results)
            {
                console.log("Code Text : " + result.getCodeTypeName());

                console.log("Code Type : " + result.getCodeText());
            });

        } catch (e)
        {
            console.log(e.getMessage());
        }
    }

    howToRecognitionCodeAllSupportedTypes2()
    {
        console.log("\n---\nfunction '" + this.howToRecognitionCodeAllSupportedTypes2.name + "'\n");
        try
        {
            let fileName = "example1.png";
            let reader = new BarCodeReader(ea.loadImageByName(this.subfolder, fileName), null, DecodeType.ALL_SUPPORTED_TYPES);
            reader.readBarCodes().forEach(function (result, i, results)
            {
                console.log("Code Text : " + result.getCodeTypeName());
                console.log("Code Type : " + result.getCodeText());
            });

        } catch (e)
        {
            console.log(e.getMessage());
        }
    }

    howToRecognitionSetBarCodeImage()
    {
        console.log("\n---\nfunction '" + this.howToRecognitionSetBarCodeImage.name + "'\n");
        let fileName = "code128.jpg";
        let reader = new BarCodeReader(ea.loadImageByName(this.subfolder, fileName), null, DecodeType.ALL_SUPPORTED_TYPES);
        reader.setBarCodeImage(this.subfolder + "code11.png", null);

        reader.readBarCodes().forEach(function (result, i, results)
        {
            console.log("Code Text : " + result.getCodeTypeName());

            console.log("Code Type : " + result.getCodeText());
        });

    }

    howToMacroPdf417()
    {
        console.log("\n---\nfunction '" + this.howToMacroPdf417.name + "'\n");
        let barcodeGenerator = new BarcodeGenerator(EncodeTypes.MACRO_PDF_417, null);
        barcodeGenerator.setCodeText("codeSomecode");
        barcodeGenerator.getParameters().getBarcode().getPdf417().setPdf417MacroFileID(15900);
        barcodeGenerator.getParameters().getBarcode().getPdf417().setPdf417MacroSegmentID(2);
        barcodeGenerator.getParameters().getBarcode().getPdf417().setPdf417MacroSegmentsCount(3);
        let image = barcodeGenerator.generateBarCodeImage(aspose_barcode.BarCodeImageFormat.PNG);
        let reader = new BarCodeReader((image), null, DecodeType.MACRO_PDF_417);
        reader.readBarCodes().forEach(function (result, i, results)
        {
            console.log("CodeText : " + result.getCodeText());
            console.log("getMacroPdf417FileID : " + result.getExtended().getPdf417().getMacroPdf417FileID() + "\n");
            console.log("getMacroPdf417SegmentID : " + result.getExtended().getPdf417().getMacroPdf417SegmentID() + "\n");
            console.log('getMacroPdf417SegmentsCount : ' + result.getExtended().getPdf417().getMacroPdf417SegmentsCount() + "\n");
        });

    }

    howToCustomerInformationInterpretingType1()
    {
        console.log("\n---\nfunction '" + this.howToCustomerInformationInterpretingType1.name + "'\n");
        let generator = new BarcodeGenerator(EncodeTypes.AUSTRALIA_POST, null);
        generator.setCodeText("59123456781234567");
        generator.getParameters().getBarcode().getAustralianPost().setAustralianPostEncodingTable(CustomerInformationInterpretingType.N_TABLE);
        generator.getParameters().setAutoSizeMode(AutoSizeMode.NEAREST);
        generator.getParameters().getImageWidth().setPixels(300);
        generator.getParameters().getImageHeight().setPixels(150);
        let image = generator.generateBarCodeImage(aspose_barcode.BarCodeImageFormat.PNG);
        let reader = new BarCodeReader(image, null, DecodeType.AUSTRALIA_POST);
        reader.getBarcodeSettings().getAustraliaPost().setCustomerInformationInterpretingType(CustomerInformationInterpretingType.N_TABLE);
        reader.readBarCodes().forEach(function (result, i, results)
        {
            console.log("CodeText : " + result.getCodeText());
            console.log("CodeType : " + result.getCodeTypeName());
        });
    }

    howToCustomerInformationInterpretingType2()
    {
        console.log("\n---\nfunction '" + this.howToCustomerInformationInterpretingType2.name + "'\n");
        let generator = new BarcodeGenerator(EncodeTypes.AUSTRALIA_POST, null);
        generator.setCodeText("6212345678");
        generator.getParameters().getBarcode().getAustralianPost().setAustralianPostEncodingTable(CustomerInformationInterpretingType.C_TABLE);
        generator.getParameters().setAutoSizeMode(AutoSizeMode.INTERPOLATION);
        generator.getParameters().getImageWidth().setPixels(300);
        generator.getParameters().getImageHeight().setPixels(150);
        let image = generator.generateBarCodeImage(aspose_barcode.BarCodeImageFormat.PNG);
        let reader = new BarCodeReader(image, null, DecodeType.AUSTRALIA_POST);
        reader.getBarcodeSettings().getAustraliaPost().setCustomerInformationInterpretingType(CustomerInformationInterpretingType.C_TABLE);
        reader.readBarCodes().forEach(function (result, i, results)
        {
            console.log("CodeText : " + result.getCodeText());
            console.log("CodeType : " + result.getCodeTypeName());
        });
    }
}

ea.setLicense();
let barCodeReaderExamples = new BarCodeReaderExamples();
barCodeReaderExamples.howToReadFromFile();
barCodeReaderExamples.howToReadImageBytes();
barCodeReaderExamples.howToSetQualitySettings();
barCodeReaderExamples.howToGetCodeBytes();
barCodeReaderExamples.howToRecognitionCode128();
barCodeReaderExamples.howToRecognitionCode11();
barCodeReaderExamples.howToRecognitionCodeAllSupportedTypes();
barCodeReaderExamples.howToRecognitionCodeAllSupportedTypes2();
barCodeReaderExamples.howToRecognitionSetBarCodeImage();
barCodeReaderExamples.howToMacroPdf417();
barCodeReaderExamples.howToCustomerInformationInterpretingType1();
barCodeReaderExamples.howToCustomerInformationInterpretingType2();
