Advertisement
A_GUES

Python Good camera

Sep 1st, 2023
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. from plyer import camera
  2. from kivy.app import App
  3. from kivy.uix.button import Button
  4. from kivy.uix.camera import Camera
  5. from kivy.uix.boxlayout import BoxLayout
  6.  
  7. class CameraApp(App):
  8.     def build(self):
  9.         # create a layout
  10.         layout = BoxLayout()
  11.        
  12.         # create a camera widget
  13.         cam = Camera(index=0, resolution=(640,480))
  14.        
  15.         # create a button that will take a picture
  16.         button = Button(text='Take Picture')
  17.        
  18.         # bind the button's on_press event to a callback function
  19.         button.bind(on_press=self.take_picture)
  20.        
  21.         # add the camera and the button to the layout
  22.         layout.add_widget(cam)
  23.         layout.add_widget(button)
  24.        
  25.         return layout
  26.    
  27.     def take_picture(self, *args):
  28.         # use plyer.camera.take_picture to take a picture
  29.         # pass a filename where to save the picture
  30.         # pass a callback function that will be called after taking picture
  31.         camera.take_picture(filename='my_picture.jpg', on_complete=self.picture_taken)
  32.    
  33.     def picture_taken(self, obj):
  34.         # this function will be called after taking picture
  35.         # obj is either the filename where picture was saved or None if error occurred
  36.         if obj:
  37.             print('Picture saved at ' + obj)
  38.         else:
  39.             print('Error taking picture')
  40.  
  41. # run the app
  42. CameraApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement