How to enable Gallery Scan

The SDK allows you to decode barcodes from image files. It supports several image formats, including BMP, PNG, and JPG.

To utilize this feature, you must provide a base64 encoded string as input:

                Barkoder.scanImage(base64string, (data: any) => {
              const barkoderResult = new BarkoderResult(data);
              if (barkoderResult) {
                // handle result
              }
          }, (err: any) => {
            console.error("Error scanning image:", err);
          });
            

Here is an example of how you can access the photo library on your device and select and scan images from within the library

                scanImage() {
    // Create an input element to open the photo picker
    const input = document.createElement('input');
    input.type = 'file';
    input.accept = 'image/*';

    input.onchange = (event: any) => {
      const file = event.target.files[0];
      if (file) {
        const reader = new FileReader();

        reader.onload = () => {
          const base64string = (reader.result as string).split(',')[1]; // Extract Base64 part

          Barkoder.scanImage(base64string, (data: any) => {
              const barkoderResult = new BarkoderResult(data);
              if (barkoderResult) {
                // handle result
              }
            }, (err: any) => {
              console.error("Error scanning image:", err);
            });
        };

        reader.onerror = () => {
          console.error("Error reading file");
        };

        // Start reading the file as Base64
        reader.readAsDataURL(file);
      } else {
        console.error("No file selected");
      }
    };

    // Trigger the photo picker dialog
    input.click();
  }
            

History:

close