Cordova 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:

TypeScript
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. Add the method to your component, next to the General example's code (it uses its Barkoder and BarkoderResult, after the scanner is initialized), and call it from a button: <button (click)="scanImage()">Scan a picture</button>. The file picker opens only from a tap. The callback gets the raw result: new BarkoderResult(data) has the barcodes in decoderResults.

TypeScript
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();
  }