Explorar o código

1、工作站的摄像头问题

guanxinyi hai 1 ano
pai
achega
2c53a1c27a

+ 20 - 3
lib/pages/facial_recognition/controller.dart

@@ -241,7 +241,9 @@ class FacialRecognitionController extends GetxController
   ) {
     int sensorOrientation = cameraDescription.sensorOrientation;
     DeviceOrientation deviceOrientation = controller.value.deviceOrientation;
-
+    DeviceOrientation lockedCaptureOrientation =
+        controller.value.lockedCaptureOrientation ??
+            DeviceOrientation.landscapeLeft;
     // 根据设备方向调整摄像头角度
     int angle = 0;
     switch (deviceOrientation) {
@@ -259,6 +261,20 @@ class FacialRecognitionController extends GetxController
         break;
     }
 
+    switch (lockedCaptureOrientation) {
+      case DeviceOrientation.portraitUp:
+        break;
+      case DeviceOrientation.landscapeLeft:
+        angle -= 90;
+        break;
+      case DeviceOrientation.portraitDown:
+        angle -= 180;
+        break;
+      case DeviceOrientation.landscapeRight:
+        angle += 90;
+        break;
+    }
+
     /// 前置需要倒转180度
     if (cameraDescription.lensDirection == CameraLensDirection.front) {
       angle -= 180;
@@ -283,8 +299,9 @@ class FacialRecognitionController extends GetxController
       try {
         //  await cameraController.lockCaptureOrientation(DeviceOrientation.landscapeLeft)
 
-        await cameraController
-            .lockCaptureOrientation(cameraController.value.deviceOrientation);
+        await cameraController.lockCaptureOrientation(_isLocalStation
+            ? DeviceOrientation.portraitUp
+            : cameraController.value.deviceOrientation);
       } on CameraException catch (e) {
         PromptBox.toast('Error: ${e.code}\n${e.description}');
       }

+ 23 - 48
lib/pages/facial_recognition/view.dart

@@ -1,5 +1,3 @@
-import 'dart:math';
-
 import 'package:camera/camera.dart';
 import 'package:fis_jsonrpc/rpc.dart';
 import 'package:flutter/material.dart';
@@ -36,8 +34,8 @@ class FacialRecognitionPage extends GetView<FacialRecognitionController> {
             child: Obx(
               () => controller.state.isCameraReady
                   ? Container(
-                      child: _buildCameraArea(),
                       color: Colors.black,
+                      child: _buildCameraArea(),
                     )
                   : const Center(
                       child: CircularProgressIndicator(
@@ -59,21 +57,13 @@ class FacialRecognitionPage extends GetView<FacialRecognitionController> {
             child: LayoutBuilder(builder: (context, constraints) {
               return Stack(
                 children: <Widget>[
-                  Container(
-                    color: Colors.black,
-                    alignment: Alignment.center,
-                    child: _cameraPreviewWidget(),
+                  OverflowBox(
+                    child: AspectRatio(
+                      aspectRatio:
+                          controller.kCameraController?.value.aspectRatio ?? 1,
+                      child: _cameraPreviewWidget(),
+                    ),
                   ),
-                  // OverflowBox(
-                  //   maxHeight: constraints.maxHeight,
-                  //   maxWidth: 2000,
-                  //   child: Container(
-                  //     color: Colors.black,
-                  //     child: Center(
-                  //       child: _cameraPreviewWidget(),
-                  //     ),
-                  //   ),
-                  // ),
                   const Center(child: CameraForFace()),
                   Align(
                     alignment: Alignment.bottomRight,
@@ -98,37 +88,22 @@ class FacialRecognitionPage extends GetView<FacialRecognitionController> {
         child: CircularProgressIndicator(),
       );
     } else {
-      return Transform.rotate(
-        angle: controller.adjustCameraAngle(
-              cameraController,
-              cameraController.description,
-            ) *
-            pi /
-            180,
-        child: Listener(
-          onPointerDown: (_) => controller.pointers++,
-          onPointerUp: (_) => controller.pointers--,
-          child: Column(
-            children: [
-              Expanded(
-                child: CameraPreview(
-                  cameraController,
-                  child: LayoutBuilder(
-                    builder:
-                        (BuildContext context, BoxConstraints constraints) {
-                      return GestureDetector(
-                        behavior: HitTestBehavior.opaque,
-                        onScaleStart: controller.handleScaleStart,
-                        onScaleUpdate: controller.handleScaleUpdate,
-                        onTapDown: (TapDownDetails details) =>
-                            controller.onViewFinderTap(details, constraints),
-                        child: const FaceBoundingBox(),
-                      );
-                    },
-                  ),
-                ),
-              ),
-            ],
+      return Listener(
+        onPointerDown: (_) => controller.pointers++,
+        onPointerUp: (_) => controller.pointers--,
+        child: CameraPreview(
+          cameraController,
+          child: LayoutBuilder(
+            builder: (BuildContext context, BoxConstraints constraints) {
+              return GestureDetector(
+                behavior: HitTestBehavior.opaque,
+                onScaleStart: controller.handleScaleStart,
+                onScaleUpdate: controller.handleScaleUpdate,
+                onTapDown: (TapDownDetails details) =>
+                    controller.onViewFinderTap(details, constraints),
+                child: const FaceBoundingBox(),
+              );
+            },
           ),
         ),
       );

+ 2 - 2
lib/pages/id_card_scan/controller.dart

@@ -264,8 +264,8 @@ class IdCardScanController extends GetxController with WidgetsBindingObserver {
         DeviceOrientation deviceOrientation =
             cameraController.value.deviceOrientation;
 
-        // await cameraController.lockCaptureOrientation(DeviceOrientation.landscapeLeft);
-        await cameraController.lockCaptureOrientation(deviceOrientation);
+        await cameraController.lockCaptureOrientation(
+            _isLocalStation ? DeviceOrientation.portraitUp : deviceOrientation);
       } on CameraException catch (e) {
         PromptBox.toast('Error: ${e.code}\n${e.description}');
       }

+ 26 - 36
lib/pages/id_card_scan/view.dart

@@ -1,5 +1,3 @@
-import 'dart:math';
-
 import 'package:camera/camera.dart';
 import 'package:flutter/material.dart';
 import 'package:get/get.dart';
@@ -24,7 +22,10 @@ class IdCardScanPage extends GetView<IdCardScanController> {
           body: SafeArea(
             child: Obx(
               () => controller.state.isCameraReady
-                  ? _buildCameraArea()
+                  ? Container(
+                      color: Colors.black,
+                      child: _buildCameraArea(),
+                    )
                   : const Center(
                       child: CircularProgressIndicator(
                         valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
@@ -47,13 +48,10 @@ class IdCardScanPage extends GetView<IdCardScanController> {
               return Stack(
                 children: <Widget>[
                   OverflowBox(
-                    maxHeight: constraints.maxHeight,
-                    maxWidth: 2000,
-                    child: Container(
-                      color: Colors.black,
-                      child: Center(
-                        child: _cameraPreviewWidget(),
-                      ),
+                    child: AspectRatio(
+                      aspectRatio:
+                          controller.kCameraController?.value.aspectRatio ?? 1,
+                      child: _cameraPreviewWidget(),
                     ),
                   ),
                   const Center(
@@ -78,32 +76,24 @@ class IdCardScanPage extends GetView<IdCardScanController> {
         child: CircularProgressIndicator(),
       );
     } else {
-      return Transform.rotate(
-        angle: controller.adjustCameraAngle(
-              cameraController,
-              cameraController.description,
-            ) *
-            pi /
-            180,
-        child: Listener(
-          onPointerDown: (_) => controller.pointers++,
-          onPointerUp: (_) => controller.pointers--,
-          child: CameraPreview(
-            cameraController,
-            child: LayoutBuilder(
-              builder: (BuildContext context, BoxConstraints constraints) {
-                return FittedBox(
-                  child: GestureDetector(
-                    behavior: HitTestBehavior.opaque,
-                    onScaleStart: controller.handleScaleStart,
-                    onScaleUpdate: controller.handleScaleUpdate,
-                    onTapDown: (TapDownDetails details) =>
-                        controller.onViewFinderTap(details, constraints),
-                    child: Container(),
-                  ),
-                );
-              },
-            ),
+      return Listener(
+        onPointerDown: (_) => controller.pointers++,
+        onPointerUp: (_) => controller.pointers--,
+        child: CameraPreview(
+          cameraController,
+          child: LayoutBuilder(
+            builder: (BuildContext context, BoxConstraints constraints) {
+              return FittedBox(
+                child: GestureDetector(
+                  behavior: HitTestBehavior.opaque,
+                  onScaleStart: controller.handleScaleStart,
+                  onScaleUpdate: controller.handleScaleUpdate,
+                  onTapDown: (TapDownDetails details) =>
+                      controller.onViewFinderTap(details, constraints),
+                  child: Container(),
+                ),
+              );
+            },
           ),
         ),
       );