Opening android front camera

560 views
Skip to first unread message

Gaurav Bhoi

unread,
Jun 13, 2021, 7:20:24 AM6/13/21
to Kivy users support
Hey guys, I m making a camera application for android so how can I toggle between front and rear camera. please help me thank you

Degenerate Tech

unread,
Jun 13, 2021, 7:58:31 AM6/13/21
to Kivy users support
Watch this video .
https://youtu.be/QWShS_BnQjQ

Gaurav Bhoi

unread,
Jun 14, 2021, 6:59:48 AM6/14/21
to kivy-...@googlegroups.com
I followed the instructions correctly but i still got some errors please help thank you. I m trying to start front camera

Screenshot from 2021-06-14 16-25-06.png

python code:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics.texture import Texture
from kivy.uix.camera import Camera
import numpy as np
import cv2
from kivy.properties import ObjectProperty
from jnius import autoclass
from android.permissions import request_permissions, Permission

camerax = autoclass('android.hardware.Camera')
CameraInfo = autoclass('android.hardware.Camera$CameraInfo')

class AndroidCamera(Camera):
    camera_resolution = (1280, 720)
    index = CameraInfo.CAMERA_FACING_FRONT

    def _camera_loaded(self, *largs):
        self.texture = Texture.create(size=np.flip(self.camera_resolution), colorfmt='rgb')
        self.texture_size = list(self.texture.size)

    def on_tex(self, *l):
        if self._camera._buffer is None:
            return None
        frame = self.frame_from_buf()

        self.frame_to_screen(frame)
        super(AndroidCamera, self).on_tex(*l)

    def frame_from_buf(self):
        w, h = self.resolution
        frame = np.frombuffer(self._camera._buffer.tostring(), 'uint8').reshape((h + h // 2, w))
        frame_bgr = cv2.cvtColor(frame, 93)
        return np.rot90(frame_bgr, 3)

    def frame_to_screen(self, frame):
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        flipped = np.flip(frame_rgb, 0)
        print(flipped)
        buf = flipped.tostring()
        self.texture.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')

class Box(BoxLayout):
    pass

class MainApp(App):
    def build(self):
        request_permissions([Permission.CAMERA])
        return Box()


if __name__ == '__main__':
    MainApp().run()

kv code:

<Box>:
    orientation: 'vertical'
    size: root.width, root.height

    AndroidCamera:
        resolution: self.camera_resolution
        allow_stretch: True
        play: True

        canvas.before:
            PushMatrix
            Rotate:
                angle: 180
                origin: self.center

        canvas.after:
            PopMatrix

--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/TQo_JZN2oSQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/850f694f-84a5-45f6-86a0-3a6263c37316n%40googlegroups.com.

Degenerate Tech

unread,
Jun 14, 2021, 9:53:24 AM6/14/21
to Kivy users support
upload the logs in file

Gaurav Bhoi

unread,
Jun 14, 2021, 4:19:18 PM6/14/21
to kivy-...@googlegroups.com
I m attaching log file down below as well as spec file

my_log.txt
buildozer.spec

Gaurav Bhoi

unread,
Jun 15, 2021, 9:42:50 AM6/15/21
to kivy-...@googlegroups.com
guys i need some guidance should i upload all related files and codes. please help me
 
Reply all
Reply to author
Forward
0 new messages