// SPDX-FileCopyrightText: Copyright 2023 Open Mobile Platform LLC // SPDX-License-Identifier: BSD-3-Clause import 'package:camera_aurora/camera_aurora_platform_interface.dart'; import 'package:flutter/material.dart'; import 'camera_data.dart'; class CameraViewfinder extends StatefulWidget { const CameraViewfinder({ super.key, required this.width, required this.height, }); final double width; final double height; @override State createState() => _CameraViewfinderState(); } class _CameraViewfinderState extends State { CameraState _cameraState = CameraState.fromJson({}); @override initState() { super.initState(); CameraAuroraPlatform.instance.startCapture(widget.width, widget.height); CameraAuroraPlatform.instance.onChangeState().listen((event) { if (mounted) { setState(() { _cameraState = event; }); } }); } @override void dispose() { super.dispose(); CameraAuroraPlatform.instance.stopCapture(); } @override Widget build(BuildContext context) { if (_cameraState.hasError()) { return Center( child: Text( 'Error: ${_cameraState.error}', style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.white), ), ); } else if (_cameraState.isNotEmpty()) { int turn = 0; switch (_cameraState.rotationDisplay) { case 0: turn = _cameraState.id.contains('front') ? -1 : 1; break; case 90: turn = 0; break; case 180: turn = _cameraState.id.contains('front') ? 1 : -1; break; default: // 270 turn = 2; } double height = 10; double width = 10; if (_cameraState.height != 0 && _cameraState.width != 0) { if (_cameraState.rotationDisplay == 90 || _cameraState.rotationDisplay == 270) { width = widget.width * _cameraState.height / _cameraState.width; height = widget.height * _cameraState.width / _cameraState.height; } else { width = _cameraState.height * widget.height / _cameraState.width; height = _cameraState.width * widget.width / _cameraState.height; } } return RotatedBox( quarterTurns: turn, child: SizedBox( width: height, // height height: width, // widht child: Opacity( opacity: _cameraState.height == 0 ? 0 : 1, child: Texture(textureId: _cameraState.textureId), ), ), ); } return const SizedBox.shrink(); } }