Skip to content

Commit 3e5323e

Browse files
committed
update example
1 parent 0e3a266 commit 3e5323e

File tree

4 files changed

+98
-53
lines changed

4 files changed

+98
-53
lines changed

example/lib/main.dart

Lines changed: 93 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import 'dart:io';
1+
import 'dart:async';
22
import 'dart:typed_data';
3-
import 'dart:ui' as ui;
43

4+
import 'package:camera_universal/camera_universal.dart';
55
import 'package:flutter/material.dart';
6-
import 'dart:async';
7-
import 'package:image/image.dart' as img;
86

97
import 'package:opencv_dart/opencv_dart.dart' as cv;
108

@@ -20,35 +18,49 @@ class MyApp extends StatefulWidget {
2018
}
2119

2220
class _MyAppState extends State<MyApp> {
23-
cv.Mat? image;
21+
var images = <Uint8List>[];
22+
Uint8List? videoFrame;
23+
CameraController cameraController = CameraController();
2424

2525
@override
2626
void initState() {
2727
super.initState();
28+
task();
2829
}
2930

30-
Future<ui.Image> convertImageToFlutterUi(img.Image image) async {
31-
if (image.format != img.Format.uint8 || image.numChannels != 4) {
32-
final cmd = img.Command()
33-
..image(image)
34-
..convert(format: img.Format.uint8, numChannels: 4);
35-
final rgba8 = await cmd.getImageThread();
36-
if (rgba8 != null) {
37-
image = rgba8;
31+
Future<void> task() async {
32+
await cameraController.initializeCameras();
33+
await cameraController.initializeCamera(
34+
setState: setState,
35+
);
36+
await cameraController.activateCamera(
37+
setState: setState,
38+
mounted: () {
39+
return mounted;
40+
},
41+
);
42+
}
43+
44+
void takePicture() async {
45+
var frame = cv.Mat.empty();
46+
if (cameraController.is_camera_init) {
47+
final im = await cameraController.action_take_picture(
48+
onCameraNotInit: () {}, onCameraNotSelect: () {}, onCameraNotActive: () {});
49+
if (im != null) {
50+
frame = cv.imread(im.path);
3851
}
3952
}
53+
if (!frame.isEmpty) {
54+
setState(() {
55+
videoFrame = cv.imencode(cv.ImageFormat.png.ext, frame);
56+
});
57+
}
58+
}
4059

41-
ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromUint8List(image.toUint8List());
42-
43-
ui.ImageDescriptor id = ui.ImageDescriptor.raw(buffer,
44-
height: image.height, width: image.width, pixelFormat: ui.PixelFormat.rgba8888);
45-
46-
ui.Codec codec = await id.instantiateCodec(targetHeight: image.height, targetWidth: image.width);
47-
48-
ui.FrameInfo fi = await codec.getNextFrame();
49-
ui.Image uiImage = fi.image;
50-
51-
return uiImage;
60+
@override
61+
void dispose() {
62+
cameraController.dispose();
63+
super.dispose();
5264
}
5365

5466
@override
@@ -61,35 +73,64 @@ class _MyAppState extends State<MyApp> {
6173
appBar: AppBar(
6274
title: const Text('Native Packages'),
6375
),
64-
body: SingleChildScrollView(
65-
child: Container(
66-
alignment: Alignment.center,
67-
padding: const EdgeInsets.all(10),
68-
child: Column(
69-
children: [
70-
Image.asset(
71-
"images/lenna.png",
72-
width: 100,
73-
height: 100,
76+
body: Container(
77+
alignment: Alignment.center,
78+
child: Column(
79+
children: [
80+
Expanded(
81+
flex: 1,
82+
child: Row(
83+
children: [
84+
Expanded(
85+
child: Camera(
86+
cameraController: cameraController,
87+
onCameraNotInit: (context) {
88+
return const SizedBox.shrink();
89+
},
90+
onCameraNotSelect: (context) {
91+
return const SizedBox.shrink();
92+
},
93+
onCameraNotActive: (context) {
94+
return const SizedBox.shrink();
95+
},
96+
onPlatformNotSupported: (context) {
97+
return const SizedBox.shrink();
98+
},
99+
),
100+
),
101+
Expanded(child: videoFrame == null ? Text("Empty") : Image.memory(videoFrame!))
102+
],
103+
),
104+
),
105+
ElevatedButton(
106+
onPressed: () async {
107+
final data = await DefaultAssetBundle.of(context).load("images/lenna.png");
108+
final im = cv.imdecode(data.buffer.asUint8List(), cv.IMREAD_COLOR);
109+
final gray = cv.Mat.empty();
110+
cv.cvtColor(im, gray, cv.COLOR_BGR2GRAY);
111+
final blur = cv.Mat.empty();
112+
cv.gaussianBlur(im, blur, (7, 7), 2, sigmaY: 2);
113+
setState(() {
114+
images = [
115+
data.buffer.asUint8List(),
116+
cv.imencode(cv.ImageFormat.png.ext, gray),
117+
cv.imencode(cv.ImageFormat.png.ext, blur)
118+
];
119+
});
120+
takePicture();
121+
},
122+
child: const Text("Process"),
123+
),
124+
Expanded(
125+
flex: 2,
126+
child: ListView.builder(
127+
itemCount: images.length,
128+
itemBuilder: (ctx, idx) => Card(
129+
child: Image.memory(images[idx]),
130+
),
74131
),
75-
spacerSmall,
76-
ElevatedButton(
77-
onPressed: () async {
78-
final data = await DefaultAssetBundle.of(context).load("images/lenna.png");
79-
setState(() {
80-
image = cv.imdecode(data.buffer.asUint8List(), cv.IMREAD_GRAYSCALE);
81-
});
82-
},
83-
child: const Text("Process")),
84-
image == null
85-
? Text("Image is null!")
86-
: Image.memory(
87-
cv.imencode(cv.ImageFormat.png.ext, image!),
88-
width: 100,
89-
height: 100,
90-
)
91-
],
92-
),
132+
),
133+
],
93134
),
94135
),
95136
),

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dependencies:
3737
# The following adds the Cupertino Icons font to your application.
3838
# Use with the CupertinoIcons class for iOS style icons.
3939
cupertino_icons: ^1.0.2
40-
image: ^4.1.4
40+
camera_universal: ^0.0.3
4141

4242
dev_dependencies:
4343
flutter_test:

example/windows/flutter/generated_plugin_registrant.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
#include "generated_plugin_registrant.h"
88

9+
#include <camera_windows/camera_windows.h>
910

1011
void RegisterPlugins(flutter::PluginRegistry* registry) {
12+
CameraWindowsRegisterWithRegistrar(
13+
registry->GetRegistrarForPlugin("CameraWindows"));
1114
}

example/windows/flutter/generated_plugins.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44

55
list(APPEND FLUTTER_PLUGIN_LIST
6+
camera_windows
67
)
78

89
list(APPEND FLUTTER_FFI_PLUGIN_LIST

0 commit comments

Comments
 (0)