Create an ImageRecognitionsFragment to handle image capture, gallery selection, and food item recognition.
Step 1: Select or Capture Image
For selecting or capturing an image, use PhotoPicker and PhotoCapture functions in ImageRecognitionsFragment. Refer to Android's official documentation for detailed instructions:
The fetchResult() method sends the selected image to the Passio SDK for recognition.
private fun fetchResult() {
if (bitmap == null) {
Toast.makeText(requireContext(), "Please select an image", Toast.LENGTH_SHORT).show()
return
}
binding.loader.isVisible = true
PassioSDK.instance.recognizeImageRemote(bitmap!!) { result ->
binding.loader.isVisible = false
val adapter = ImageRecognitionsAdapter(result) { info -> onShowDetails(info) }
binding.list.adapter = adapter
}
}
PassioSDK.instance.recognizeImageRemote
The recognizeImageRemote function takes an image in Bitmap format, analyzes it remotely using Passio's servers, and returns a list of food items recognized within the image. This function is asynchronous and utilizes a callback to return the result once the analysis is complete.
Output
The function returns a list of PassioAdvisorFoodInfo objects through a callback
The structure of PassioAdvisorFoodInfo is as follows:
recognisedName (String): The name of the recognized food item.
portionSize (String): Suggested portion size for the recognized food.
weightGrams (Double): Estimated weight of the food item in grams.
foodDataInfo (PassioFoodDataInfo?): An optional field containing more detailed nutritional information if available.
packagedFoodItem (PassioFoodItem?): An optional field containing packaged food item data if the recognized item is a packaged product.
resultType (PassioFoodResultType): The type of result, indicating if the food is freshly prepared, packaged, or a general item.
Step 3: Display Food Details
Navigate to a details screen upon item selection.
private fun onShowDetails(passioAdvisorFoodInfo: PassioAdvisorFoodInfo) {
if (passioAdvisorFoodInfo.foodDataInfo != null) {
PassioSDK.instance.fetchFoodItemForDataInfo(passioAdvisorFoodInfo.foodDataInfo!!, passioAdvisorFoodInfo.foodDataInfo!!.nutritionPreview.servingQuantity, passioAdvisorFoodInfo.foodDataInfo!!.nutritionPreview.servingUnit){ passioFoodItem->
passioFoodItem?.let {
navigateToDetails(it)
}
}
}
else if (passioAdvisorFoodInfo.packagedFoodItem != null) {
navigateToDetails(passioAdvisorFoodInfo.packagedFoodItem!!)
}
}
private fun navigateToDetails(foodItem: PassioFoodItem) {
FoodDetailsFragment.setPassioFoodItem(foodItem)
findNavController().navigate(ImageRecognitionsFragmentDirections.imageRecognitionsToFoodDetails())
}
PassioSDK.instance.fetchFoodItemForDataInfo
The fetchFoodItemForDataInfo function retrieves detailed food information from Passio's database based on provided data fields. This function is also asynchronous and accepts a callback that returns a PassioFoodItem object with the full nutritional profile of the requested food item.
Input Parameters
foodDataInfo (PassioFoodDataInfo): A data object containing metadata about the food item, including its ID and a preview of its nutrition details.
servingQuantity (Double): The quantity or portion size of the food.
servingUnit (String): The unit of measurement for the serving size, such as grams, ounces, or pieces.
Output
The function returns a PassioFoodItem through the callback. This object contains the full nutritional breakdown of the food item, which can include: