Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to load camera in webview_flutter

I am using webview_flutter: ^0.3.15+1 for in-app browser in flutter(here) and want to open this URL check which have a camera but it's not opening as expected nor camera is loaded on that webpage

Code for webView Page

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'dart:async';

class ExamWebView extends StatefulWidget {
  final PageController controller;
  final int destination;

  const ExamWebView({Key key, this.controller, Key index, this.destination})
      : super(key: key);
  @override
  _ExamWebViewState createState() => _ExamWebViewState();
}

class _ExamWebViewState extends State<ExamWebView> {
  Completer<WebViewController> _controller = Completer<WebViewController>();
  JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
    return JavascriptChannel(
        name: '_Toaster',
        onMessageReceived: (JavascriptMessage message) {
          Scaffold.of(context).showSnackBar(
            SnackBar(content: Text(message.message)),
          );
        });
  }

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Center(
            child: Text("Exam"),
          ),
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: WebView(
                initialUrl:
                    "https://therealtechwiz.github.io/project/facerecognition",
                gestureRecognizers: Set()
                  ..add(
                    Factory<VerticalDragGestureRecognizer>(
                      () => VerticalDragGestureRecognizer(),
                    ),
                  ),
                // Factory<VerticalDragGestureRecognizer>(
                //  () => VerticalDragGestureRecognizer()..onUpdate = (_) {},
                // ),
                initialMediaPlaybackPolicy: AutoMediaPlaybackPolicy
                    .require_user_action_for_all_media_types,
                javascriptMode: JavascriptMode.unrestricted,
                onWebViewCreated: (WebViewController webViewController) {
                  _controller.complete(webViewController);
                },
                javascriptChannels: <JavascriptChannel>[
                  _toasterJavascriptChannel(context),
                ].toSet(),
              ),
            )
          ],
        ));
  }
}

Added these Permissions in android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.INTERNET"/>

response in terminal:

D/EgretLoader(23485): EgretLoader(Context context)
D/EgretLoader(23485): The context is not activity
W/ContentCatcher(23485): Failed to notify a WebView
I/chromium(23485): [INFO:CONSOLE(79)] "[object DOMException] please use the fiddle instead", source: https://therealtechwiz.github.io/project/facerecognition/script.js (79)
I/chromium(23485): [INFO:CONSOLE(1)] "Uncaught (in promise) Error: failed to fetch: (404) , from url: https://therealtechwiz.github.io/models/face_recognition_model-weights_manifest.json", source: https://therealtechwiz.github.io/project/facerecognition/face-api.min.js (1)
W/Choreographer(23485): Frame time is 0.077364 ms in the future!  Check that graphics HAL is generating vsync timestamps using the correct timebase.

Getting this output :

enter image description here

Ouput expected : can be seen here

enter image description here

can anyone please see and suggest me a solution to fix

like image 993
Tarun Jain Avatar asked Oct 27 '25 12:10

Tarun Jain


2 Answers

This should be it:

_controller = WebViewController(
  onPermissionRequest: (request) => request.grant(),
)
like image 65
Shareef Hadid Avatar answered Oct 30 '25 02:10

Shareef Hadid


To access camera in webview_flutter:

  1. Add the camera permission in AndroidManifest.xml:
     <uses-permission android:name="android.permission.CAMERA"/>
    
  2. Request camera permission with permission_handler:
    Future<void> requestCameraPermission() async {
       final status = await Permission.camera.request();
         if (status == PermissionStatus.granted) {
         // Permission granted.
         } else if (status == PermissionStatus.denied) {
         // Permission denied.
         } else if (status == PermissionStatus.permanentlyDenied) {
         // Permission permanently denied.
      }
    }
    
    
  3. Create WebViewController with fromPlatformCreationParams and then grant the camera permission in onPermissionRequest callback:
    late final PlatformWebViewControllerCreationParams params;
    params = const PlatformWebViewControllerCreationParams();
    
    WebViewController controller = WebViewController.fromPlatformCreationParams(
      params,
      onPermissionRequest: (WebViewPermissionRequest request) {
        request.grant();
      },
    )
    
like image 39
yushulx Avatar answered Oct 30 '25 02:10

yushulx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!