import {
PassioSDK,
DetectionCameraView,
} from '@passiolife/nutritionai-react-native-sdk-v2';<DetectionCameraView style={{flex: 1, width: '100%'}} />const config: FoodDetectionConfig = {
/**
* Detect barcodes on packaged food products. Results will be returned
* as `BarcodeCandidates` in the `FoodCandidates` property of `FoodDetectionEvent`
*/
detectBarcodes: true,
/**
* Results will be returned as DetectedCandidate in the `FoodCandidates`and
* property of `FoodDetectionEvent`
*/
detectPackagedFood: true,
/**
* Detect barcodes on packaged food products. Results will be returned
* under the `nutritionFacts` property on `FoodDetectionEvent`.
*/
detectNutritionFacts: true,
};
useEffect(() => {
if (!isReady) {
return;
}
const subscription = PassioSDK.startFoodDetection(
config,
async (detection: FoodDetectionEvent) => {
const { candidates, nutritionFacts } = detection
if (candidates?.barcodeCandidates?.length) {
// show barcode candidates to the user
} else if (candidates?.packagedFoodCode?.length) {
// show OCR candidates to the user
} else if (candidates?.detectedCandidates?.length) {
// show visually recognized candidates to the user
} else if (nutritionFacts) {
// Show scanned nutrition facts to the user
}
},
);
// stop food detection when component unmounts
return () => subscription.remove();
}, [isReady]);
export interface FoodDetectionEvent {
/**
* A collection of food candidates detected by the models.
*/
candidates?: FoodCandidates;
/**
* Detected nutrition facts when scanning a nutrition label.
*/
nutritionFacts?: NutritionFacts;
}/**
* A collection of food candidates detected by the models.
*/
export interface FoodCandidates {
/**
* Food candidate results from visual scanning. The array is sorted by confidence, with the most confident result at index 0.
*/
detectedCandidates: DetectedCandidate[]
/**
* Food candidate results from barcode scanning.
*/
barcodeCandidates?: BarcodeCandidate[]
/**
* Food candidate results from packagedFoodCode scanning.
*/
packagedFoodCode?: PackagedFoodCode[]
}/**
* Nutrition facts scanned from the nutrition label on a package food item
*/
export interface NutritionFacts {
servingSizeQuantity?: number
servingSizeUnitName?: string
servingSizeGram?: number
servingSizeUnit: ServingSizeUnit
calories?: number
fat?: number
carbs?: number
protein?: number
saturatedFat?: number
transFat?: number
cholesterol?: number
sodium?: number
dietaryFiber?: number
sugars?: number
sugarAlcohol?: number
}//npm.pkg.github.com/:_authToken=GITHUB_ACCESS_TOKEN
@passiolife:registry=https://npm.pkg.github.comnpm install @passiolife/nutritionai-react-native-sdk-v2 yarn add @passiolife/nutritionai-react-native-sdk-v2cd ios; pod installconst [isReady, setIsReady] = useState(false);
// Effect to configure the SDK and request camera permission
useEffect(() => {
Promise.all([
PassioSDK.configure({
key: 'your-developer-key',
autoUpdate: true,
}),
PassioSDK.requestCameraAuthorization(),
]).then(([sdkStatus, cameraAuthorized]) => {
console.log(
`SDK configured: ${sdkStatus.mode} Camera authorized: ${cameraAuthorized}`,
);
setIsReady(sdkStatus.mode === 'isReadyForDetection' && cameraAuthorized);
});
}, []);
platform :ios, '13.0'dependencies {
// Add this line below for Passio SDK library
implementation files("$rootDir/../node_modules/@passiolife/nutritionai-react-native-sdk-v2/android/libs/passiolib-release.aar")
...
}buildscript {
ext {
...
minSdkVersion = 26
...
}
}