How to access VisionKit?
I'm just dipping my toes into objc_util and have fallen at the first hurdle.
I want to write a small script that calls VisionKit to scan a document and return the result to Pythonista. API is here: https://developer.apple.com/documentation/visionkit
I've started with:
from objc_util import * VNDocumentCameraViewController = ObjCClass('VNDocumentCameraViewController')
But Pythonista says that the class VNDocumentCameraViewController doesn't exist.
I'd love some help getting past this (and if anyone could take the example further, that would be amazing).
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
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 🙄
@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.
@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
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)
If you are going straight to file, you might consider using
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.
so that you don't need to specify w,h.
Yes but UIGraphicsBeginPDFContextToData needs it also
Édit: sorry, written before reading your last line
I don't say it is the best way, it was only an exercice for me.
instead of creating the context as nsdata.
I added the file lines only to test /check pdfdata.