【问题标题】:Qr Code show Black preview on second time opening the qr code scanner二维码在第二次打开二维码扫描仪时显示黑色预览
【发布时间】:2022-09-28 20:38:52
【问题描述】:

所以扫描仪只有在第一次打开扫描功能时才能正常工作,当它请求许可时,它会立即打开摄像头,但第二次打开它只显示黑屏,所以我需要将摄像头反转到前置摄像头让它回到后置摄像头以使其再次工作:

这是我在扫描屏幕上的代码:

class Scanner extends StatefulWidget {
  const Scanner({Key key}) : super(key: key);

  @override
  _ScannerState createState() => _ScannerState();
}

class _ScannerState extends State<Scanner> {

  bool _flashOn = false;
  bool _frontCam = false;
  GlobalKey _qrKey = GlobalKey(debugLabel: \'QR\');
  QRViewController _controller;
  String value;
  Barcode result;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          QRView(
              key: _qrKey,
              overlay: QrScannerOverlayShape(borderColor: Color(0xFFE1C884)),
              onQRViewCreated: (QRViewController controller) {
                this._controller = controller;
                controller.scannedDataStream.listen((scanData){
                  print(scanData);
                  if (mounted) {
                    result = scanData;
                    IdCheckController.text = result.code.toString();
                    _controller.dispose();
                    Navigator.pop(context,scanData);
                  }
                });
              }),
          Align(
            alignment: Alignment.topCenter,
            child: Padding(
                padding: EdgeInsets.only(top: 10.h),
                child: Text(
                  \'Scanner\',
                  style: TextStyle(
                      color:
                          // Colors
                          //     .black,
                          Color(0xFFE1C884),
                      fontFamily: \'Saveur\',
                      fontWeight: FontWeight.bold,
                      fontSize: 20.sp),
                )),
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 3.h),
              child: ButtonBar(
                alignment: MainAxisAlignment.spaceBetween,
                children: [
                  IconButton(
                      onPressed: () {
                        setState(() {
                          _flashOn = !_flashOn;
                        });
                        _controller.toggleFlash();
                      },
                      icon: Icon(_flashOn
                          ? Icons.flash_on_rounded
                          : Icons.flash_off_rounded),color: Color(0xFFE1C884),),
                  IconButton(
                      onPressed: () {
                        setState(() {
                          _frontCam = !_frontCam;
                        });
                        _controller.flipCamera();
                      },
                      icon: Icon(_frontCam
                          ? Icons.camera_front_rounded
                          : Icons.camera_rear_rounded),color: Color(0xFFE1C884),),
                  IconButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      icon: Icon(Icons.exit_to_app),color: Color(0xFFE1C884),),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}

这是黑屏的图片:

这是终端日志:

D/ColorViewRootUtil( 8409): nav gesture mode swipeFromBottom ignore false downX 604 downY 384 mScreenHeight 1600 mScreenWidth 720 mStatusBarHeight 36 globalScale 1.3235294 nav mode 3 event MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=604.1609, y[0]=384.75952, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=2170207146, downTime=2170207146, deviceId=2, source=0x1002, displayId=0 } rotation 0
I/BufferQueue( 8409): [unnamed-8409-34](this:0x79d1751000,id:34,api:0,p:-1,c:-1) BufferQueue core=(8409:hotel)
W/hotel( 8409): type=1400 audit(0.0:8938594): avc: denied { read } for name=\"u:object_r:debug_bq_dump_prop:s0\" dev=\"tmpfs\" ino=16455 scontext=u:r:untrusted_app:s0:c34,c258,c512,c768 tcontext=u:object_r:debug_bq_dump_prop:s0 tclass=file permissive=0
E/libc    ( 8409): Access denied finding property \"vendor.debug.bq.dump\"
I/BufferQueueConsumer( 8409): [unnamed-8409-34](this:0x79d1751000,id:34,api:0,p:-1,c:8409) connect(C): consumer=(8409:hotel) controlledByApp=true
E/libc    ( 8409): Access denied finding property \"vendor.debug.bq.dump\"
I/BufferQueueConsumer( 8409): [unnamed-8409-34](this:0x79d1751000,id:34,api:0,p:-1,c:8409) setConsumerName: unnamed-8409-34
E/libc    ( 8409): Access denied finding property \"vendor.debug.bq.dump\"
I/BufferQueueConsumer( 8409): [SurfaceTexture-0-8409-34](this:0x79d1751000,id:34,api:0,p:-1,c:8409) setConsumerName: SurfaceTexture-0-8409-34
D/Surface ( 8409): lockHardwareCanvas
D/Surface ( 8409): Surface::connect(this=0x79c4b29000,api=1)
I/BufferQueueProducer( 8409): [SurfaceTexture-0-8409-34](this:0x79d1751000,id:34,api:1,p:8409,c:8409) connect(P): api=1 producer=(8409:hotel) producerControlledByApp=true
D/mali_winsys( 8409): EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
D/Surface ( 8409): Surface::setBufferCount(this=0x79c4b29000,bufferCount=3)
I/BufferQueueConsumer( 8409): [SurfaceTexture-0-8409-34](this:0x79d1751000,id:34,api:1,p:8409,c:8409) setDefaultBufferSize: width=720 height=1600
D/Surface ( 8409): lockHardwareCanvas
D/TextureView( 8409): getHardwareLayer, createNewSurface:true
I/BufferQueue( 8409): [unnamed-8409-35](this:0x79c4f7a000,id:35,api:0,p:-1,c:-1) BufferQueue core=(8409:.hotel)
E/libc    ( 8409): Access denied finding property \"vendor.debug.bq.dump\"
I/BufferQueueConsumer( 8409): [unnamed-8409-35](this:0x79c4f7a000,id:35,api:0,p:-1,c:8409) connect(C): consumer=(8409:hotel) controlledByApp=true
E/libc    ( 8409): Access denied finding property \"vendor.debug.bq.dump\"
I/BufferQueueConsumer( 8409): [unnamed-8409-35](this:0x79c4f7a000,id:35,api:0,p:-1,c:8409) setConsumerName: unnamed-8409-35
E/libc    ( 8409): Access denied finding property \"vendor.debug.bq.dump\"
I/BufferQueueConsumer( 8409): [SurfaceTexture-0-8409-35](this:0x79c4f7a000,id:35,api:0,p:-1,c:8409) setConsumerName: SurfaceTexture-0-8409-35
I/BufferQueueConsumer( 8409): [SurfaceTexture-0-8409-35](this:0x79c4f7a000,id:35,api:0,p:-1,c:8409) setDefaultBufferSize: width=720 height=1600
D/OpenGLRenderer( 8409): 0x79d19d8da0 setSurfaceTexture: 0x0 to 0x79c4b39400
D/Surface ( 8409): lockHardwareCanvas

    标签: flutter dart


    【解决方案1】:

    您好,我遇到了同样的问题,过了一段时间,我通过添加以下代码找到了解决方案,

    @override
      Widget build(BuildContext context) {
        if (controller != null && mounted) {
          controller!.pauseCamera();
          controller!.resumeCamera();
        }
        return _buildBody();
      }
    
      @override
      void dispose() {
        controller?.dispose();
        controller?.stopCamera();
        super.dispose();
      }
    

    这样每次我重新打开页面时控制器都会首先刷新,每次关闭页面时都会停止相机。我希望这可以帮助你...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多