Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
rainyl committed Mar 13, 2024
1 parent 0e3a266 commit 3e5323e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 53 deletions.
145 changes: 93 additions & 52 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'dart:io';
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:camera_universal/camera_universal.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:image/image.dart' as img;

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

Expand All @@ -20,35 +18,49 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
cv.Mat? image;
var images = <Uint8List>[];
Uint8List? videoFrame;
CameraController cameraController = CameraController();

@override
void initState() {
super.initState();
task();
}

Future<ui.Image> convertImageToFlutterUi(img.Image image) async {
if (image.format != img.Format.uint8 || image.numChannels != 4) {
final cmd = img.Command()
..image(image)
..convert(format: img.Format.uint8, numChannels: 4);
final rgba8 = await cmd.getImageThread();
if (rgba8 != null) {
image = rgba8;
Future<void> task() async {
await cameraController.initializeCameras();
await cameraController.initializeCamera(
setState: setState,
);
await cameraController.activateCamera(
setState: setState,
mounted: () {
return mounted;
},
);
}

void takePicture() async {
var frame = cv.Mat.empty();
if (cameraController.is_camera_init) {
final im = await cameraController.action_take_picture(
onCameraNotInit: () {}, onCameraNotSelect: () {}, onCameraNotActive: () {});
if (im != null) {
frame = cv.imread(im.path);
}
}
if (!frame.isEmpty) {
setState(() {
videoFrame = cv.imencode(cv.ImageFormat.png.ext, frame);
});
}
}

ui.ImmutableBuffer buffer = await ui.ImmutableBuffer.fromUint8List(image.toUint8List());

ui.ImageDescriptor id = ui.ImageDescriptor.raw(buffer,
height: image.height, width: image.width, pixelFormat: ui.PixelFormat.rgba8888);

ui.Codec codec = await id.instantiateCodec(targetHeight: image.height, targetWidth: image.width);

ui.FrameInfo fi = await codec.getNextFrame();
ui.Image uiImage = fi.image;

return uiImage;
@override
void dispose() {
cameraController.dispose();
super.dispose();
}

@override
Expand All @@ -61,35 +73,64 @@ class _MyAppState extends State<MyApp> {
appBar: AppBar(
title: const Text('Native Packages'),
),
body: SingleChildScrollView(
child: Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(10),
child: Column(
children: [
Image.asset(
"images/lenna.png",
width: 100,
height: 100,
body: Container(
alignment: Alignment.center,
child: Column(
children: [
Expanded(
flex: 1,
child: Row(
children: [
Expanded(
child: Camera(
cameraController: cameraController,
onCameraNotInit: (context) {
return const SizedBox.shrink();
},
onCameraNotSelect: (context) {
return const SizedBox.shrink();
},
onCameraNotActive: (context) {
return const SizedBox.shrink();
},
onPlatformNotSupported: (context) {
return const SizedBox.shrink();
},
),
),
Expanded(child: videoFrame == null ? Text("Empty") : Image.memory(videoFrame!))
],
),
),
ElevatedButton(
onPressed: () async {
final data = await DefaultAssetBundle.of(context).load("images/lenna.png");
final im = cv.imdecode(data.buffer.asUint8List(), cv.IMREAD_COLOR);
final gray = cv.Mat.empty();
cv.cvtColor(im, gray, cv.COLOR_BGR2GRAY);
final blur = cv.Mat.empty();
cv.gaussianBlur(im, blur, (7, 7), 2, sigmaY: 2);
setState(() {
images = [
data.buffer.asUint8List(),
cv.imencode(cv.ImageFormat.png.ext, gray),
cv.imencode(cv.ImageFormat.png.ext, blur)
];
});
takePicture();
},
child: const Text("Process"),
),
Expanded(
flex: 2,
child: ListView.builder(
itemCount: images.length,
itemBuilder: (ctx, idx) => Card(
child: Image.memory(images[idx]),
),
),
spacerSmall,
ElevatedButton(
onPressed: () async {
final data = await DefaultAssetBundle.of(context).load("images/lenna.png");
setState(() {
image = cv.imdecode(data.buffer.asUint8List(), cv.IMREAD_GRAYSCALE);
});
},
child: const Text("Process")),
image == null
? Text("Image is null!")
: Image.memory(
cv.imencode(cv.ImageFormat.png.ext, image!),
width: 100,
height: 100,
)
],
),
),
],
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
image: ^4.1.4
camera_universal: ^0.0.3

dev_dependencies:
flutter_test:
Expand Down
3 changes: 3 additions & 0 deletions example/windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include "generated_plugin_registrant.h"

#include <camera_windows/camera_windows.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
CameraWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("CameraWindows"));
}
1 change: 1 addition & 0 deletions example/windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
camera_windows
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
Expand Down

0 comments on commit 3e5323e

Please sign in to comment.