请! Kivy 应用 Buildozer 可在 Windows 上运行,但不能在 Android 上运行

Posted

技术标签:

【中文标题】请! Kivy 应用 Buildozer 可在 Windows 上运行,但不能在 Android 上运行【英文标题】:Please! Kivy app Buildozer work on windows but not on Android 【发布时间】:2021-11-17 10:06:12 【问题描述】:

我可以在 windows 中运行 main.py...但是当 buildozer 到 apk 时,它不能在 android 中运行。

请帮帮我

这是 main.py

from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.toast import toast
from kivy.properties import ObjectProperty
from kivy.uix.camera import Camera
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.core.window import Window
import cv2
from plyer import filechooser

class Dsmenu(MDBoxLayout):
    manager = ObjectProperty()
    menu = ObjectProperty()   
class Chupanh(Screen):
    def switch_camera(self):
        try:
            self.ids.camera.index = (0 if self.ids.camera.index == 1 else 1)
        except AttributeError:
            toast("Không tìm thấy camera")
            self.ids.camera.index = (0 if self.ids.camera.index == 1 else 1)
class Phantichdv(Screen):
    pass
class Anhluu(Screen):
    pass
sm = ScreenManager()
sm.add_widget(Chupanh(name ='chupanh'))
sm.add_widget(Phantichdv(name ='phantichdv'))
sm.add_widget(Anhluu(name ='anhluu'))
class FingerPrintApp(MDApp):       
    def build(self):
        self.theme_cls.primary_palette = "DeepPurple" 
        self.theme_cls.theme_style = "Dark"

    def img_reload(self):
        self.root.ids.manager.get_screen('phantichdv').ids.image_shot.reload()
    def capture(self):          
        self.camera = self.root.ids.manager.get_screen('chupanh').ids.camera       
        while self.camera.export_to_png("capture_img.png"):         
            break
        self.root.ids.manager.get_screen('chupanh').ids.camera.play = False
        self.root.ids.manager.get_screen('phantichdv').ids.image_shot.source = "capture_img.png"
        self.root.ids.manager.get_screen('phantichdv').ids.image_shot.reload()
    def enhance_img(self):                 
        pass       
        
    def phantich_img(self):
        self.root.ids.manager.get_screen('phantichdv').ids.image_shot.source = "phantich_img.png"

    def show_save_img(self):
        self.root.ids.manager.get_screen('anhluu').ids.image_show.source = "phantich_img.png"
        self.root.ids.manager.get_screen('anhluu').ids.image_show.reload()
        
    def file_manager_open(self):
        raw_path = filechooser.open_file(title="Chọn ảnh chứa dấu vết đường vân",
                                                                filters=[("All", "*.*"),("JPEG", "*.jpg"),("PNG", "*.png"),("TIF", "*.tif"),("Bitmap", "*.bmp")])
        try:
            self.img = cv2.imread(raw_path[0])            
            # while cv2.imwrite("capture_img.png",self.img):
            while cv2.imwrite("capture_img.png",self.img):    
                break
            self.root.ids.manager.get_screen('chupanh').ids.camera.play = False
            # self.root.ids.manager.get_screen('phantichdv').ids.image_shot.source = "capture_img.png"
            self.root.ids.manager.get_screen('phantichdv').ids.image_shot.source = "capture_img.png"
            self.root.ids.manager.get_screen('phantichdv').ids.image_shot.reload()                        
        except IndexError:
            toast("Bạn chưa chọn file",duration=2)   
            
FingerPrintApp().run()

这是指纹.kv

MDBoxLayout:
    id:screen
    orientation: 'vertical'    
    MDToolbar:
        id: toolbar
        title: 'FingerPrint APP'
        pos_hint: "top": 1
        anchor_title: 'left' 
        elevation: 10                
        right_action_items: [["menu", lambda x: menu.set_state("open")]]
    MDNavigationLayout:
        orientation: 'vertical'     
        ScreenManager:
            id:manager        
            Chupanh:        
            Phantichdv: 
            Anhluu:
        MDNavigationDrawer:            
            id: menu
            Dsmenu:
                manager: manager
                menu: menu 
<Dsmenu>:
    name: "menu"
    orientation: "vertical"
    # padding: "10dp"
    spacing: "10dp" 
    BoxLayout:
        orientation: "vertical"
        size: "310dp", "40dp"
        size_hint: None, None  
        # Image:
        #     size: "300dp", "200dp"
        #     pos_hint: 'center_x': 0.5 , 'top': 1
        #     source: 
    MDLabel:
        text: "   Tác giả: VĂN VŨ THIÊN - PC09 Quảng Nam"
        font_style: "Subtitle2"
        size_hint_y: None
        height: 10
    MDLabel:
        text: "   Email: vanvuthien@gmail.com"
        font_style: "Caption"
        size_hint_y: None
        height: 10
    MDLabel:
        text: "   SĐT: 033.5951.464"
        font_style: "Caption"
        size_hint_y: None
        height: 10
    ScrollView:
        MDList:
            OneLineIconListItem:
                text: "CHỌN ẢNH"
                on_release:
                    root.menu.set_state("close")
                    root.manager.current = "chupanh"
                    root.manager.get_screen('chupanh').ids.camera.play = True
                IconLeftWidget:
                    icon: "image-search-outline"                
            OneLineIconListItem:
                text: "PHÂN TÍCH DẤU VẾT"
                on_release:
                    root.menu.set_state("close")
                    root.manager.current = "phantichdv"
                    root.manager.get_screen('chupanh').ids.camera.play = False 
                IconLeftWidget:
                    icon: "fingerprint"
            OneLineIconListItem:
                text: "ẢNH ĐÃ PHÂN TÍCH"
                on_release:
                    root.menu.set_state("close")
                    root.manager.current = "anhluu"
                    root.manager.get_screen('chupanh').ids.camera.play = False 
                IconLeftWidget:
                    icon: "content-save-all"
<Chupanh>:
    name: 'chupanh'         
    Camera:
        index: 0
        id: camera
        resolution: (600, 800)
        play: True                
        pos_hint: 'center_x': .5, 'center_y': .5  
    MDIconButton:                            
        icon: 'image-search-outline' 
        md_bg_color: app.theme_cls.primary_color 
        user_font_size: "18sp"
        elevation: 10           
        pos_hint: 'center_x': 0.2 , 'center_y': 0.1
        on_release: 
            app.file_manager_open()
            root.manager.current = "phantichdv"
            root.manager.transition.direction = "left"
                
    MDFillRoundFlatIconButton: 
        text: 'Chụp ảnh'       
        icon: 'camera-iris' 
        elevation: 10              
        pos_hint: 'center_x': 0.5 , 'center_y': 0.1
        on_release: 
            app.capture()
            root.manager.current = "phantichdv"
            root.manager.transition.direction = "left"
    MDIconButton:                            
        icon: 'camera-flip-outline' 
        md_bg_color: app.theme_cls.primary_color 
        user_font_size: "18sp"
        elevation: 10              
        pos_hint: 'center_x': 0.8 , 'center_y': 0.1
        on_release: root.switch_camera()  
<Phantichdv>:
    name: "phantichdv"
    Image:
        id: image_shot
        elevation: 10
        pos_hint: 'center_x': 0.5, 'center_y': 0.5 
    MDLabel:
        text: "Thông số:"
        pos_hint: 'x': 0.05 , 'center_y': 0.95
    MDSlider:
        id: slider 
        size_hint_x: 0.4
        min: 0
        max: 100
        pos_hint: 'x': 0.03 , 'center_y': 0.90
    MDLabel:
        text: "Âm bản:"
        pos_hint: 'x': 0.45 , 'center_y': 0.95
    MDSwitch:
        id: amban
        pos_hint: 'x': 0.45 , 'center_y': 0.90
        active: False
    MDRaisedButton:        
        text: 'Xử lý'                                   
        pos_hint: 'x': 0.7 , 'center_y': 0.92
        elevation: 10             
        on_release:
            app.enhance_img() 
    MDIconButton:                            
        icon: 'keyboard-backspace' 
        md_bg_color: app.theme_cls.primary_color 
        user_font_size: "18sp"
        elevation: 10              
        pos_hint: 'center_x': 0.2 , 'center_y': 0.1
        on_release: 
            root.manager.current = "chupanh"
            root.manager.transition.direction = "right"
            root.manager.get_screen('chupanh').ids.camera.play = True
    MDFillRoundFlatIconButton:        
        icon: 'fingerprint'
        text: 'Phân tích'                
        pos_hint: 'center_x': 0.5 , 'center_y': 0.1
        on_release: app.phantich_img() 
    MDIconButton:                            
        icon: 'content-save-all-outline' 
        md_bg_color: app.theme_cls.primary_color 
        theme_text_color: "Custom"
        user_font_size: "18sp"
        elevation: 10 
        text_color: 1,1,1,1              
        pos_hint: 'center_x': 0.8 , 'center_y': 0.1
        on_release:
            app.show_save_img() 
            root.manager.current = "anhluu"
            root.manager.transition.direction = "left"
    MDProgressBar:
        id: prog
        type: 'indeterminate'
        pos_hint: 'center_x': 0.5 , 'center_y': 0.02 
<Anhluu>:
    name: "anhluu" 
    Image:
        id: image_show  
        pos_hint: 'top': 1 
    MDIconButton:                            
        icon: 'camera-iris' 
        md_bg_color: app.theme_cls.primary_color 
        user_font_size: "18sp"
        elevation: 10            
        pos_hint: 'center_x': 0.75 , 'center_y': 0.1
        on_release: 
            root.manager.get_screen('chupanh').ids.camera.play = True
            root.manager.current = "chupanh"
            root.manager.transition.direction = "right"
    MDIconButton:                            
        icon: 'keyboard-backspace' 
        md_bg_color: app.theme_cls.primary_color 
        user_font_size: "18sp"
        elevation: 10              
        pos_hint: 'center_x': 0.25 , 'center_y': 0.1
        on_release: 
            root.manager.current = "phantichdv"
            root.manager.transition.direction = "right"

我用

制作了 buildozer.spec

要求 = python3,opencv-python,plyer,kivy==2.0.0,https://github.com/kivymd/KivyMD/archive/master.zip

android.permissions = CAMERA、WRITE_EXTERNAL_STORAGE、READ_EXTERNAL_STORAGE

日志猫:

    09-24 15:13:58.661  1330  1365 I python  :  Traceback (most recent call last):
09-24 15:13:58.661  1330  1365 I python  :    File "/content/.buildozer/android/app/main.py", line 10, in <module>
09-24 15:13:58.661  1330  1365 I python  :    File "/content/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/easy/cv2/__init__.py", line 5, in <module>
09-24 15:13:58.661  1330  1365 I python  :  ImportError: dlopen failed: "/data/data/org.test.easy/files/app/_python_bundle/site-packages/cv2/cv2.so" is 64-bit instead of 32-bit
09-24 15:13:58.661  1330  1365 I python  : Python for android ended.
09-24 15:13:58.878  2089  3881 I ActivityManager: Process org.test.easy (pid 1330) has died: fore TOP
09-24 15:13:58.910  2089  2191 W ActivityManager: setHasOverlayUi called on unknown pid: 1330

【问题讨论】:

【参考方案1】:

您面临的错误是由于您的应用程序架构和 cv2 构建相矛盾。将您的 opencv 要求从 opencv-python 更改为 opencv。 所以你的要求应该是

requirements = python3,opencv,plyer,kivy==2.0.0,https://github.com/kivymd/KivyMD/archive/master.zip

然后在你的项目目录中运行buildozer android clean。现在,如果您重新编译并运行您的应用程序应该可以正常工作。

【讨论】:

非常感谢。但我遇到了一个新错误:jnius.jnius.JavaException: JVM exception occurred: Fail to connect to camera service java.lang.RuntimeException 您需要在 buildozer.spec 文件中以及在实例化相机类之前的运行时请求相机权限。在这里查看更多信息。 github.com/Android-for-Python/CameraXF-Example/blob/…

以上是关于请! Kivy 应用 Buildozer 可在 Windows 上运行,但不能在 Android 上运行的主要内容,如果未能解决你的问题,请参考以下文章

kivy buildozer 虚拟机

Python/Kivy/Buildozer - 应用程序在 Android 中崩溃,但在 PC 上运行良好

使用 buildozer 在 android 上部署 kivy 应用程序时出错

Buildozer 构建 Android Kivy 应用程序返回命令失败

如何使用 kivy-buildozer 为我的 android 应用程序添加图标?

Kivy 和 buildozer “权限被拒绝”