Browse Source

[texture] Add camera width and height

embedder_texture
Vitaliy Zarubin 1 year ago
parent
commit
4db615def4
  1. 11
      example/lib/packages/embedder_texture/page.dart
  2. 39
      packages/embedder_texture/aurora/camera.cpp
  3. 2
      packages/embedder_texture/aurora/camera_pixels_helper.cpp
  4. 6
      packages/embedder_texture/aurora/embedder_texture_plugin.cpp
  5. 7
      packages/embedder_texture/aurora/include/embedder_texture/camera.h
  6. 30
      packages/embedder_texture/lib/embedder_texture.dart
  7. 10
      packages/embedder_texture/lib/embedder_texture_method_channel.dart
  8. 2
      packages/embedder_texture/lib/embedder_texture_platform_interface.dart

11
example/lib/packages/embedder_texture/page.dart

@ -31,16 +31,19 @@ class _EmbedderTexturePageState extends AppState<EmbedderTexturePage> {
MediaQueryData media, MediaQueryData media,
AppLocalizations l10n, AppLocalizations l10n,
) { ) {
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height;
return BlockLayout<EmbedderTextureModel>( return BlockLayout<EmbedderTextureModel>(
model: getIt<EmbedderTextureModel>(), model: getIt<EmbedderTextureModel>(),
title: widget.package.key, title: widget.package.key,
builder: (context, child, model) { builder: (context, child, model) {
return const Padding( return Padding(
padding: EdgeInsets.all(20), padding: const EdgeInsets.all(20),
child: Center( child: Center(
child: EmbedderTexture( child: EmbedderTexture(
width: 320, width: width,
height: 240, height: height,
), ),
), ),
); );

39
packages/embedder_texture/aurora/camera.cpp

@ -26,20 +26,25 @@ void Camera::InitializeCamera(int cameraID)
} }
} }
void Camera::StartCapture() Aurora::StreamCamera::CameraCapability Camera::StartCapture()
{ {
if (m_camera) { if (m_camera) {
Aurora::StreamCamera::CameraInfo info; Aurora::StreamCamera::CameraInfo info;
if (m_camera->getInfo(info)) { if (m_camera->getInfo(info)) {
std::vector<Aurora::StreamCamera::CameraCapability> caps; std::vector<Aurora::StreamCamera::CameraCapability> caps;
if (m_manager->queryCapabilities(info.id, caps)) { if (m_manager->queryCapabilities(info.id, caps)) {
m_camera->startCapture(caps.at(1)); for(Aurora::StreamCamera::CameraCapability cap : caps) {
if (m_widthTexture + m_heightTexture < cap.width + cap.height) {
m_camera->startCapture(cap);
return cap;
}
} }
} }
} }
} }
return Aurora::StreamCamera::CameraCapability{};
}
void Camera::StopCapture() void Camera::StopCapture()
{ {
@ -50,15 +55,22 @@ void Camera::StopCapture()
} }
} }
int64_t Camera::Register(int cameraID) std::map<Encodable, Encodable> Camera::Register(int cameraID, int width, int height)
{ {
m_widthTexture = width;
m_heightTexture = height;
m_textureId = m_plugin->RegisterTexture( m_textureId = m_plugin->RegisterTexture(
[this](size_t width, size_t height) { return this->m_buffer; }); [this](size_t width, size_t height) { return this->m_buffer; });
InitializeCamera(cameraID); InitializeCamera(cameraID);
StartCapture(); auto cab = StartCapture();
return m_textureId; return std::map<Encodable, Encodable>{
{"textureId", m_textureId},
{"width", cab.width},
{"height", cab.height},
};
} }
void Camera::Unregister() void Camera::Unregister()
@ -72,10 +84,18 @@ void Camera::onCameraFrame(std::shared_ptr<Aurora::StreamCamera::GraphicBuffer>
if (buffer->handleType == Aurora::StreamCamera::HandleType::EGL) { if (buffer->handleType == Aurora::StreamCamera::HandleType::EGL) {
// @todo Not tested. The device needs to be completed. // @todo Not tested. The device needs to be completed.
auto eglImage = CameraEGLHelper::EGLCreateImage(buffer); auto eglImage = CameraEGLHelper::EGLCreateImage(buffer);
this->m_buffer = new TextureVariant(FlutterEGLImage{eglImage}); this->m_buffer = new TextureVariant(FlutterEGLImage{
eglImage,
buffer->width,
buffer->height,
});
} else { } else {
auto pixels = CameraPixelsHelper::YUVtoARGB(buffer->mapYCbCr()); auto pixels = CameraPixelsHelper::YUVtoARGB(buffer->mapYCbCr());
this->m_buffer = new TextureVariant(FlutterPixelBuffer{pixels}); this->m_buffer = new TextureVariant(FlutterPixelBuffer{
pixels,
buffer->width,
buffer->height,
});
} }
m_plugin->MarkTextureAvailable(m_textureId); m_plugin->MarkTextureAvailable(m_textureId);
@ -83,7 +103,8 @@ void Camera::onCameraFrame(std::shared_ptr<Aurora::StreamCamera::GraphicBuffer>
void Camera::onCameraError(const std::string &errorDescription) void Camera::onCameraError(const std::string &errorDescription)
{ {
std::cout << "onCameraError" << std::endl; this->Unregister();
std::cout << errorDescription << std::endl;
} }
void Camera::onCameraParameterChanged(Aurora::StreamCamera::CameraParameter parameter, void Camera::onCameraParameterChanged(Aurora::StreamCamera::CameraParameter parameter,

2
packages/embedder_texture/aurora/camera_pixels_helper.cpp

@ -10,8 +10,8 @@ uint8_t *CameraPixelsHelper::YUVtoARGB(std::shared_ptr<const Aurora::StreamCamer
QImage *image = new QImage(size, QImage::Format_ARGB32); QImage *image = new QImage(size, QImage::Format_ARGB32);
planarYuv420ToArgb(frame->y, planarYuv420ToArgb(frame->y,
frame->cb,
frame->cr, frame->cr,
frame->cb,
frame->yStride, frame->yStride,
frame->cStride, frame->cStride,
frame->cStride, frame->cStride,

6
packages/embedder_texture/aurora/embedder_texture_plugin.cpp

@ -36,10 +36,12 @@ void EmbedderTexturePlugin::onMethodCall(const MethodCall &call)
void EmbedderTexturePlugin::onCreate(const MethodCall &call) void EmbedderTexturePlugin::onCreate(const MethodCall &call)
{ {
auto cameraId = 0; auto cameraId = 0;
auto width = call.GetArgument<Encodable::Int>("width");
auto height = call.GetArgument<Encodable::Int>("height");
auto textureId = m_camera->Register(cameraId); auto result = m_camera->Register(cameraId, width, height);
call.SendSuccessResponse(textureId); call.SendSuccessResponse(result);
} }
void EmbedderTexturePlugin::onRemove(const MethodCall &call) void EmbedderTexturePlugin::onRemove(const MethodCall &call)

7
packages/embedder_texture/aurora/include/embedder_texture/camera.h

@ -21,12 +21,12 @@ public:
void onCameraParameterChanged(Aurora::StreamCamera::CameraParameter, void onCameraParameterChanged(Aurora::StreamCamera::CameraParameter,
const std::string &value) override; const std::string &value) override;
int64_t Register(int cameraID); std::map<Encodable, Encodable> Register(int cameraID, int width, int height);
void Unregister(); void Unregister();
private: private:
void InitializeCamera(int cameraID); void InitializeCamera(int cameraID);
void StartCapture(); Aurora::StreamCamera::CameraCapability StartCapture();
void StopCapture(); void StopCapture();
private: private:
@ -38,6 +38,9 @@ private:
int m_cameraId = -1; int m_cameraId = -1;
int64_t m_textureId; int64_t m_textureId;
int m_widthTexture;
int m_heightTexture;
TextureVariant *m_buffer; TextureVariant *m_buffer;
}; };

30
packages/embedder_texture/lib/embedder_texture.dart

@ -19,15 +19,19 @@ class EmbedderTexture extends StatefulWidget {
class _EmbedderTextureState extends State<EmbedderTexture> { class _EmbedderTextureState extends State<EmbedderTexture> {
int _textureID = 0; int _textureID = 0;
int _camera_width = 0;
int _camera_height = 0;
@override @override
initState() { initState() {
super.initState(); super.initState();
EmbedderTexturePlatform.instance EmbedderTexturePlatform.instance
.create(widget.width, widget.height) .create(widget.width, widget.height)
.then((value) => setState(() { .then((data) => setState(() {
if (mounted) { if (mounted) {
_textureID = value!; _textureID = data['textureId']!;
_camera_width = data['width']!;
_camera_height = data['height']!;
debugPrint(_textureID.toString()); debugPrint(_textureID.toString());
} }
})); }));
@ -42,12 +46,32 @@ class _EmbedderTextureState extends State<EmbedderTexture> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_textureID != 0) { if (_textureID != 0) {
return SizedBox(
double w = 0;
double h = 0;
if (MediaQuery.of(context).orientation == Orientation.portrait) {
w = _camera_width * widget.height / (widget.width - 40);
h = widget.width - 40;
} else {
w = _camera_height.toDouble();
h = widget.height - 40 - 56;
}
return Container(
color: Colors.black,
width: widget.width, width: widget.width,
height: widget.height, height: widget.height,
child: Center( child: Center(
child: RotationTransition(
turns: AlwaysStoppedAnimation(MediaQuery.of(context).orientation == Orientation.portrait ? 90 / 360 : 0),
child: SizedBox(
width: w,
height: h,
child: Texture(textureId: _textureID) child: Texture(textureId: _textureID)
), ),
)
),
); );
} }
return const SizedBox.shrink(); return const SizedBox.shrink();

10
packages/embedder_texture/lib/embedder_texture_method_channel.dart

@ -10,12 +10,12 @@ class MethodChannelEmbedderTexture extends EmbedderTexturePlatform {
final methodChannel = const MethodChannel('embedder_texture'); final methodChannel = const MethodChannel('embedder_texture');
@override @override
Future<int?> create(double width, double height) async { Future<Map<dynamic, dynamic>> create(double width, double height) async {
final version = await methodChannel.invokeMethod<int>('create', { final data = await methodChannel.invokeMethod<Map<dynamic, dynamic>?>('create', {
'width': width, 'width': width.round(),
'height': height, 'height': height.round(),
}); });
return version; return data ?? {};
} }
@override @override

2
packages/embedder_texture/lib/embedder_texture_platform_interface.dart

@ -23,7 +23,7 @@ abstract class EmbedderTexturePlatform extends PlatformInterface {
_instance = instance; _instance = instance;
} }
Future<int?> create(double width, double height) { Future<Map<dynamic, dynamic>> create(double width, double height) {
throw UnimplementedError('create() has not been implemented.'); throw UnimplementedError('create() has not been implemented.');
} }

Loading…
Cancel
Save