Usage
Launching Standalone Capture
Launching the Standalone Capture flow can be accomplished using the captureDocuments
async function of the exposed SensibillCaptureStandalone
object.
The simplest method of launching the capture flow is to run captureDocuments
with no arguments.
The captureDocuments
method returns a promise, resolving to the filepaths of the captured image(s), saved in jpeg format
import { SbCaptureError, SensibillCaptureStandalone } from 'sensibill-reactnative-bridge-sdk';
// other code..
const captureDocumentImages = async () => {
try {
// Launch capture
const capturedImageFiles: string[] = await SensibillCaptureStandalone.captureDocuments();
// Do something with the result..
} catch (e) {
console.log('Capture failed to launch!');
const typedError = e as SbCaptureError;
}
};
The captureDocuments
optionally accepts an SbCaptureConfiguration
as an argument, which allows to provide Capture feature flags, and branding customization, as explained on Customization
page.
Capture Result Usage Samples
Displaying a Captured Document
Displaying a captured document can be achieved by reading the saved jpg
file from the disk and displaying it.
For this example, we’ll use the 'react-native-fs'
library, but other libraries capable of reading from Android/iOS app storage should be able to achieve the same result.
// We'll need to read the file
import { readFile } from 'react-native-fs';
// Inside component definition...
// A state to hold the image file contents
const [imageData, setImageData] = React.useState<string | undefined>();
// A function to open the image
const openSavedImage = async (imageFilepath: string) => {
const data = await readFile(imageFilepath, 'base64');
setImageData(data)
};
// Within your view hierarchy, display the image
{imageData ? (
<Image source={{ uri: `data:image/jpeg;base64,${imageData}` }} style={styles.imageStyle} />
) : (
<Text>No Image</Text>
)}
Reading Exif Data
The Exif data attached to the captured documents can be read from the saved image files if desired.
Info
Location / GPS info will only be attached if ALL of the below are true:- SbCaptureFeatureFlags.attachLocationData
is provided and set to true- Your app independantly requested (and was granted) location permissions from the user- The device itself is able to acquire a location fix
For this example, the 'react-native-exif'
library will be used, but other libraries with similar functionality could be used instead.
// Import the functionality from the exif library
import * as Exif from 'react-native-exif';
// The type the library returns
interface LatLong {
latitude?: number;
longitude?: number;
}
// A function to read the exif data
const readExifGps = async (imageFilepath: string) => {
const gpsInfo = await Exif.getLatLong(imageFilepath);
console.log(`Recorded latitude: ${gpsInfo.latitude}`);
console.log(`Recorded longitude: ${gpsInfo.longitude}`);
};
Deleting Files Once Finished
We heavily insist that for user privacy reasons, once you’re finished processing document images you delete them from the file system.
An example on how this could be done is included below, once again using the 'react-native-fs'
library.
// Import the required function to delete a native file
import { unlink } from 'react-native-fs';
// Function to delete an image
const deleteImageFile = async (imageFilepath: string) => {
try {
await unlink(imageFilepath);
console.log(`file: ${imageFilepath} deleted`);
} catch (e) {
console.log(`failed to delete image: ${e}`);
}
}
Capture Error Cases
In the case that capture failed due to an internal error, the promise returned by SensibillCaptureStandalone.captureDocuments
will be rejected.
The type of this error will conform to SbCaptureError
((reference)[/rnbridge/docs/interfaces/SbCaptureError.html]).
The error cause can be determined via captureError.code
(see (reference)[/rnbridge/docs/enums/SbCaptureErrorCode.html] for all possible codes), while a more detailed description can be found at captureError.message
.
Usage and parsing examples:
const captureDocumentImages = async () => {
try {
// Launch capture
const capturedImageFiles: string[] = await SensibillCaptureStandalone.captureDocuments();
// Do something with the result..
} catch (e) {
const typedError = e as SbCaptureError;
if (typedError.code === SbCaptureErrorCode.CODE_FAILED_TO_LAUNCH) {
// This error can only occur on Android devices.
// It is thrown when the necessary contexts for launching a new Activity aren't present when `captureDocuments()`
// is called. This could possibly occur in edge case scenarios where the `captureDocuments()` call is made
// after the calling page has exited / finished, or other similar scenarios.
// Handle as desired given the above explained context.
console.log('(Android only) Failed to launch capture.');
console.log(`Error message: ${typedError.message}`);
} else if (typedError.code === SbCaptureErrorCode.CODE_FAILED_TO_SAVE) {
// This error can occur on either Android or iOS.
// It is thrown when the process of saving captured images to the device fails. This means that while the user
// successfully completed the capture process, the captured images aren't available for processing.
// This could possibly occur if the filesystem is full, or other similar scenarios.
// Handle as desired given the above explained context.
console.log('Failed to save images to device after capture.');
console.log(`Error message: ${typedError.message}`);
} else {
// This case should never occur, however it's still best to handle in case more errors are added in the future.
console.log('Unrecognized error encountered!');
console.log(`Error details(code:${typedError.code}, message:${typedError.message})`);
}
}
};