Forum Archive

Camera

DavinE

Hi guys,

I have a problem when using the camera.
I have used the following code which I had from here:

def scanner_app(self, label_text):
        #FUNKTION captureOutput_didOutputMetadataObjects_fromConnection_
        def captureOutput_didOutputMetadataObjects_fromConnection_(_self, _cmd, _output, _metadata_objects, _conn):
            global scannerCode
            objects = ObjCInstance(_metadata_objects)
            for obj in objects:
                s = str(obj.stringValue())
                if s not in scannerCode:
                    scannerCode = s
            scannerView.close()

        global scannerCode
        scannerCode = ''

        AVCaptureSession = ObjCClass('AVCaptureSession')
        AVCaptureDevice = ObjCClass('AVCaptureDevice')
        AVCaptureDeviceInput = ObjCClass('AVCaptureDeviceInput')
        AVCaptureMetadataOutput = ObjCClass('AVCaptureMetadataOutput')
        AVCaptureVideoPreviewLayer = ObjCClass('AVCaptureVideoPreviewLayer')
        dispatch_get_current_queue = c.dispatch_get_current_queue
        dispatch_get_current_queue.restype = c_void_p

        MetadataDelegate = create_objc_class('MetadataDelegate', methods=[captureOutput_didOutputMetadataObjects_fromConnection_], protocols=['AVCaptureMetadataOutputObjectsDelegate'])

        delegate = MetadataDelegate.new()
        scannerView = ui.View(frame=(0, 0, self.WIDTH, self.HEIGHT))
        scannerView.name = 'QR Code Scannen'
        session = AVCaptureSession.alloc().init()
        device = AVCaptureDevice.defaultDeviceWithMediaType_('vide')
        _input = AVCaptureDeviceInput.deviceInputWithDevice_error_(device, None)
        if _input:
            session.addInput_(_input)
        else:
            print('Failed to create input')
            return
        output = AVCaptureMetadataOutput.alloc().init()
        queue = ObjCInstance(dispatch_get_current_queue())
        output.setMetadataObjectsDelegate_queue_(delegate, queue)
        session.addOutput_(output)
        output.setMetadataObjectTypes_(output.availableMetadataObjectTypes())
        prev_layer = AVCaptureVideoPreviewLayer.layerWithSession_(session)
        prev_layer.frame = ObjCInstance(scannerView).bounds()
        prev_layer.setVideoGravity_('AVLayerVideoGravityResizeAspectFill')
        ObjCInstance(scannerView).layer().addSublayer_(prev_layer)
        label = ui.Label(frame=(0, 0, self.WIDTH, 30), flex='W', name='label')
        label.background_color = (0, 0, 0, 0.5)
        label.text_color = '#ffffff'
        label.text = label_text
        label.alignment = ui.ALIGN_CENTER
        scannerView.add_subview(label)
        session.startRunning()
        scannerView.present('fullscreen')
        scannerView.wait_modal()
        session.stopRunning()
        delegate.release()
        session.release()
        output.release()
        scannerView.close()
        return scannerCode 

As you can see in this video:
https://imgur.com/a/VDNN8A1

Crashes the camera immediately call again....
The problem I have with an iPhone 11 Pro IOS 15.6.1 on my other iPhone XS IOS 15.6.1 it goes without problems....

Maybe someone can take a look at the problem and help me.

Thanks in advance!

cvp

@DavinE Perhaps is the camera of both iPhones not exactly the same, or not configured in the same way (ex: jpg or heic).

Perhaps could you protect the line by a try/except

        try:
            s = str(obj.stringValue())
            if s not in scannerCode:
                ...
        except:
            pass
ccc
try:
    xyz
except:
    pass

is a dangerous construction in Python… https://realpython.com/the-most-diabolical-python-antipattern

try:
    xyz
except Exception as e:
    print(repr(e))

might be safer and more enlightening.

DavinE

@cvp, @ccc
Thank you very much, try; except I had not thought 🙈