Flutter: Get information from scanned barcode number - flutter

I am quite new to Flutter, and I am currently working on an application that needs barcode reading. So I used the barcode_scan library, and I am now able to get the barcode number from a scanned barcode.
To be clear, I am not asking how to scan a barcode / get the barcode number.
My question is: how can I get more information about a product from its barcode number in Flutter (e.g. product name)? Can I do this from the barcode_scan library, or will I need something else?
Edit: Forgot to mention, when scanning QR codes I am able to get the information it encodes (e.g. product name or URL), but this is not the case with barcodes.
My current (relevant) code is as follows:
String result = 'Hey there!';
Future scanBarcode() async {
if(await Permissions.checkCameraPermission()) {
try {
await BarcodeScanner.scan().then((scan_result) {
//here scan_result is the obtained barcode number
setState(() { result = scan_result; });
});
}
//handling exceptions...
}
else {
setState(() { result = 'Camera permission denied'; });
}
}
(Side note, here Permissions.checkCameraPermission() is a function I created to check if camera permission is granted, shouldn't be relevant for the question).

I'm using the same plugin for scanning barcode or qr code for my flutter app..
String result = 'Hey there!';
Future scanBarcode() async {
if(await Permissions.checkCameraPermission()) {
try {
// I get the scan result from BarcodeScanner this way and it is working for me.
result = await BarcodeScanner.scan();
}
//handling exceptions...
}
else {
setState(() { result = 'Camera permission denied'; });
}
}

Well, it seems the only plausible way to do it from Flutter would be to use a barcode lookup API like #RichardHeap proposed in the comments. If my app was Flutter only (without a backend server connected to it) I believe I would have gone that way.
But I guess I'll just send the barcode number itself to the backend and deal with it on the server-side.

What I understand is, on scanning the code, you need the information / data about the product.
If I understood it right, there are several aggregators of the barcode. You can get APIs from them. But there is not single place from where you get all the information.

Related

Saving the input image from Face detection as a file? [Flutter + Google ML Kit Face Detection]

is it possible to save the processed image as a File?
Here is what I'm trying to do, our app have a KYC (Know your customer) and we implemented the
face detection to make the users do several poses. What I want is to save them as an image file and upload it on the database
Example Scenario:
App ask the user to smile > The user smiled > save the image.
Here is what I have right now:
Where the app checks if the user smiled
if (faces.isNotEmpty) {
if (inputImage.inputImageData?.size != null &&
inputImage.inputImageData?.imageRotation != null) {
if (faces[0].smilingProbability! > 0.85) {
await _getImg();
}
}
}
Then I call a Function to stop image stream then take a picture (this works but on some physical device it crashes) but if I dont stop the image stream then called the takePicture() right-away it just crashes all the time.
_getImg() async {
setState(() {
globalBusy = true;
});
await _controller.stopImageStream();
var img = await _controller.takePicture();
VerificationVarHandler.livelinesImgsPaths.add(img.path);
}
As you can see it's not the best way at least for me I think, so maybe I can use the
inputImage from the _processCameraImage() because it has a byte? then I can pass that bytes to a decoder and save it locally when I trigger a function?
Or maybe better yet there is more elegant way of achieving this?
You can check out this gfg article - https://www.geeksforgeeks.org/face-detection-in-flutter-using-firebase-ml-kit/
Learn flutter in easiest way - https://auth.geeksforgeeks.org/user/ms471841/articles

how to get country code like(+91 ,+1 etc) using user's current location flutter

I'm working on flutter app and I have a text widget where I want to show user's current country code based on his location without any dropdown.
I'm getting value like "IN","US" using Locale, but I don't want it,
I need numeric country code like +91 (for India), +1 (for US) etc.
anyone help me please.
You can use these files countries.dart and country.dart
Get the user country code from http://ip-api.com/json with http package and then use it to get the country phone code from countryList:
Future<String> getCountryPhoneCode() async {
var response = await http.get(Uri.parse('http://ip-api.com/json'));
var jsonResponse = json.decode(response.body);
final isoCode = jsonResponse['countryCode'];
print("country code " + isoCode);
return "+" +
countryList
.firstWhere((element) => element.isoCode == isoCode,
orElse: () => countryList.first)
.phoneCode;
}
I tested this and it works for me but you can improve it to avoid http exceptions
You can use country_codes package:
Install:
dependencies:
country_codes: ^2.0.1
Usage:
await CountryCodes.init(); // Optionally, you may provide a `Locale` to get countrie's localizadName
final CountryDetails details = CountryCodes.detailsForLocale();
print(details.alpha2Code); // Displays alpha2Code, for example US.
print(details.dialCode); // Displays the dial code, for example +1.
print(details.name); // Displays the extended name, for example United States.
print(details.localizedName);

How to use StreamBuilder in FirebaseStorage to inform my App from changes in my Firebase Storage? In Flutter

Here is my challenge:
I'm using the following example from https://firebase.flutter.dev/docs/storage/usage/
to upload images to my FireBaseStorage from my phone gallery (which delivers me the String filepath-variable).
Future<void> handleTaskExample2(String filePath) async {
File largeFile = File(filePath);
firebase_storage.UploadTask task = firebase_storage.FirebaseStorage.instance
.ref('uploads/picture-to-upload.jpeg')
.putFile(largeFile);
task.snapshotEvents.listen((firebase_storage.TaskSnapshot snapshot) {
print('Task state: ${snapshot.state}');
print(
'Progress: ${(snapshot.bytesTransferred / snapshot.totalBytes) * 100} %');
}, onError: (e) {
print(task.snapshot);
if (e.code == 'permission-denied') {
print('User does not have permission to upload to this reference.');
}
});
try {
await task;
print('Upload complete.');
} on firebase_core.FirebaseException catch (e) {
if (e.code == 'permission-denied') {
print('User does not have permission to upload to this reference.');
}
// ...
}
}
Actually, everything works fine. My uploaded Image to my Storage gets shown in my GridView inside the App...
But if I upload an Image to my Storage manually over the FireBase Website, my gridView (where the pictures from my storage are shown) is not auto-updating.
But if I change my screen and come back, the manually uploaded picture is shown.
It looks like I need something like a StreamBuilder that my App gets informed whenever changes happen in my Storage.
Does anyone have an Idea how to implement this or any other ideas to solve my problem?
Thanks
The listAll and list only get the data once so you unfortunately can't have a stream on them. You have 2 options here.
Call the listAll in a periodic interval like every 2-3 min
Create a cloud functions that get's triggered when files get uploaded to that specific directory and change a value in RTDB or Firestore. In the App you can listen to that data. Idealy it should be just a timestamp. You can store in your app the last timestamp you got from the database and if that changes just call listAll.
It depends on your usecase what would fit for you the best.

Flutter Blue Sending Quite Large File from BLE device to App

I need to send a file .txt from a device to my app (worst case almost 2mb). The BLE device divide the file into packages. I don't know if my method is correct, but I create a loop of characteristic.write/characteristic.read telling everytime what package the device has to send.
Here's my code:
for(int i = 0; i < packNumber.length; i++) {
initialValue = '9,50,100,$i,0,$checksumId,0/';
await characteristic
.write(utf8.encode(initialValue)).then((wValue) async {
await Future.delayed(Duration(milliseconds: 100)).then((value) async {
await characteristic.read().then((rValue) {
//do something with rVlaue
});
});
});
}
It works, but is it the best solution? And in case how can I speed up the transfer (for now I have to set a delay before reading, waiting for characteristic.write to finish)?
Thank you guys
You should change the remote device to send notifications instead of using reads. That gives maximum throughput.

Flutter IOS beacon not able to scan with Region and UUID settings

I have this problem is that in flutter I notice there is not able to operate or use the traditional bluetooth as there is no any library supporting it. I have tested flutter_blue-master etc. So then I saw that it can behave as beacon. So I have used the codes below. For android I just set
Region(
identifier: 'com.example.myDeviceRegion',)); its able to work. So the same I set in IOS its not able to work? So what is best workaround for blueetooth in flutter? I am using this package flutter_beacon. For the beacon broadcasting I am using this package beacon_broadcast.
initScanBeacon() async {
await flutterBeacon.initializeScanning;
await checkAllRequirements();
if (!authorizationStatusOk ||
!locationServiceEnabled ||
!bluetoothEnabled) {
print('RETURNED, authorizationStatusOk=$authorizationStatusOk, '
'locationServiceEnabled=$locationServiceEnabled, '
'bluetoothEnabled=$bluetoothEnabled');
return;
}
/*final regions = <Region>[
Region(
identifier: 'com.example.myDeviceRegion',
),
];*/
final regions = <Region>[];
regions.add(Region(
identifier: 'com.example.myDeviceRegion',
minor: 100,
major: 1));
if (_streamRanging != null) {
if (_streamRanging.isPaused) {
_streamRanging.resume();
return;
}
}
_streamRanging =
flutterBeacon.monitoring(regions).listen((MonitoringResult result) {
print(result);
if (result != null && mounted) {
print("GOT RESTULT READY");
setState(() {
//_regionBeacons[result.region] = result.region;
_beacons.clear();
print("List value is json"+result.toJson.toString());
_regionBeacons.values.forEach((list) {
print("List value is");
_beacons.addAll(list);
print("after Beacon size now is "+_beacons.length.toString());
});
//_beacons.sort(_compareParameters);
print("Beacon size now is "+_beacons.length.toString());
});
}
});
}
A few things to check:
Make sure your Region definition has a proximityUUID value. I am surprised that it works even on Android without this. On iOS it certainly won't work at all -- iOS requires a beacon proximityUUID be specified up front in order to detect. The value you give for the prximityUUID must exactly match what your beacon is advertising or you won't see it.
Make sure you have gone through all the iOS setup steps here: https://pub.dev/packages/flutter_beacon
Be extra sure that you have granted location permission to your iOS app. You can go to Settings -> Your App Name to check if location permission is granted.
Make sure bluetooth is enabled in settings for the phone
Make sure location is enabled in settings for the phone
https://pub.dev/packages/flutter_beacon
There's an update on GitHub but was yet to push to pub.dev previously.
Do update to 0.5.0.
Always check pub.dev for updates. or github reported issues.