import com.dynamsoft.core.EnumErrorCode;
import com.dynamsoft.core.basic_structures.FileImageTag;
import com.dynamsoft.core.basic_structures.ImageTag;
import com.dynamsoft.cvr.CaptureVisionRouter;
import com.dynamsoft.cvr.CapturedResult;
import com.dynamsoft.cvr.EnumPresetTemplate;
import com.dynamsoft.dbr.BarcodeResultItem;
import com.dynamsoft.dbr.DecodedBarcodesResult;
import com.dynamsoft.license.LicenseError;
import com.dynamsoft.license.LicenseException;
import com.dynamsoft.license.LicenseManager;

import java.util.Scanner;

public class ReadAnImage {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        try {
            int errorCode = 0;
            String errorMsg = "";

            // Initialize license.
            // You can request and extend a trial license from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=samples&package=java
            
            try {
                LicenseError licenseError = LicenseManager.initLicense("t0088bQEAAG/qZRGdlVaJe6IIJDock35DiwZX0QwgZhJQ8MOAKqreZUeAOm6mJBUWKZ6NkqLU68Crm3TH5sZKnk7EffeNIqGcpq+sjcnGkKzhV1ZRSGadRZlORQ==");
                if (licenseError.getErrorCode() != EnumErrorCode.EC_OK) {
                    errorCode = licenseError.getErrorCode();
                    errorMsg = licenseError.getErrorString();
                }
            } catch (LicenseException e) {
                errorCode = e.getErrorCode();
                errorMsg = e.getErrorString();
            }

            if (errorCode != EnumErrorCode.EC_OK) {
                System.out.println("License initialization failed: ErrorCode: " + errorCode + ", ErrorString: " + errorMsg);
                return;
            }

            CaptureVisionRouter cvRouter = new CaptureVisionRouter();
            CapturedResult[] results = cvRouter.captureMultiPages("../../Images/GeneralBarcodes.png", EnumPresetTemplate.PT_READ_BARCODES);
            if (results == null || results.length == 0) {
                System.out.println("No barcode detected.");
            } else {
                for (int index = 0; index < results.length; index++) {
                    CapturedResult result = results[index];
                    if (result.getErrorCode() == EnumErrorCode.EC_UNSUPPORTED_JSON_KEY_WARNING) {
                        System.out.println("Warning: " + result.getErrorCode() + ", " + result.getErrorString());
                    } else if (result.getErrorCode() != EnumErrorCode.EC_OK) {
                        System.out.println("Error: " + result.getErrorCode() + ", " + result.getErrorString());
                    }

                    ImageTag tag = result.getOriginalImageTag();
                    int pageNumber = tag instanceof FileImageTag ? ((FileImageTag)tag).getPageNumber() : index;

                    DecodedBarcodesResult barcodeResult = result != null ? result.getDecodedBarcodesResult() : null;
                    BarcodeResultItem[] items = barcodeResult != null ? barcodeResult.getItems() : null;
                    if (items == null || items.length == 0) {
                        System.out.println("Page-" + (pageNumber + 1) + " No barcode detected.");
                    } else {
                        System.out.println("Page-" + (pageNumber + 1) + " Decoded " + items.length + " barcodes.");
                        for (int i = 0; i < items.length; i++) {
                            BarcodeResultItem item = items[i];
                            System.out.println();
                            System.out.println("Result " + (i + 1));
                            System.out.println("Barcode Format: " + item.getFormatString());
                            System.out.println("Barcode Text: " + item.getText());
                        }
                    }
                }
            }
        } finally {
            System.out.print("Press Enter to quit...");
            scanner.nextLine();
            scanner.close();
        }
    }
}
