【iOS开发】相册选择图片识别条形码
2021-09-14 本文已影响0人
WMSmile
【iOS开发】相册选择图片识别条形码
原生识别条形码
VNDetectBarcodesRequest 只要iOS11.0+以上支持,支持iOS11.0-以下请使用其他方案
// Create a barcode detection-request
let barcodeRequest = VNDetectBarcodesRequest(completionHandler: { request, error in
guard let results = request.results else { return }
// Loopm through the found results
for result in results {
// Cast the result to a barcode-observation
if let barcode = result as? VNBarcodeObservation {
// Print barcode-values
print("Symbology: \(barcode.symbology.rawValue)")
// Print barcode string
if let payload = barcode.payloadStringValue {
print("Payload: \(payload)")
}
if let desc = barcode.barcodeDescriptor as? CIQRCodeDescriptor {
let content = String(data: desc.errorCorrectedPayload, encoding: .utf8)
// FIXME: This currently returns nil. I did not find any docs on how to encode the data properly so far.
print("Payload: \(String(describing: content))")
print("Error-Correction-Level: \(desc.errorCorrectionLevel)")
print("Symbol-Version: \(desc.symbolVersion)")
}
}
}
})
// 获取条形码
func WM_FUNC_getInfoByImage() -> Void {
let myImage = UIImage.init(named: "1631511918532.jpg");
// Create an image handler and use the CGImage your UIImage instance.
guard let image = myImage?.cgImage else { return }
let handler = VNImageRequestHandler(cgImage: image, options: [:])
// Perform the barcode-request. This will call the completion-handler of the barcode-request.
guard let _ = try? handler.perform([barcodeRequest]) else {
return print("Could not perform barcode-request!")
}
}
使用其他第三方来识别条形码
这里介绍两种第三方,ZXing和MLKit两种方式,当然,其他的第三方可以自己去尝试。
第一种、使用 ZXing 来识别图片的二维码
zxingify-objc - GitHub地址 可以去GitHub查看
1、使用pods集成
pod 'ZXingObjC', '~> 3.6.4'
2、github有OC代码,这里只贴出swift 代码
// 使用ZXing
func WM_FUNC_barcode_zxing() -> Void {
//自行更改图片传入
if let inputImage = UIImage.init(named: "1631511918532.jpg"){
let cgImage = inputImage.cgImage;
let source = ZXCGImageLuminanceSource.init(cgImage: cgImage);
let bitmap = ZXBinaryBitmap.init(binarizer: ZXHybridBinarizer.init(source: source))
if let reader:ZXMultiFormatReader = ZXMultiFormatReader.reader() as? ZXMultiFormatReader {
do {
let hints = ZXDecodeHints.hints();
let result = try reader.decode(bitmap,hints: hints as? ZXDecodeHints)
let contents = result.text
// The barcode format, such as a QR code or UPC-A
let format = result.barcodeFormat
print("contents == \(contents ?? "") format = \(format.rawValue)")
} catch let error as NSError {
print(error)
}
}else{
print("ZXMultiFormatReader init error")
}
}else{
print("inputImage init error")
}
}
第二种、使用 MLKit 来识别图片的二维码
谷歌官方地址文档GoogleMLKit之 barcode-scanning 需要梯子,请自备梯子。
1、使用pods集成
pod 'GoogleMLKit/BarcodeScanning', '2.3.0'
2、github有OC和swift代码,这里只贴出swift 代码
//条形码
func WM_FUNC_barcode() -> Void {
//自行更改图片传入
if let inputImage = UIImage.init(named: "1631511918532.jpg"){
let visionImage = VisionImage(image: inputImage)
visionImage.orientation = inputImage.imageOrientation
let barcodeScanner = BarcodeScanner.barcodeScanner()
barcodeScanner.process(visionImage) { features, error in
guard error == nil, let features = features, !features.isEmpty else {
// Error handling
return
}
// Recognized barcodes
for barcode in features {
print("barcode = " + (barcode.rawValue ?? ""))
// let corners = barcode.cornerPoints
// let displayValue = barcode.displayValue
// let rawValue = barcode.rawValue
// let valueType = barcode.valueType
// switch valueType {
// case .wiFi:
// let ssid = barcode.wifi?.ssid
// let password = barcode.wifi?.password
// let encryptionType = barcode.wifi?.type
// case .URL:
// let title = barcode.url!.title
// let url = barcode.url!.url
// default:
// // See API reference for all supported value types
// print(barcode.url)
// print("11223344");
// }
}
}
}
}