iOS根据相册图片识别条形码、二维码以及脸部特征
首先介绍一下CIDetector,CIDetector 支持文本,二维码,脸部特征和图片文本的识别.
/* Specifies a detector type for face recognition. */
@available(iOS 5.0, *)
public let CIDetectorTypeFace: String
/* Specifies a detector type for rectangle detection. */
@available(iOS 8.0, *)
public let CIDetectorTypeRectangle: String
/* Specifies a detector type for barcode detection. */
@available(iOS 8.0, *)
public let CIDetectorTypeQRCode: String
/* Specifies a detector type for text detection. */
@available(iOS 9.0, *)
public let CIDetectorTypeText: String
原生识别二维码
// 获取二维码
func WM_FUNC_getQrCode(qrcodeImg:UIImage) -> Void {
if let img:CIImage = CIImage.init(image: qrcodeImg) {
if let qrDetector = CIDetector.init(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh]) {
let reultFeature = qrDetector.features(in: img);
for feature in reultFeature {
if feature.isKind(of: CIQRCodeFeature.self) {
let qrFeature:CIQRCodeFeature = feature as! CIQRCodeFeature
print("qrcode == \(qrFeature.messageString ?? "")")
}else{
print("not CIQRCodeFeature")
}
}
}else{
print("CIDetector init error")
}
}else{
print("CIImage init error")
}
}
原生识别脸部特征
//获取脸部特征
func WM_FUNC_getFace(_ image:UIImage) -> Void {
let imageInput:UIImage = image;
if let cgImage = imageInput.cgImage {
let ciImage:CIImage = CIImage.init(cgImage: cgImage)
let context:CIContext = CIContext.init(options: nil);
let param:[String:Any] = [CIDetectorAccuracy:CIDetectorAccuracyHigh]
if let faceDetector:CIDetector = CIDetector.init(ofType: CIDetectorTypeFace, context: context, options: param) {
let detectResult = faceDetector.features(in: ciImage)
// let resultView:UIView = UIImageView.init(frame: imageInput.size);
// [self.view addSubview:resultView];
for feature in detectResult {
if feature.isKind(of: CIFaceFeature.self){
let faceFeature:CIFaceFeature = feature as! CIFaceFeature;
//左眼
if (faceFeature.hasLeftEyePosition) {
// var leftEyeView:UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 5, height: 5));
// leftEyeView.center = faceFeature.leftEyePosition
// leftEyeView.layer.borderWidth = 1;
// leftEyeView.layer.borderColor = UIColor.red.cgColor;
//// [resultView addSubview:leftEyeView];
}
//右眼
else if (faceFeature.hasRightEyePosition) {
}
//嘴
else if (faceFeature.hasMouthPosition) {
}else{
//整个脸部的位置
}
}
}
}else{
print("CIDetector init error")
}
}else{
print("CIImage init error")
}
}
原生识别条形码
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!")
}
}
网友评论