// Copyright (C) 2003-2026, Foxit Software Inc..
// All Rights Reserved.
//
// http://www.foxitsoftware.com
//
// The following code is copyrighted and contains proprietary information and trade secrets of Foxit Software Inc..
// You cannot distribute any part of Foxit PDF SDK to any third party or general public,
// unless there is a separate license agreement with Foxit Software Inc. which explicitly grants you such rights.
//
// This file contains an example to demonstrate how to use Foxit PDF SDK to generate barcode.

import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.common.Image;
import com.foxit.sdk.common.Barcode;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import static com.foxit.sdk.common.Constants.e_ErrSuccess;

public class barcode {
	static String key = "8f3g1sFttW8NAgfSNv+BvQJm2vDtTY+wGQ/1PcmGtqKTwvvI8mLE4KvuHocWH+XZnP3U+Vh1BnqpgQIjO0jwRz7evDdlijTdutpBEsozaXEvFpLbixmsSGvtznflVsuEmsu5ytLwVJiTap2VcCiiSbyuvQwQu88I8BxJ+XXv3LPXN5v/+00zDrZ+V5yLefi+bk7JX5FRE2jfSnOJIqEaVkHOpCIjwf9aszl01YoY7x0EAehg0ILal+v2QLLo7HnkAVt/TA31rddZexhq/2hwAwfy4OfvkJPnESppD7D+SAsX5CfXoU55P1vmyRe+lKBM6pOcarUbuaN4Vjj62SD0sTDbJZ2Ypyp+kcIxZ9wcD+Mg8aSimr5+hg8wZJT0u0mIAdZhTJ++A9sq9OmVAfL2QQ6bh2ISyKPGJPv29u738bAokRjqzZy9bi30mYfGLEwKChEd2R2NBDKZTDOVxFwpzD1IbF0/wN/DDbMO2gS0xUvgSbJyuXfA3ALtmoUH9I9iScaBVon5oYRRy6OTnFMgDgVM+nFTzWbaGKZfO+PMBnreaQ/Ezr9wH7YKJ+LrM4r6xVQjcHDUyx4dlVLe3zQlDRKGGChhyvymVDBuZR8B0/M9Y/DKr1sQAzp/W1Wsz+LZb+JUIxLBrJzgRLtOliuLF+JOTy036W0shWcXYYc2bwLdPWP/BXygmnopzUTCtuo/QLVTRNdENFqciYYvSHEZxgV1sUFpuBqIMUEigWR/2gMlMDiTVq0udEqn8g9bkw1SImVy50v2gwEbUHkEa243BP/3/Dae0YhH03NDUlTSVBKX6w1UVQK6/7aDL3A4EEg47YmKgh6NsABzRsgSvLBhBQNqzOoUvnKaf7YMlw0ns06BdFswP6DaY1KmuRR2Xn6MtDTshGNtT9+azHPqaAWaZs1rHz3fGYb5pfJHptJlx+r9609bY+ZhCERLYG+0lTNWCFnDxdI8Vj1BG4iGWaDBkIC5qdJI3qDLVDABdtIk43fMhgfr+TUqds+G6KBDapS9p32nKZc6lBqvlNN12Pv1AJCC1rwfECzmNQi3MXogQCxzi5pHAgd/BGyGwo4RWhFrKFN869H0whvDVC8NJM2upFrEEJET4fecQyMLWHuTCWvjRZclLC/GGOj9PAXHOcEdFkq4UkloMulXLGsQzkzHB3Elvd2NnxZ2+3FK2e0xNx+rBMru/i/HHLG6WZvz6Jz7mjmibNfBExydtEI5lWUZ1Wsy0r17kyvlME9Bot7XXPkx/+cKTcxfNWSb3kaPROYIgSpGjrXxfrphBnr1mioJPq3WrtP9E5lZBeMd8jxXT4NlX3T0frsci6Xy1ChB/BytU7pvn6bMfddTnEDd/QpkW+6WYOnHHrIYVk2ngKjUBl5nGOkp37wpgUpWp5tcICjpbjfNdY43VfTyHRf4dGRR";
	static String sn = "nkyvo37UxrKdu5Peq294Eb+g2ihRldgPQEP6MFtAM4LwtxXXJ+T7MQ==";
	static String output_path = "../output_files/barcode/";

    // You can also use System.load("filename") instead. The filename argument must be an absolute path name.
    static {
        String os = System.getProperty("os.name").toLowerCase();
        String lib = "fsdk_java_";
        if (os.startsWith("win")) {
            lib += "win";
        } else if (os.startsWith("mac")) {
            lib += "mac";
        } else {
            lib += "linux";
      	}
        
        if (System.getProperty("sun.arch.data.model").equals("64")) {
          if(System.getProperty("os.arch").equals("aarch64")){
            if(os.startsWith("mac")){
              lib += "arm";
             }
             else{
               lib += "armv8";
             }
           }
           else{
              lib += "64";
           }
        } else {
           if(System.getProperty("os.arch").equals("arm")){
              lib += "armv7";
           }
           else{
              lib += "32";
           }
        }
        System.loadLibrary(lib);
    }

	private static void createResultFolder(String output_path) {
		File myPath = new File(output_path);
		if (!myPath.exists()) {
			myPath.mkdir();
		}
	}

	private static void Save2Image(Bitmap bitmap, String filepath) throws PDFException {
		// Add the bitmap to image and save the image.
		Image image = new Image();
		image.addFrame(bitmap);
		String save_name = output_path + filepath;
		image.saveAs(save_name);
	}
	
	// Generate barcode in different type and save each of them as an image file.
	public static void Barcode_GenerateEachType(String codeStr, int codeFormat, int unitWidth, int unitHeight, int qrLevel, String bmpName) {
		try {
			Barcode barcode = new Barcode();
			Bitmap bitmap = barcode.generateBitmap(codeStr, codeFormat, unitWidth, unitHeight, qrLevel);
			Save2Image(bitmap, bmpName);
		} catch (Exception e) {
		  e.printStackTrace();
		}
	}

	// Generate all types of barcode and save each of them as an image file.
	public static void Barcode_GenerateAll() {
		// Strings used as barcode content.
		String[] codeStr = {"TEST-SHEET", "102030405060708090", "80674313", "890444000335", "9780804816632", "070429", "Unknown - change me!", "TestForBarcodeQrCode"};
		// Barcode format types
		int[] codeFormat = {Barcode.e_FormatCode39, Barcode.e_FormatCode128,
			Barcode.e_FormatEAN8, Barcode.e_FormatUPCA, 
			Barcode.e_FormatEAN13, Barcode.e_FormatITF, 
			Barcode.e_FormatPDF417, Barcode.e_FormatQRCode};

		// Image names for the saved image files.(except QR code)
		String[] bmpName = {"/code39_TEST-SHEET.bmp", "/CODE_128_102030405060708090.bmp",
			"/EAN_8_80674313.bmp", "/UPC_A_890444000335.bmp", "/EAN_13_9780804816632.bmp",
			"/ITF_070429.bmp", "/PDF_417_Unknown.bmp"};

		//Format error correction level of QR code.
		int[] qrLevel = {Barcode.e_QRCorrectionLevelLow, Barcode.e_QRCorrectionLevelMedium, 
			Barcode.e_QRCorrectionLevelQuater, Barcode.e_QRCorrectionLevelHigh};
		//Image names for the saved image files for QR code.
		String[] bmpQrName = {"/QR_CODE_TestForBarcodeQrCode_L.bmp", "/QR_CODE_TestForBarcodeQrCode_M.bmp",
			"/QR_CODE_TestForBarcodeQrCode_Q.bmp", "/QR_CODE_TestForBarcodeQrCode_H.bmp"};

		//Unit width for barcode in pixels, preferred value is 1-5 pixels.
		int unitWidth = 2;
		//Unit height for barcode in pixels, preferred value is >= 20 pixels.
		int unitHeight = 120;

		// Generate barcode for different types.
		for (int i = 0; i < 8; i++) {
			if (codeFormat[i] != Barcode.e_FormatQRCode)  // Not QR code
				Barcode_GenerateEachType(codeStr[i], codeFormat[i], unitWidth, unitHeight, 0, bmpName[i]);
			else {
				// QR code
				//Generate for each format error correction level.
				for (int j = 0; j < 4; j++)
					Barcode_GenerateEachType(codeStr[i], codeFormat[i], unitWidth, unitHeight, qrLevel[j], bmpQrName[j]);
			}
		}
	}
		
	public static void main(String[] args) throws PDFException, IOException {
		createResultFolder(output_path);
		// Initialize library.
		int error_code = Library.initialize(sn, key);
		if (error_code != e_ErrSuccess) {
			System.out.println(String.format("Library Initialize Error: %d\n", error_code));
			return;
		}

		try {
			// Generate all types of barcode
			Barcode_GenerateAll();
			System.out.println("Barcode demo.");
		} catch (Exception e) {
			e.printStackTrace();
		}
		Library.release();
		return;
	}

}
