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.
To utilize this feature, you must provide a base64 encoded string as input:
Barkoder.scanImage({ base64: base64String });
npm install @capacitor/camera
npx cap sync
Setup _scanImage #
import { Capacitor } from '@capacitor/core';
import { Camera, CameraResultType, CameraSource } from '@capacitor/camera';
async scanImage() {
// Add NSPhotoLibraryUsageDescription and NSPhotoLibraryAddUsageDescription in Info.plist (for iOS)
try {
// Check if the platform is iOS
if (Capacitor.getPlatform() === 'ios') {
console.log('Requesting photo library permissions for iOS...');
// Request photo library permissions for iOS only
const permissions = await Camera.requestPermissions({ permissions: ['photos'] });
if (permissions.photos !== 'granted') {
console.warn('Photo library permission not granted on iOS');
return;
}
}
// Now open the gallery to pick an image (no explicit permission request needed for Android)
const image = await Camera.getPhoto({
resultType: CameraResultType.Base64, // Get the base64 string
source: CameraSource.Photos, // Open the gallery
quality: 100 // Optional: Adjust image quality
});
if (image.base64String) {
// Pass the base64 string to Barkoder's scanImage function
console.log('Image selected, passing base64 to Barkoder...');
Barkoder.scanImage({ base64: image.base64String });
} else {
console.warn('No image selected');
}
} catch (error) {
console.error('Error selecting image:', error);
}
}