# Opencv crash in android app

**URL:** <https://forum.opencv.org/t/opencv-crash-in-android-app/9970>\
**Category:** Python\
**Tags:** android, programming, kivy\
**Created:** [August 26, 2022, 3:25pm UTC](https://forum.opencv.org/t/opencv-crash-in-android-app/9970 "2022-08-26T15:25:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdul\_Rehman](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/abdul_rehman/32/5927_2.png) [@Abdul\_Rehman](https://forum.opencv.org/u/Abdul_Rehman)\
**Post date:** [August 26, 2022, 3:25pm UTC](https://forum.opencv.org/t/opencv-crash-in-android-app/9970/1 "2022-08-26T15:25:00Z")

</div>

OpenCV camera is working in python but crashes in the Android app when i build it with kivy buildozer.

here is the code and error

```auto
08-26 20:10:44.564 8048 8426 I python : [CRITICAL] [Application] No window is created. Terminating application run.
08-26 20:10:44.605 8048 8426 I python : [WARNING] [Base] Unknown <android> provider
08-26 20:10:44.606 8048 8426 I python : [INFO] [Base] Start application main loop
08-26 20:10:44.609 8048 8426 I python : [ERROR] [Base] No event listeners have been created
08-26 20:10:44.610 8048 8426 I python : [ERROR] [Base] Application will leave
08-26 20:10:44.611 8048 8426 I python : Python for android ended.

```

import time

from kivy.app import App  
from kivy.lang import Builder

from kivy.utils import platform  
import cv2  
from pyzbar import pyzbar

if platform == “android”:  
from android.permissions import request\_permissions, Permission

```
request_permissions([Permission.CAMERA, Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE])

```

time.sleep(2)

class CameraClick:

```
def read_barcodes(self, frame):
    barcodes = pyzbar.decode(frame)
    for barcode in barcodes:
        print(barcodes)
        x, y, w, h = barcode.rect
        # 1
        barcode_info = barcode.data.decode('utf-8')
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        # 2
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, "BC=" + barcode_info, (x + 6, y - 6), font, 2.0, (255, 255, 255),
                    1) # print barcode on camera image
   
        with open("barcode_result.txt", mode='a') as file: # open file for appent
           
            file.write("Recognized Barcode:" + barcode_info + "\n")

    return frame

```

class TestCamera(App):  
if platform == “android”:  
Builder.load\_file(‘android\_layout.kv’)  
else:  
Builder.load\_file(‘layout.kv’)

```
def build(self):
    camera = cv2.VideoCapture(0)
    ret, frame = camera.read()
    # 2
    while ret:
        ret, frame = camera.read()
        print("camera read")
        camera_click = CameraClick()
        frame = camera_click.read_barcodes(frame)
        cv2.imshow('Barcode/QR code reader', frame)
        if cv2.waitKey(1) & 0xFF == 27: # esc key pressed exit
            camera.release()
            cv2.destroyAllWindows()
            break

```

if **name** == “ **main** ”:  
TestCamera().run()

```auto
type or paste code here

```

---

<div class="post-metadata">

**Author:** ![matti.vuori](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@matti.vuori](https://forum.opencv.org/u/matti.vuori)\
**Post date:** [August 27, 2022, 9:05pm UTC](https://forum.opencv.org/t/opencv-crash-in-android-app/9970/2 "2022-08-27T21:05:01Z")

</div>

I don’t know what kivy bulldozer is, but the error does not mention camera, but a window - perhaps the window for imshow.

Anyway, you need to to debug the code and find the exact thing that makes it crash.
