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. 

                npm install @nativescript/imagepicker

import { ImagePicker } from '@nativescript/imagepicker';
import { knownFolders, path, File } from '@nativescript/core';
            

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

                pickImageAndScan() {
        // Create an instance of the Image Picker
        const context = ImagePicker.create({
            mode: 'single' // use "multiple" for multiple selection
        });

        // Start the Image Picker
        context
            .authorize()
            .then(() => context.present())
            .then((selection) => {
                if (selection.length > 0) {
                    // Get the selected image
                    const selectedImage = selection[0];
                    
                    // Convert the selected image to Base64
                    return this.convertImageToBase64(selectedImage);
                } else {
                    console.log("No image selected.");
                }
            })
            .then((base64Image) => {
                if (base64Image) {
                    this._base64Image = base64Image;

                    // Now call the scanImage method using the base64 image
                    if (this.barkoderView && this._base64Image) {
                        this.barkoderView.scanImage(this._base64Image, this);
                    } else {
                        console.error("barkoderView or base64Image not available.");
                    }
                }
            })
            .catch((e) => {
                console.error("Error picking image: ", e);
            });
    }

    convertImageToBase64(imageAsset): Promise<string | null> {
        return new Promise((resolve, reject) => {
            try {
                imageAsset.getImageAsync((nativeImage) => {
                    if (nativeImage) {
                        let base64String: string;

                        if (global.isIOS) {
                            const data = NSData.dataWithData(UIImagePNGRepresentation(nativeImage));
                            base64String = data.base64EncodedStringWithOptions(0);
                        } else if (global.isAndroid) {
                            const bitmap = nativeImage;
                            const outputStream = new java.io.ByteArrayOutputStream();
                            bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, outputStream);
                            const byteArray = outputStream.toByteArray();
                            base64String = android.util.Base64.encodeToString(byteArray, android.util.Base64.NO_WRAP);
                        } else {
                            resolve(null);
                            return;
                        }

                        resolve(`data:image/png;base64,${base64String}`);
                    } else {
                        reject("Failed to get native image.");
                    }
                });
            } catch (error) {
                reject(error);
            }
        });
    }
            

History:

close