I have to learn a barcode from a PDF doc. It’s created from knowledge acquired through a Share Extension.
First, I attempt to parse every PDF web page right into a UIImage. If nothing is discovered, I attempt to “cut up” every web page vertically into two and check out detecting once more.
The issue is that it really works on some iPhone fashions, however there are different iPhone fashions the place it would not work in any respect.
It really works fantastic on iPhone 7 and iPhone 14 Professional Max, however would not work on iPhone 6 and iPhone 11.
What am I doing mistaken?
extension PDFPage {
// MARK: - Public Strategies
func renderToImages() -> [UIImage] {
let topImage = generateHelfedImage(cutype: .high)
let bottomImage = generateHelfedImage(cutype: .bot)
return [
topImage ?? UIImage(),
bottomImage ?? UIImage()
]
}
func renderToImage() -> UIImage {
let pageBounds = self.bounds(for: .mediaBox)
let fullImage = generateImage(top: pageBounds.measurement.top)
return fullImage ?? UIImage()
}
// MARK: - Personal Strategies
non-public enum CutType {
case high
case bot
}
non-public func generateHelfedImage(cutype: CutType) -> UIImage? {
let pageBounds = self.bounds(for: .mediaBox)
let render = UIGraphicsImageRenderer(measurement: CGSize(
width: pageBounds.width,
top: cutype == .high ? pageBounds.top / 2 : pageBounds.top)
)
let imageBounds = CGRect(x: 0,
y: 0,
width: pageBounds.measurement.width,
top: pageBounds.measurement.top)
let picture = render.picture { context in
UIColor.white.set()
context.fill(imageBounds)
context.cgContext.translateBy(x: 0, y: cutype == .bot ? pageBounds.top / 2 : pageBounds.top)
context.cgContext.scaleBy(x: 1, y: -1)
draw(with: .mediaBox, to: context.cgContext)
}
return picture.resizeTo(targetSize: pageBounds.measurement, scale: picture.scale)
}
non-public func generateImage(top: CGFloat) -> UIImage? {
let pageBounds = self.bounds(for: .mediaBox)
let renderSize = CGSize(
width: pageBounds.width,
top: pageBounds.top
)
let imageBounds = CGRect(
x: 0,
y: 0,
width: renderSize.width,
top: renderSize.top
)
let render = UIGraphicsImageRenderer(measurement: renderSize)
let picture = render.picture { context in
UIColor.orange.set()
context.fill(imageBounds)
context.cgContext.translateBy(x: 0, y: top)
context.cgContext.scaleBy(x: 1, y: -1)
draw(with: .mediaBox, to: context.cgContext)
}
return picture
}
}
extension PDFDocument {
func convertToImages(halfPages: Bool) -> [UIImage] {
var photos: [UIImage] = []
for pageIndex in 0..
enum VNRequestType: CaseIterable {
case code
func request(completion: @escaping([T]?) -> Void) -> VNRequest {
swap self {
case .code:
let request = VNDetectBarcodesRequest { request, error in
guard error == nil else {
return completion(nil)
}
var observations: [VNBarcodeObservation] = []
for lead to request.outcomes ?? [] {
if let statement = consequence as? VNBarcodeObservation {
observations.append(statement)
}
}
if observations.isEmpty {
return completion(nil)
}
let findedCodes = observations.map { ($0.payloadStringValue, $0.symbology) }
return completion(findedCodes as? [T])
}
request.symbologies = [
.upce,
.code39,
.code93,
.code128,
.ean8,
.ean13,
.aztec,
.pdf417,
.itf14,
.dataMatrix
]
return request
}
}
}
extension UIImage {
func resizeTo(targetSize: CGSize, scale: CGFloat = 1.0) -> UIImage {
let currentSize = self.measurement
let widthRatio = targetSize.width / currentSize.width
let heightRatio = targetSize.top / currentSize.top
var newSize: CGSize
if widthRatio > heightRatio {
newSize = CGSize(width: measurement.width * heightRatio, top: measurement.top * heightRatio)
} else {
newSize = CGSize(width: measurement.width * widthRatio, top: measurement.top * widthRatio)
}
let rect = CGRect(x: 0, y: 0, width: newSize.width, top: newSize.top)
UIGraphicsBeginImageContextWithOptions(newSize, false, scale)
self.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage ?? UIImage()
}
func detect(
content material sort: [T].Sort,
with requests: [VNRequestType],
completion: @escaping([T]?) -> Void
) throws {
guard let cgImage = self.cgImage else { return }
let handler = VNImageRequestHandler(cgImage: cgImage)
let request = requests.map { $0.request(completion: completion) }
strive handler.carry out(request)
}
}
I attempted to make use of thumbnail too