こんにちは
Plyerのカメラ機能を使って画面にカメラの映像を写したいと思ってます。
自分の考えている方法はカメラクラスを映し出したいクラスにidなどで繋げることかなと考えていますが、どうにも成功しません。
もしわかるかたいましたら、ぜひ情報をお願いします。
python
1from os import getcwd 2from os.path import exists 3from os.path import splitext 4 5import kivy 6 7 8from kivy.app import App 9from kivy.properties import ObjectProperty 10from kivy.uix.floatlayout import FloatLayout 11from kivy.uix.popup import Popup 12from kivy.logger import Logger 13 14from plyer import camera 15 16 17class CameraDemo(FloatLayout): 18 def __init__(self): 19 super(CameraDemo, self).__init__() 20 self.cwd = getcwd() + "/" 21 self.ids.path_label.text = self.cwd 22 23 def do_capture(self): 24 filepath = self.cwd + self.ids.filename_text.text 25 ext = splitext(filepath)[-1].lower() 26 27 if(exists(filepath)): 28 popup = MsgPopup("Picture with this name already exists!") 29 popup.open() 30 return False 31 32 try: 33 camera.take_picture(filename=filepath, 34 on_complete=self.camera_callback) 35 except NotImplementedError: 36 popup = MsgPopup( 37 "This feature has not yet been implemented for this platform.") 38 popup.open() 39 40 def camera_callback(self, filepath): 41 if(exists(filepath)): 42 popup = MsgPopup("Picture saved!") 43 popup.open() 44 else: 45 popup = MsgPopup("Could not save your picture!") 46 popup.open() 47 48 49class CameraDemoApp(App): 50 def __init__(self): 51 super(CameraDemoApp, self).__init__() 52 self.demo = None 53 54 def build(self): 55 self.demo = CameraDemo() 56 return self.demo 57 58 def on_pause(self): 59 return True 60 61 def on_resume(self): 62 pass 63 64 65class MsgPopup(Popup): 66 def __init__(self, msg): 67 super(MsgPopup, self).__init__() 68 self.ids.message_label.text = msg 69 70 71if __name__ == '__main__': 72 CameraDemoApp().run()
kv
1#camerademo.kv 2 3<CameraDemo>: 4 FloatLayout: 5 Label: 6 id: path_label 7 text: 'Working Directory:' 8 pos_hint: {'x': 0.25, 'y': 0.7} 9 size_hint: 0.5, 0.1 10 11 TextInput: 12 id: filename_text 13 text: 'enter_file_name_here.jpg' 14 pos_hint: {'x': 0.25, 'y': .6} 15 size_hint: 0.5, 0.1 16 multiline: False 17 18 Button: 19 text: 'Take picture from camera!' 20 pos_hint: {'x': 0.25, 'y': .3} 21 size_hint: 0.5, 0.2 22 on_press: root.do_capture() 23 24<MsgPopup>: 25 size_hint: .7, .4 26 title: "Attention" 27 28 BoxLayout: 29 orientation: 'vertical' 30 padding: 10 31 spacing: 20 32 33 Label: 34 id: message_label 35 size_hint_y: 0.4 36 text: "Label" 37 Button: 38 text: 'Dismiss' 39 size_hint_y: 0.4 40 on_press: root.dismiss() 41
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/29 03:47
2017/12/29 03:50
2017/12/29 05:19 編集