flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
9.43k stars 360 forks source link

The camera that calls Android in the html rendered by ft.WebView does not respond!!!! #3218

Closed hanchenglong001 closed 1 week ago

hanchenglong001 commented 1 week ago

Description

I tried using webview in an Android app packaged with flet. Then call the camera module in the html page. But it failed.

Code example to reproduce the issue:

test.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>调用相机拍照</title>
</head>
<body>
    <input type="file" accept="image/*" capture="camera" id="cameraInput">
    <img id="previewImage" src="" alt="预览图">

    <script>
        const cameraInput = document.getElementById('cameraInput');
        const previewImage = document.getElementById('previewImage');

        cameraInput.addEventListener('change', (event) => {
            const file = event.target.files[0];
            const reader = new FileReader();

            reader.onload = (e) => {
                previewImage.src = e.target.result;
            };

            reader.readAsDataURL(file);
        });
    </script>
</body>
</html>

main.py

import time

import flet as ft
import threading
from flask import Flask,send_file,render_template

PORT=5000
app = Flask(__name__)

@app.route('/')
def hello_world():

    return render_template(r"html/test.html")

def run_flask_in_thread():
    app.run(host='0.0.0.0', port=PORT)

def main(page: ft.Page):
    page.title = "测试工具"
    flask_thread = threading.Thread(target=run_flask_in_thread)
    flask_thread.start()
    page.navigation_bar = ft.NavigationBar(
        destinations=[
            ft.NavigationDestination(icon=ft.icons.EXPLORE, label="首页")
        ]
    )
    wv = ft.WebView(
        url=f"http://localhost:{PORT}/",
        expand=True,
        on_page_started=lambda _: print("Page start"),
        on_page_ended=lambda _: print("Page ended"),
        on_web_resource_error=lambda e: print("Page error:", e.data),
    )
    page.add(wv)

ft.app(target=main)

I added the AndroidManifest. XML

 <uses-permission android:name="android.permission.CAMERA" />

Describe the results you received: Clicking the button does not turn on the camera properly. But it is call the camera by opening a web page in a mobile browser.

Screenshot_20240506_143637

Operating system:

Windows python3.9 Android12

requirements.txt

flet==0.22.* requests==2.31.0 flask==3.0.3