How to enable Gallery Scan
The SDK allows you to decode barcodes from image files. It supports several image formats, including BMP, PNG, and JPG.
1. Gallery - To utilize this feature, you must provide a base64 encoded string as input:
barkoder.scanImage(base64Image, (result) => {
// use the result
});_barkoder.setDecodingSpeed(Barkoder.DecodingSpeed.rigorous);
2. Add image picker:
yarn add react-native-image-picker
3. Import:
import { launchImageLibrary } from 'react-native-image-picker';
4. Use: Then setup a _scanImage method that will invoke the image picker and then convert that to base64 and send it to our decoder:
async function scanImagePressed() {
try {
launchImageLibrary(
{ mediaType: 'photo', includeBase64: true },
(response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.errorCode) {
console.error('Image Picker Error:', response.errorMessage);
} else if (response.assets?.[0]?.base64) {
const base64Image = response.assets[0].base64; barkoder.scanImage(base64Image, (result) => {
// use the result
});
} else {
console.error('No valid image selected');
}
}
);
} catch (error) {
console.error('Error launching image library:', error);
}
}