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.


    How to access VisionKit?

    Pythonista
    help needed objcutil
    5
    43
    14151
    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.
    • cvp
      cvp @JonB last edited by

      @JonB I was busy to post this

      def documentCameraViewController_didFinishWithScan_(_self, _cmd, _controller, _scan):
      	VNDocumentViewController = ObjCInstance(_controller)
      	scan = ObjCInstance(_scan)	# VNDocumentCameraScan
      	#print(scan)
      	VNDocumentViewController.dismissViewControllerAnimated_completion_(True, None)
      
      1 Reply Last reply Reply Quote 0
      • JonB
        JonB @cvp last edited by JonB

        @cvp re: getting the name wrong, in Swift, they omit the "nouns", i think. So the docs show the selectors without nouns. In objc_util it lets you do this too, if you use kwargs.

        Re: dismissing... So did that work? If not, maybe need to on_main_thread it?

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

          scan returns an UIImage

          	print(scan.imageOfPageAtIndex_(0)) 
          
          1 Reply Last reply Reply Quote 0
          • cvp
            cvp @JonB last edited by

            @JonB said:

            documentCameraViewController_didFinishWithScan_

            One more time, thanks for your help.

            I really become too old for this stuff. Shame on me, I'm so sad to have not seen that myself.
            Sorry to all people who tried to help us.😢😪

            1 Reply Last reply Reply Quote 0
            • cvp
              cvp @emkay_online last edited by

              @emkay_online quick an dirty to see the UIImage

              	import ctypes
              	from PIL import Image
              	UIImage = scan.imageOfPageAtIndex_(0)	
              	# Save UIImage to jpg file
              	func = c.UIImageJPEGRepresentation
              	func.argtypes = [ctypes.c_void_p, ctypes.c_float]
              	func.restype = ctypes.c_void_p
              	x = ObjCInstance(func(UIImage.ptr, 1.0))
              	x.writeToFile_atomically_('test.jpg', True)
              	# Display jpg file
              	image = Image.open("test.jpg")
              	image.show()
              
              1 Reply Last reply Reply Quote 0
              • cvp
                cvp @emkay_online last edited by

                @emkay_online operational

                from objc_util import *
                import ui
                load_framework('VisionKit')
                
                def documentCameraViewController_didFinishWithScan_(_self, _cmd, _controller, _scan):
                	VNDocumentViewController = ObjCInstance(_controller)
                	scan = ObjCInstance(_scan)	# VNDocumentCameraSca
                	
                	UIImage = scan.imageOfPageAtIndex_(0)	
                	ObjCInstance(VNDocumentViewController.ui_view['iv']).setImage_(UIImage)
                
                	VNDocumentViewController.dismissViewControllerAnimated_completion_(True, None)
                	
                def documentCameraViewController_didFailWithError_(_self, _cmd, _controller, _error):
                	print('documentCameraViewController_didFailWithError_')
                	
                def documentCameraViewControllerDidCancel_(_self, _cmd, _controller):
                	print('documentCameraViewControllerDidCancel_')
                
                methods = [documentCameraViewController_didFinishWithScan_, documentCameraViewController_didFailWithError_, documentCameraViewControllerDidCancel_]
                protocols = ['VNDocumentCameraViewControllerDelegate']
                try:
                		MyVNDocumentCameraViewControllerDelegate = ObjCClass('MyVNDocumentCameraViewControllerDelegate')
                except:
                	MyVNDocumentCameraViewControllerDelegate = create_objc_class('MyVNDocumentCameraViewControllerDelegate', methods=methods, protocols=protocols)
                
                @on_main_thread	
                def main():
                	VNDocumentCameraViewController = ObjCClass('VNDocumentCameraViewController').alloc().init()
                	delegate = MyVNDocumentCameraViewControllerDelegate.alloc().init()
                	VNDocumentCameraViewController.delegate = delegate	
                
                	uiview = ui.View()
                	iv = ui.ImageView(name='iv')
                	iv.frame = uiview.bounds
                	iv.flex = 'wh'
                	uiview.add_subview(iv)
                	uiview.present('fullscreen')
                	VNDocumentCameraViewController.ui_view = uiview	# to pass to delegate
                	
                	objc_uiview = ObjCInstance(uiview)
                	SUIViewController = ObjCClass('SUIViewController')
                	vc = SUIViewController.viewControllerForView_(objc_uiview)	
                	vc.presentViewController_animated_completion_(VNDocumentCameraViewController, True, None)
                	
                main() 
                
                mikael emkay_online 2 Replies Last reply Reply Quote 0
                • mikael
                  mikael @cvp last edited by

                  @cvp, good stuff. It gives the straightened-out picture, right? Can I get a multi-page PDF, or is the framework for single pictures only?

                  cvp 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @mikael last edited by

                    @mikael no,no, the framework is for multiple scans but my little script not.
                    I think that if you don't dismiss the camera view controller, you could continue to scan multiple images, but I did not test it.

                    1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB last edited by

                      i think the document object guves you UIImages only, but you could create a pdf using some UIKit functions:

                      https://developer.apple.com/documentation/uikit/images_and_pdf?language=objc

                      (you need to acces the functions using c.UIGraphicsBeginPDFContextToFile etc and set argtypes and restype)

                      I think the basic flow is, you have to create a pdf context to a file, begn a pdf page, draw your content (UiImage's drawAtPoint ), begn a new page, etc, then finally end the context.

                      1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp last edited by

                        Do you know that PIL save has an option to save as pdf?

                        pil_image.save('File.pdf',"PDF",resolution=100.0) 
                        
                        stephen JonB 2 Replies Last reply Reply Quote 0
                        • stephen
                          stephen @cvp last edited by

                          @cvp this would of been wonderfull information about 3 months go lol

                          1 Reply Last reply Reply Quote 0
                          • JonB
                            JonB @cvp last edited by

                            @cvp can PIL do multipage PDF? I guess you still would need to do the ui2pil type business.

                            mikael cvp 2 Replies Last reply Reply Quote 0
                            • mikael
                              mikael @JonB last edited by

                              @JonB said:

                              @cvp can PIL do multipage PDF? I guess you still would need to do the ui2pil type business.

                              Which I guess would make it slow in a way that this type of use case does not reslly tolerate.

                              Playing with the idea of a truly personalized doc scanner that would put the scan whete it needs to go. Also, combining this with the text recognition stuff would be good (as soon as they get international characters right).

                              1 Reply Last reply Reply Quote 0
                              • JonB
                                JonB last edited by

                                But the built in objc methods for writing to PDF (see link above) ought to be reasonably fast. I haven't tried.

                                cvp 2 Replies Last reply Reply Quote 0
                                • cvp
                                  cvp @JonB last edited by

                                  @JonB said:

                                  can PIL do multipage PDF? I guess you still would need to do the ui2pil type business.

                                  I don't think it does multi pages, but easy to do it yourself via PdfFileMerger.
                                  Yes for ui2pil

                                  1 Reply Last reply Reply Quote 0
                                  • cvp
                                    cvp @JonB last edited by

                                    @JonB said:

                                    But the built in objc methods for writing to PDF (see link above) ought to be reasonably fast. I haven't tried

                                    I have already done the inverse (PDF -> image) via Objectivec context.. quick 🙄

                                    1 Reply Last reply Reply Quote 0
                                    • emkay_online
                                      emkay_online @cvp last edited by

                                      @cvp Thank you so much for this code. It works a treat and really gives me a solid foundation to build on.

                                      Also thank you for giving me a good grounding in the objc_util module - it's certainly a complex one, especially for someone with no iOS coding experience.

                                      Thank you.

                                      cvp 1 Reply Last reply Reply Quote 0
                                      • cvp
                                        cvp @emkay_online last edited by

                                        @emkay_online you should dismiss the viewcontroller in the didcancel delegate, not in the didfinish like I did for testing only. This if you want to process multiple scans

                                        1 Reply Last reply Reply Quote 0
                                        • cvp
                                          cvp @JonB last edited by cvp

                                          @JonB said:

                                          But the built in objc methods for writing to PDF (see link above) ought to be reasonably fast. I haven't tried.

                                          Please applaus. ok, I exaggerate 😂, but was not so easy (for me)

                                          from objc_util import *
                                          import ui
                                          
                                          UIGraphicsGetCurrentContext = c.UIGraphicsGetCurrentContext
                                          UIGraphicsGetCurrentContext.restype = c_void_p
                                          UIGraphicsGetCurrentContext.argtypes = []
                                          
                                          UIGraphicsBeginPDFContextToData = c.UIGraphicsBeginPDFContextToData
                                          UIGraphicsBeginPDFContextToData.restype = None
                                          UIGraphicsBeginPDFContextToData.argtypes = [c_void_p, CGRect, c_void_p]
                                          
                                          UIGraphicsBeginPDFPage = c.UIGraphicsBeginPDFPage
                                          UIGraphicsBeginPDFPage.restype = None
                                          UIGraphicsBeginPDFPage.argtypes = []
                                          
                                          UIGraphicsEndPDFContext = c.UIGraphicsEndPDFContext
                                          UIGraphicsEndPDFContext.restype = None
                                          UIGraphicsEndPDFContext.argtypes = []
                                          
                                          pdfdata = NSMutableData.alloc()#.init()
                                          ui_image = ui.Image.named('test:Lenna')
                                          uiimage = ObjCInstance(ui_image)
                                          w,h = ui_image.size
                                          frame = CGRect(CGPoint(0, 0), CGSize(w, h))	
                                          UIGraphicsBeginPDFContextToData(pdfdata, frame, None)
                                          UIGraphicsBeginPDFPage()
                                          pdfContext = UIGraphicsGetCurrentContext()
                                          uiimage.drawInRect_(frame)
                                          UIGraphicsEndPDFContext()
                                          # just to be sure that pdfdata is ok
                                          with open('a.pdf',mode='wb') as fil:
                                          	b = nsdata_to_bytes(pdfdata)
                                          	fil.write(b) 
                                          
                                          1 Reply Last reply Reply Quote 1
                                          • JonB
                                            JonB last edited by JonB

                                            👏👏👏

                                            If you are going straight to file, you might consider using

                                            https://developer.apple.com/documentation/uikit/1623927-uigraphicsbeginpdfcontexttofile?language=objc

                                            instead of creating the context as nsdata. Might save a few lines.

                                            Also, you can use drawAtPoint instead of drawInRect, so that you don't need to specify w,h.

                                            I think if you omit the frame when you create the context, it also create an 8.5x11.

                                            cvp 2 Replies Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors