Skip to content

Commit

Permalink
Converted functions into async
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Islam committed Nov 3, 2023
1 parent c8b16a6 commit 33effc2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## 0.0.1

* TODO: Describe initial release.
* Initial release.

## 0.0.2

* Fixes in core API.

## 0.0.3

* Created API for storing frame into Base64 Image.

## 0.0.4

* Code refractor

## 0.0.5

* Converted API functions into asyc functions.
22 changes: 3 additions & 19 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
final _cameraLinuxPlugin = CameraLinux();
bool _isCameraOpen = false;
late Uint8List _latestFrameData;
late String _base64Image;

@override
Expand All @@ -32,24 +31,9 @@ class _MyAppState extends State<MyApp> {
await _cameraLinuxPlugin.initializeCamera();
}

// Convert Frame Into Base64
String uint8ListToBase64Url(Uint8List data) {
String base64String = base64Encode(data);

String base64Url = base64String
.replaceAll('+', '-')
.replaceAll('/', '_')
.replaceAll('=', '');

int requiredLength = (4 - (base64Url.length % 4)) % 4;
return base64Url + '=' * requiredLength;
}

// Capture The Frame
void _captureImage() {
_latestFrameData = _cameraLinuxPlugin.captureImage();
String base64Url = uint8ListToBase64Url(_latestFrameData);
_base64Image = base64Url;
// Capture The Image
void _captureImage() async {
_base64Image = await _cameraLinuxPlugin.captureImage();
setState(() {
_isCameraOpen = true;
});
Expand Down
20 changes: 18 additions & 2 deletions lib/camera_linux.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:ffi';
import 'dart:typed_data';
import 'camera_linux_bindings_generated.dart';
Expand All @@ -22,11 +23,26 @@ class CameraLinux {
_bindings.stopVideoCapture();
}

// Convert Frame Into Base64
String uint8ListToBase64Url(Uint8List data) {
String base64String = base64Encode(data);

String base64Url = base64String
.replaceAll('+', '-')
.replaceAll('/', '_')
.replaceAll('=', '');

int requiredLength = (4 - (base64Url.length % 4)) % 4;
return base64Url + '=' * requiredLength;
}

// Capture The Frame
Uint8List captureImage() {
Future<String> captureImage() async {
final lengthPtr = calloc<Int>();
Pointer<Uint8> framePointer = _bindings.getLatestFrameBytes(lengthPtr);
return getLatestFrameData(framePointer, lengthPtr.value);
final latestFrame = getLatestFrameData(framePointer, lengthPtr.value);
final base64Image = uint8ListToBase64Url(latestFrame);
return base64Image;
}

// Get The Latest Frame
Expand Down
10 changes: 5 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: camera_linux
description: A new Flutter FFI plugin project.
version: 0.0.3
description: Take pictures from camere on linux platform.
version: 0.0.5
homepage: https://github.com/islamroshan/camera-linux.git

environment:
Expand All @@ -12,14 +12,14 @@ dependencies:
sdk: flutter
plugin_platform_interface: ^2.0.2
ffi: ^2.0.1

dev_dependencies:
ffigen: ^6.1.2
image: ^3.0.1
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

Expand All @@ -35,7 +35,7 @@ flutter:
# This is required for using `dart:ffi`.
# All these are used by the tooling to maintain consistency when
# adding or updating assets for this project.
#
#
# Please refer to README.md for a detailed explanation.
plugin:
platforms:
Expand Down

0 comments on commit 33effc2

Please sign in to comment.