omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Camera

    Pythonista
    4
    4
    501
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DavinE
      DavinE last edited by

      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 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @DavinE last edited by 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
        
        1 Reply Last reply Reply Quote 0
        • ccc
          ccc last edited by

          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.

          1 Reply Last reply Reply Quote 1
          • DavinE
            DavinE last edited by

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors