在第17天,做了一个ISBN 查询 书本的书名、书本描述
那麽今天我希望做一个使用拍照扫描辨识出ISBN码,这样就不用手动输入ISBN码
import Vision
主要是透过apple 发布的Vision套件来辨识
透过Vision套件来做也需要一个相机的View来使用Vision套件
使用AVFoundation套件来建立相机View
// 使用AVFoundation建立相机View
import AVFoundation
func checkPermission(){
let mediaType = AVMediaType.video
// authorizationStatus回传一个常数看是否能够使用某个(mediaType)类型
let status = AVCaptureDevice.authorizationStatus(for: mediaType)
switch status{
case .denied,.restricted:
// 如果不能使用,则会出现提示来要求权限
let Noauthorized = UIAlertController(title:"Error", message: "", preferredstyle: .alert)
Noauthorized.addAction(UIAlertAction(title:"Set", style: .default){
let setting = URL(string:UIApplication.openSettingsURLString)!
UIApplication.shared.open(setting)
}
default:
break
}
}
ConfigLiveView(){
func ConfigLiveView(){
// 最重要的部分,Session负责管理Capture & DataFlow
captureSession = AVCaptureSession()
// 规范Capture的资料要是什麽解析度
captureSession.sessionPresent = .hd1920x1080
// 开始Capture Device
// 使用DiscoverySession来将符合资料的Device资料储存
let deviceDiscoverySession = AVCaptureDevice.DiscoverytSession(deviceTypes:[AVCaptureDevice.DeviceType.builtInDualCamera],mediaType:AVMediaType.video, position: .back)
// 所有符合资料的Device
let devices = deviceDiscoverySession.devices
for device in devices{
if device.position == AVCaptureDevice.Postion.back{
// 将符合资料的储存进backCamera内
backCamera = device
}
}
// 设定Input & Output
do{
// 让AVCaptureDeviceInput类的资料为backCamera储存至captureDeviceInput
let captureDeviceInput = try AVCaptureDeviceInput(device:backCamera)
captureSession.addInput(captureDeviceInput)
}catch{
showAlert(withTitle:"Camera Error")
return
}
catureOutput = AVCapturePhotoOutput()
// 设定好拍摄设定
captureOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSetting(format:[AVVideoCodeKey:AVVideoCodecType.jpeg)
captureSession.addOuptut(captureOutput!)
// 增加Preview Layer
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session:captureSession)
cameraPreviewLayer?.videoGravity = .resizeAspectFill
cameraPreviewLayer?.connection.videoOrientation = .portrait
cameraPreviewLayer?.frame = view.frame
// The sublayer to be inserted into the current layer.
self.view.layer.insertlayer(cameraPreviewLayer!,at:0)
captureSession.startRunning()
}
addButton()
func addButton(){
let width:CGFloat = 75
let height = width
button = UIButton(frame:CGRect(x: (view.frame.width - width)/2, y: view.frame.height - height - 32, width: width, height: height))
button.backgroundColor = UIColor.white
button.showsTouchWhenHighlighted = true
button.addTarget(self,action: #selector(captureImage), for: .touchUpInside)
view.addSubView(button)
}
captureImage() 用来Capture照片
// Capture
@objc func captureImage(){
let setting = AVCapturePhotoSettings()
captureOutput?.capturePhoto(with:setting, delegate: self)
}
showAlert() 用来作为提示用
func showAlert(withTitle: String){
let UIAlertController(title:withTitle, message:"", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title:"OK", style: .default, handler:nil))
present(alertController,animated:true,completion:nil)
}
参考网址:
程序码都是参考自
加上了一点自己DIY的理解
Apple Developer Documentation
Apple Developer Documentation
-ISO 31000 在风险管理社区中,人们普遍认为无法消除风险,并且“没有风险”是不可能的,因为...
「曾经沧海难为水,中英不空眼睛痛」,每当看到文字没对齐、段落没缩排或者中文字遇英文字不加空白,就会感...
前言 大家好,我是毛毛。ヾ(´∀ ˋ)ノ 第二次参加IT铁人赛,希望可以透过这次铁人赛纪录Leetc...
人的科技文明发展始终来自於人性 在现今的科技发展之下,所有的技术日新月异、推陈出新,所有的科技不断的...
打从进CMoney培训的第一天起,我应该怀疑过自己无数次:我真的适合当工程师吗? 而当身边的人提出对...