NativeScript Gallery scan

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

Shell
npm install @nativescript/imagepicker

At the top of the view model (app/main-view-model.ts):

TypeScript
import { create } from '@nativescript/imagepicker';
import { isAndroid, isIOS } 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. The two methods go inside the view model class (the one with barkoderView, as in the General example), whose scanningFinished gets the results; a button on the page calls the first: <Button text="Scan a picture" tap="{{ pickImageAndScan }}" />

TypeScript
pickImageAndScan() {
        // Create an instance of the Image Picker
        const context = create({
            mode: 'single', // use "multiple" for multiple selection
            android: { use_photo_picker: true } // Android 13+: the system photo picker (no storage permission)
        });

        // Start the Image Picker
        context
            .authorize()
            .then(() => context.present())
            .then((selection) => {
                if (selection.length > 0) {
                    // Get the selected image
                    const selectedImage = selection[0].asset;
                    
                    // Convert the selected image to Base64
                    return this.convertImageToBase64(selectedImage);
                } else {
                    console.log("No image selected.");
                }
            })
            .then((base64Image) => {
                if (base64Image) {
                    // Now call the scanImage method using the base64 image
                    if (this.barkoderView) {
                        this.barkoderView.scanImage(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 (isIOS) {
                            const data = NSData.dataWithData(UIImagePNGRepresentation(nativeImage));
                            base64String = data.base64EncodedStringWithOptions(0 as NSDataBase64EncodingOptions);
                        } else if (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(base64String); // scanImage takes the base64 string itself, without a "data:" prefix
                    } else {
                        reject("Failed to get native image.");
                    }
                });
            } catch (error) {
                reject(error);
            }
        });
    }