Generating image files from CameraImage - flutter

We would like to use CameraController.startImageStream() instead of CameraController.takePicture() to get a return value equivalent to CameraController.takePicture(). We know that various answers have been documented for these but we have not yet succeeded with any of the methods.
We have implemented the following. We tried setting the imageFormatGroup during CameraController generation, but this did not improve things. We have to make these work on both Android and iOS. We want a way to run it faster. It can be written in Flutter or using C++ and ffi.
import 'package:image/image.dart' as imglib;
final cameras = await availableCameras();
final firstCamera = cameras.first;
cameraController = CameraController(
firstCamera,
ResolutionPreset.medium,
// imageFormatGroup: ImageFormatGroup.yuv420,
);
await cameraController!.initialize();
await cameraController!.startImageStream((CameraImage cameraImage) async {
cameraController!.stopImageStream();
Uint8List list = cameraImage.planes.first.bytes;
final width = cameraImage.width;
final height = cameraImage.height;
imglib.Image test = imglib.Image.fromBytes(width, height, list);
List<int> _imageBytes = imglib.encodeJpg(test);
Uint8List convert = Uint8List.fromList(_imageBytes);
File file = File.fromRawPath(convert);
XFile? result = XFile(file.path);
});
Index out of range occurred when we called encodeJpg(). How should we fix this?
fvm flutter doctor -v
[✓] Flutter (Channel stable, 3.3.6, on macOS 12.6 21G115 darwin-x64, locale en-JP)
• Flutter version 3.3.6 on channel stable at /Users/xxxxx/fvm/versions/stable
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 6928314d50 (3 weeks ago), 2022-10-25 16:34:41 -0400
• Engine revision 3ad69d7be3
• Dart version 2.18.2
• DevTools version 2.15.0
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
• Android SDK at /Users/xxxxx/src/android-sdks
• Platform android-33, build-tools 33.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 13F100
• CocoaPods version 1.11.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
[✓] VS Code (version 1.73.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.52.0
[✓] Connected device (3 available)
• SHV43 (mobile) • XXXXXXXXXXXXXXX • android-arm64 • Android 10 (API 29)
• macOS (desktop) • macos • darwin-x64 • macOS 12.6 21G115 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 107.0.5304.110
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!

I assume your problem comes from mixing different image formats.
The camera stream in this line
Uint8List list = cameraImage.planes.first.bytes
returns an image that is encoded with YUV instead of the common RGB. So before further using this image, I would recommend converting it.
There are multiple sources already out there on how to do that conversion. Let me point you to this blog post and the following stackoverflow questions: 1 2.
You certainly can implement this conversion via ffi, but it is not strictly necessary.

Related

Flutter writeAsBytes from dart:io library returns PathNotFoundException

I am trying to use the following function in flutter:
Future<File> getFile(String fileName) async {
final appDir = await getTemporaryDirectory();
final appPath = appDir.path;
final fileOnDevice = File('$appPath/$fileName');
final rawAssetFile = await rootBundle.load(fileName);
final rawBytes = rawAssetFile.buffer.asUint8List();
await fileOnDevice.writeAsBytes(rawBytes, flush: true);
return fileOnDevice;
}
But somehow i keep getting this error, failed PathNotFoundException: Cannot open file, path = '/var/mobile/Containers/Data/Application/352527E9-XXXX-XXXX-XXXX-DCFXXXXXXXXX/Library/Caches/assets/
The function is called by final dataFile = await getFile('assets/sample.txt');
Does anyone know how to solve this? Any help is appreciated!
This is my flutter doctor -v
[✓] Flutter (Channel stable, 3.7.0, on macOS 13.1 22C65 darwin-arm64, locale en-SG)
• Flutter version 3.7.0 on channel stable at /Users/xaviertoh/Documents/Flutter/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision b06b8b2710 (3 weeks ago), 2023-01-23 16:55:55 -0800
• Engine revision b24591ed32
• Dart version 2.19.0
• DevTools version 2.20.1
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
• Android SDK at /Users/xaviertoh/Library/Android/sdk
• Platform android-33, build-tools 33.0.1
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14C18
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
[✓] VS Code (version 1.75.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.58.0
[✓] Connected device (3 available)
• iPhone (mobile) • 00008020-000D05313C02002E • ios • iOS 13.5 17F75
• macOS (desktop) • macos • darwin-arm64 • macOS 13.1 22C65
darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 110.0.5481.77
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
Thank you for reading!
Things i have tried:
I have confirmed that my file name is correct in the assets folder and i also tried running this code in various locations in the code such as in a widget and also in main() just to test it out.
I also tried running flutter in xcode and visual studio code.
Also tried to run the code in android emulator and also on iPhone physical device.
The /var/mobile/Containers/Data/Application/.../Library/Caches/assets/ directory must already exist. File.writeAsBytes won't automatically create parent directories for you.
You can do that step by first doing:
await fileOnDevice.parent.create(recursive: true);

Flutter assign null as value to Map<String, dynamic> throws "Error: Expected a value of type 'Object', but got one of type 'Null'"

I'm trying to assign null to a specific key in Map<String, dynamic> in order to nullify the value in my SQL DB.
But I keep getting an error Error: Expected a value of type 'Object', but got one of type 'Null', in dartpad it works fine without any errors.
Here is the error I'm getting:
Error: Expected a value of type 'Object', but got one of type 'Null'
Here is my code:
Map<String, dynamic> map = {};
map["key2"] = null;
Flutter doctor:
[✓] Flutter (Channel master, 3.1.0-0.0.pre.1014, on macOS 12.5.1 21G83 darwin-x64, locale en-IL)
• Flutter version 3.1.0-0.0.pre.1014 at /Users/daniel/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 08e61c3be2 (6 months ago), 2022-05-31 00:18:09 -0400
• Engine revision 693f95391e
• Dart version 2.18.0 (build 2.18.0-152.0.dev)
• DevTools version 2.13.1
[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
• Android SDK at /Users/daniel/Library/Android/sdk
• Platform android-32, build-tools 32.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• CocoaPods version 1.11.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2020.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
[✓] VS Code (version 1.73.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.52.0
[✓] Connected device (2 available)
• macOS (desktop) • macos • darwin-x64 • macOS 12.5.1 21G83 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 107.0.5304.110
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!

Flutter - Unable to send message using NearByService

I am experiencing an issue to send messages from the advertising device to the browser device(Reader) and getting an error like this-
Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: com/event. Response ID: 0
The steps followed are -
Advertiser advertises using a key.
Browser establishes a connection.
Advertiser sends a message using the following code of NearByservice package -
/// Sends a message encapsulated in a Data instance to nearby peers.
FutureOr sendMessage(String deviceID, String message) async {
await _channel.invokeMethod(_sendMessage, <String, dynamic>{
'deviceId': deviceID,
'message': message,
});
And getting the above error mentioned in the title. Assumed some environmental issues and tried to clean up project, rebuilt and faced the same issue.
JFYI -
After executing "flutter doctor -v", the following logs were received -
[✓] Flutter (Channel master, 3.1.0-0.0.pre.1487, on macOS 12.4 21F79 darwin-x64, locale en-BD)
• Flutter version 3.1.0-0.0.pre.1487 on channel master at /Users/imac/Flutter_Projects/Flutter_SDK/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 20217092c1 (4 days ago), 2022-07-02 23:49:08 -0400
• Engine revision 51dc113c3f
• Dart version 2.18.0 (build 2.18.0-249.0.dev)
• DevTools version 2.15.0
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
• Android SDK at /Users/imac/Library/Android/sdk
• Platform android-33, build-tools 33.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 13.4)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 13F17a
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
[✓] Connected device (4 available)
• SM G960F (mobile) • 2295dc5c4a0d7ece • android-arm64 • Android 10 (API 29)
• SM N950F (mobile) • ce061716a909a80e0d7e • android-arm64 • Android 9 (API 28)
• macOS (desktop) • macos • darwin-x64 • macOS 12.4 21F79 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 103.0.5060.114
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
Can anyone support this to accomplish please?

Flutter path_provider getApplicationSupportDirectory() throws _CastError Null check operator on a null value

I am having issues running my flutter application. I am using objectbox-dart for storing data in my app. So far so good, but when I create a store using path_provider, the getApplicationSupportDirectory() throws _CastError: Null check operator on a null value. Below is the output of flutter doctor -v
[✓] Flutter (Channel stable, 2.2.1, on Mac OS X 10.15.7 19H2 darwin-x64, locale en-PK)
• Flutter version 2.2.1 at /Users/muhammadumar/sdks/flutter
• Framework revision 02c026b03c (11 days ago), 2021-05-27 12:24:44 -0700
• Engine revision 0fdb562ac8
• Dart version 2.13.1
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Users/muhammadumar/Library/Android/sdk
• Platform android-30, build-tools 30.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.4, Build version 12D4e
• CocoaPods version 1.10.0
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
[✓] VS Code (version 1.56.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.23.0
[✓] Connected device (2 available)
• MI 8 Lite (mobile) • bb4e8ab • android-arm64 • Android 10 (API 29)
• Chrome (web) • chrome • web-javascript • Google Chrome 91.0.4472.77
• No issues found!
Below is the exact line of code of path_provider file where exception is being caught
Future<Directory> getApplicationSupportDirectory() async {
final String? path = await _platform.getApplicationSupportPath();
if (path == null) {
throw MissingPlatformDirectoryException(
'Unable to get application support directory');
}
return Directory(path);
}
I am unable to run my app. Any help would be appreciated.
Thanks

Error: TimeoutException Future not completed And TypeError: T.as is not a function at _Future.new.[_setValue]

I got this error while dealing with Flutter web and Firebase Storage:
Error: TimeoutException after 0:00:05.000000: Future not completed
at Object.createErrorWithStack (http://localhost:65534/dart_sdk.js:4478:12)
at Object._rethrow (http://localhost:65534/dart_sdk.js:37394:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:65534/dart_sdk.js:37388:13)
Then followed by this error (always in pairs)
TypeError: T.as is not a function
at _Future.new.[_setValue] (http://localhost:60098/dart_sdk.js:32317:11)
at Function._propagateToListeners (http://localhost:60098/dart_sdk.js:32660:30)
at async._AsyncCallbackEntry.new.callback (http://localhost:60098/dart_sdk.js:32357:27)
at Object._microtaskLoop (http://localhost:60098/dart_sdk.js:37220:13)
at _startMicrotaskLoop (http://localhost:60098/dart_sdk.js:37226:13)
at http://localhost:60098/dart_sdk.js:32848:9
I have spent the whole 2 days to figure out where this comes from.
So far with the best of my knowledge it seems to come from when I upload an image to Firebase Storage:
Here is the code:
final uploadTask = _storageRef.child(_path).put(_file,
fb.UploadMetadata(
contentType: _file.type, customMetadata: customMetaData));
// Listening to the progress.
final StreamSubscription<fb.UploadTaskSnapshot> stream =
uploadTask.onStateChanged.listen((event) => event);
stream.onDone(() {
uploadTask.cancel();
stream.cancel();
});
And just in case someone asks, here's my flutter doctor -v
[✓] Flutter (Channel dev, 1.20.0-0.0.pre, on Mac OS X 10.15.5 01, locale
en-US)
• Flutter version 1.20.0-0.0.pre at /Applications/flutter
• Framework revision d9653445f4 (7 days ago), 2020-06-09 18:43:03 -0400
• Engine revision e8c13aa012
• Dart version 2.9.0 (build 2.9.0-14.0.dev 5c1376615e)
• Pub download mirror https://pub.flutter-io.cn
• Flutter download mirror https://storage.flutter-io.cn
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at /Applications/Android/sdk
• Platform android-29, build-tools 29.0.3
• ANDROID_HOME = /Applications/Android/sdk
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build
1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 11.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.4.1, Build version 11E503a
• CocoaPods version 1.9.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 47.0.2-dev.2
• Dart plugin version 193.7361
• Java version OpenJDK Runtime Environment (build
1.8.0_242-release-1644-b3-6222593)
[✓] VS Code (version 1.46.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.11.0
[✓] Connected device (2 available)
• Web Server • web-server • web-javascript • Flutter Tools
• Chrome • chrome • web-javascript • Google Chrome 83.0.4103.106
• No issues found!
Any help is appreciated. Thank you
I just encountered this issue too; it seems to be caused by some quirk in the JS-interop feature of dart (I created an issue in dart-lang/sdk attempting to get some clarification!)
The root cause of this particular problem was an issue with the internal implementation of the onStateChanged Stream in the UploadTask class, here.
The original code was defining the onCompletion callback of the UploadTask as:
var onCompletion = allowInterop(() => _changeController.close());
But whatever .close() was returning was unexpected in the js-interop layer and causing this crash, so I replaced it by:
var onCompletion = allowInterop(() {
_changeController.close();
});
Which did the trick, at least on the firebase_storage_web plugin I'm working on!
The fix has been published as firebase:^7.3.1
PS: Looking at the package:firebase code, I bet the OP wouldn't have any issues if they used uploadTask.future, instead of uploadTask.onStateChanged, but of course you can't have upload progress then. Docs.