1. React Native
  2. VisionCore Module

React Native

VisionCore Module

The VisionCore module provides camera-independent functionality for the React Native Vision SDK. It allows you to perform OCR predictions, model management, and data logging without requiring the camera component.


Import and Setup

        import { VisionCore } from 'react-native-vision-sdk';

      

Environment Management

VisionCore.setEnvironment(environment)

Sets the environment for all VisionCore operations. This should be called once during app initialization.

Parameters

  • environment (required): Environment string - 'staging' | 'dev' | 'sandbox' | 'qa' | 'production'

Example

        import { VisionCore } from 'react-native-vision-sdk';

// Set environment during app initialization
VisionCore.setEnvironment('production');

// For development/testing
VisionCore.setEnvironment('staging');

      
NOTE

Call setEnvironment() once at app startup before using any other VisionCore methods.


Model Management

WARNING

DEPRECATED: The loadModel(), unLoadModel(), and predict() methods shown below are deprecated and will be removed in v3.0.0. Please use the new Model Management API for better control and features.

NEW in v2.0.2: VisionCore now includes a comprehensive Model Management API with 14 new methods for fine-grained control over on-device models. See the Model Management documentation for complete details.

Quick Comparison

Old API (Deprecated):

        await VisionCore.loadModel(token, apiKey, 'shipping_label', 'large');
const result = await VisionCore.predict(imageUri, barcodes);

      

New API (Recommended):

        const module = { type: 'shipping_label', size: 'large' };
await VisionCore.downloadModel(module, apiKey, token);
await VisionCore.loadOCRModel(module, apiKey, token);
const result = await VisionCore.predictWithModule(module, imageUri, barcodes);

      

Deprecated: VisionCore.loadModel(token, apiKey, modelType, modelSize)

DEPRECATED - Use downloadModel() + loadOCRModel() instead.

Loads on-device models without requiring the camera view. This method downloads and prepares models for offline OCR processing.

Parameters

  • token (optional): Authentication token (string | null)
  • apiKey (optional): API key (string | null)
  • modelType (required): Model type (string)
    • 'shipping_label'
    • 'item_label'
    • 'bill_of_lading'
    • 'document_classification'
  • modelSize (required): Model size (string)
    • 'nano' - Smallest, fastest
    • 'micro' - Small and fast
    • 'small' - Balanced
    • 'medium' - Higher accuracy
    • 'large' - Best accuracy
    • 'xlarge' - Maximum accuracy

Returns

  • Promise<void> - Resolves when model loading starts

Example

        const loadShippingLabelModel = async () => {
    try {
        await VisionCore.loadModel(
            null,                    // token (optional)
            'your_api_key',         // apiKey
            'shipping_label',       // modelType
            'large'                 // modelSize
        );
        console.log('Model loading started');
    } catch (error) {
        console.error('Failed to load model:', error);
    }
};

      

Deprecated: VisionCore.unLoadModel(modelType, shouldDeleteFromDisk)

DEPRECATED - Use unloadModel() and/or deleteModel() instead.

Unloads on-device models to free up memory and disk space when they're no longer needed.

Parameters

  • modelType (required): Model type to unload (string | null)
    • 'shipping_label' - Unload specific model
    • 'item_label' - Unload specific model
    • 'bill_of_lading' - Unload specific model
    • 'document_classification' - Unload specific model
    • null - Unload all models
  • shouldDeleteFromDisk (optional): Delete model files from disk (boolean, default: false)
    • true - Removes model files from disk permanently
    • false - Keeps files for faster reloading

Returns

  • Promise<string> - Success message

Example

        // Unload a specific model
const unloadShippingLabel = async () => {
    try {
        const result = await VisionCore.unLoadModel(
            'shipping_label',  // Model type to unload
            true               // Delete from disk
        );
        console.log(result); // "Model unloaded successfully"
    } catch (error) {
        console.error('Failed to unload model:', error);
    }
};

// Unload all models
const unloadAllModels = async () => {
    try {
        const result = await VisionCore.unLoadModel(
            null,  // Pass null to unload all models
            true   // Delete from disk
        );
        console.log(result); // "All models unloaded successfully"
    } catch (error) {
        console.error('Failed to unload models:', error);
    }
};

      

Use Cases

  • Free up memory when switching between different model types
  • Clean up disk space after processing
  • Prepare for app updates or model version changes
  • Optimize app performance by removing unused models
WARNING

Platform Differences

  • iOS: Supports unloading specific models individually
  • Android: Due to singleton pattern, unloading a specific model will destroy the entire singleton instance, effectively unloading all models

Deprecated: VisionCore.onModelDownloadProgress(callback)

DEPRECATED - Use the progressCallback parameter in downloadModel() instead.

Subscribes to model download progress updates. Returns a subscription that can be removed.

Parameters

  • callback (required): Function that receives progress updates
    • progress (number): Download progress (0.0 to 1.0)
    • downloadStatus (boolean): Whether model is downloaded
    • isReady (boolean): Whether model is ready for use

Returns

  • Subscription - Event subscription with .remove() method

Example

        import { useEffect, useState } from 'react';

const ModelProgressExample = () => {
    const [progress, setProgress] = useState(0);
    const [isReady, setIsReady] = useState(false);

    useEffect(() => {
        const subscription = VisionCore.onModelDownloadProgress((progress, downloadStatus, isReady) => {
            setProgress(progress * 100); // Convert to percentage
            setIsReady(isReady);

            if (isReady) {
                console.log('Model is ready for predictions!');
            }
        });

        // Load model
        VisionCore.loadModel(null, 'your_api_key', 'shipping_label', 'large');

        // Cleanup subscription
        return () => subscription.remove();
    }, []);

    return (
        <View>
            <Text>Download Progress: {progress.toFixed(0)}%</Text>
            <Text>Model Ready: {isReady ? 'Yes' : 'No'}</Text>
        </View>
    );
};

      

Prediction Methods

On-Device Prediction

Deprecated: VisionCore.predict(imagePath, barcodes)

DEPRECATED - Use predictWithModule() instead for explicit model selection.

Performs on-device OCR prediction using the loaded model.

Parameters

  • imagePath (required): Path to image file (string)
  • barcodes (optional): Array of detected barcodes (string[], default: [])

Returns

  • Promise<any> - OCR prediction result

Example

        const performOnDevicePrediction = async () => {
    try {
        const imageUri = 'file:///path/to/image.jpg';
        const barcodes = ['1234567890']; // Optional

        const result = await VisionCore.predict(imageUri, barcodes);
        console.log('Prediction result:', result);
    } catch (error) {
        console.error('Prediction failed:', error);
    }
};

      

Cloud Prediction Methods

VisionCore.predictShippingLabelCloud(imagePath, barcodes, options)

Cloud-based shipping label prediction.

Parameters

  • imagePath (required): Image file path (string)
  • barcodes (optional): Barcode array (string[], default: [])
  • options (optional): Configuration object
    • token?: string | null
    • apiKey?: string | null
    • locationId?: string | null
    • options?: {[key: string]: any} | null
    • metadata?: {[key: string]: any} | null
    • recipient?: {[key: string]: any} | null
    • sender?: {[key: string]: any} | null
    • shouldResizeImage?: boolean

Example

        const options = {
    apiKey: 'your_api_key',
    metadata: { orderId: '12345' },
    shouldResizeImage: true
};

const result = await VisionCore.predictShippingLabelCloud(imageUri, [], options);

      

VisionCore.predictItemLabelCloud(imagePath, options)

Cloud-based item label prediction.

Parameters

  • imagePath (required): Image file path (string)
  • options (optional): Configuration object
    • token?: string | null
    • apiKey?: string | null
    • shouldResizeImage?: boolean

VisionCore.predictBillOfLadingCloud(imagePath, barcodes, options)

Cloud-based bill of lading prediction.

Parameters

  • imagePath (required): Image file path (string)
  • barcodes (optional): Barcode array (string[], default: [])
  • options (optional): Configuration object
    • token?: string | null
    • apiKey?: string | null
    • locationId?: string | null
    • options?: {[key: string]: any} | null
    • shouldResizeImage?: boolean

VisionCore.predictDocumentClassificationCloud(imagePath, options)

Cloud-based document classification.

Parameters

  • imagePath (required): Image file path (string)
  • options (optional): Configuration object
    • token?: string | null
    • apiKey?: string | null
    • shouldResizeImage?: boolean

VisionCore.predictWithCloudTransformations(imagePath, barcodes, options)

Hybrid on-device + cloud prediction.

Parameters

  • imagePath (required): Image file path (string)
  • barcodes (optional): Barcode array (string[], default: [])
  • options (optional): Configuration object (same as shipping label options)

Logging Methods

VisionCore.logItemLabelDataToPx(imageUri, barcodes, responseData, token, apiKey, shouldResizeImage, metadata)

Logs item label data to PackageX Vision for analytics.

Parameters

  • imageUri (required): Image file path (string)
  • barcodes (required): Barcode array (string[])
  • responseData (required): OCR response data (any)
  • token (optional): Auth token (string | null)
  • apiKey (optional): API key (string | null)
  • shouldResizeImage (optional): Resize image (boolean, default: true)
  • metadata (optional): Custom metadata ({[key: string]: any} | null, default: {})

Returns

  • Promise<any> - Logging result

VisionCore.logShippingLabelDataToPx(imageUri, barcodes, responseData, token, apiKey, locationId, options, metadata, recipient, sender, shouldResizeImage)

Logs shipping label data to PackageX Vision for analytics.

Parameters

  • imageUri (required): Image file path (string)
  • barcodes (required): Barcode array (string[])
  • responseData (required): OCR response data (any)
  • token (optional): Auth token (string | null)
  • apiKey (optional): API key (string | null)
  • locationId (optional): Location ID (string | null)
  • options (optional): Additional options ({[key: string]: any} | null)
  • metadata (optional): Custom metadata ({[key: string]: any} | null)
  • recipient (optional): Recipient info ({[key: string]: any} | null)
  • sender (optional): Sender info ({[key: string]: any} | null)
  • shouldResizeImage (optional): Resize image (boolean, default: true)

Returns

  • Promise<any> - Logging result

Complete Setup Example

        import React, { useEffect, useState } from 'react';
import { View, Text, TouchableOpacity, Alert } from 'react-native';
import { VisionCore } from 'react-native-vision-sdk';

const VisionCoreExample = () => {
    const [isModelReady, setIsModelReady] = useState(false);
    const [progress, setProgress] = useState(0);

    useEffect(() => {
        // 1. Set environment
        VisionCore.setEnvironment('production');

        // 2. Monitor model download progress
        const subscription = VisionCore.onModelDownloadProgress((progress, downloadStatus, isReady) => {
            setProgress(progress * 100);
            setIsModelReady(isReady);
        });

        // 3. Load model
        loadModel();

        return () => subscription.remove();
    }, []);

    const loadModel = async () => {
        try {
            await VisionCore.loadModel(
                null,
                'your_api_key',
                'shipping_label',
                'large'
            );
        } catch (error) {
            console.error('Failed to load model:', error);
        }
    };

    const performPrediction = async () => {
        if (!isModelReady) {
            Alert.alert('Model Not Ready', 'Please wait for model to finish loading');
            return;
        }

        try {
            // Example with on-device prediction
            const result = await VisionCore.predict('file:///path/to/image.jpg', []);
            Alert.alert('Success', JSON.stringify(result, null, 2));
        } catch (error) {
            Alert.alert('Error', error.message);
        }
    };

    return (
        <View style={{ padding: 20 }}>
            <Text>Model Progress: {progress.toFixed(0)}%</Text>
            <Text>Model Ready: {isModelReady ? 'Yes' : 'No'}</Text>

            <TouchableOpacity
                onPress={performPrediction}
                disabled={!isModelReady}
                style={{
                    backgroundColor: isModelReady ? '#007AFF' : '#ccc',
                    padding: 15,
                    borderRadius: 8,
                    alignItems: 'center',
                    marginTop: 20
                }}
            >
                <Text style={{ color: 'white' }}>
                    {isModelReady ? 'Predict' : 'Loading Model...'}
                </Text>
            </TouchableOpacity>
        </View>
    );
};

export default VisionCoreExample;

      

NOTE

VisionCore methods are completely independent of the camera component and can be used in any React Native app without requiring camera permissions or the VisionSdkView component.