1.前言
最近在做扫描身份证识别信息的功能,刚开始想的是用扫二维码那个方法,但是失败了,对相机这一块也不是太熟,于是各种招资料,发现扫描身份证的不是很多,最后自己想了个办法,实验后是成功了,但是效果不是太理想,但是其中用到了自定义相机拍照的功能,这里主要记录一下这个功能,扫描为辅。
2.上代码
代码是用swift3.0实现的,代码里有拍照功能,还有去除快门声的方法,亲测去除有用,但不能保证一定不会发出声音
//1.首先引入 AVFoundation这个库
import AVFoundation
class IDTestViewController: UIViewController {
//AVCaptureSession对象来执行输入设备和输出设备之间的数据传递
var session: AVCaptureSession!
//输入
var videoInput:AVCaptureDeviceInput!
//图片输出
var stillImageOutput:AVCaptureStillImageOutput!
//预览图层
var preview:AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
initAVCaptureSession()
}
override func viewDidDisappear(_ animated: Bool) {
//关闭session
self.session.stopRunning()
}
//MARK: - 初始化相机
func initAVCaptureSession(){
do{
self.session = AVCaptureSession()
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
self.videoInput = try AVCaptureDeviceInput(device: device)
self.stillImageOutput = AVCaptureStillImageOutput()
let outPutsetting = [AVVideoCodecKey : AVVideoCodecJPEG]
stillImageOutput.outputSettings = outPutsetting
self.session.addInput(self.videoInput)
self.session.addOutput(self.stillImageOutput)
self.preview = AVCaptureVideoPreviewLayer(session:self.session)
self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill
self.preview.frame = self.view.bounds
self.view.layer.insertSublayer(self.preview, at: 0)
//运行
self.session.startRunning()
}catch _ as NSError{
//打印错误消息
let errorAlert = UIAlertController(title: "提醒", message: "请在iPhone的\"设置-隐私-相机\"选项中,允许本程序访问您的相机", preferredStyle: UIAlertControllerStyle.alert)
self.present(errorAlert, animated: true, completion: nil)
errorAlert.addAction(UIAlertAction.init(title: "好的", style: UIAlertActionStyle.default, handler: { (UIAlertAction) in
self.dismiss(animated: true, completion: nil)
}))
}
}
func avOrientationForDeviceOrientation(deviceOrientation: UIDeviceOrientation) ->AVCaptureVideoOrientation {
//调整拍出来的图片方向与手机的方向
var result = AVCaptureVideoOrientation.portrait
if ( deviceOrientation == UIDeviceOrientation.landscapeLeft){
result = AVCaptureVideoOrientation.landscapeRight
}else if ( deviceOrientation == UIDeviceOrientation.landscapeRight ){
result = AVCaptureVideoOrientation.landscapeLeft
}
return result
}
//拍照
func takePhoto(){
let stillImageConnect: AVCaptureConnection = self.stillImageOutput.connection(withMediaType: AVMediaTypeVideo)
let curDeviceOrientation = UIDevice.current.orientation
let avcaptureOrientation = self.avOrientationForDeviceOrientation(deviceOrientation: curDeviceOrientation)
stillImageConnect.videoOrientation = avcaptureOrientation
stillImageConnect.videoScaleAndCropFactor = 1.0
let path = "/System/Library/Audio/UISounds/photoShutter.caf"
let docs = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last
let data = NSData(contentsOfFile: path)
data?.write(toFile: (docs?.appending("photoShutter.caf"))!, atomically: true)
//去除快门声音
var soundID:SystemSoundID = 0
if soundID == 0 {
//一定放在主目录下(后面有声音下载地址)
let path = Bundle.main.path(forResource: "photoShutter2", ofType: "caf")
let filePath = URL(fileURLWithPath: path!, isDirectory: false)
AudioServicesCreateSystemSoundID(filePath as CFURL, &soundID)
}
AudioServicesPlaySystemSound(soundID)
stillImageOutput.captureStillImageAsynchronously(from: stillImageConnect) { (imageDataSampleBuffer, error) in
//转为jpegData
let jpegData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
//至此你获得了图片的Data格式,转换一下就得到了图片
let image = UIImage(data: jpegData!)
}
3.总结参考
总结:
其实想实现自己的界面在上面增加自己的控件就可以了,直接加在view上即可。
参考:
1.代码参考自定义相机这篇文章,作者写的挺仔细用的OC语言,我这里只是简单的实现了下部分,不知会出现啥bug,仅供参考哈,想实现更多的功能,去参考他的一下
2.去除快门声参考stackoverflow
声音下载地址
亲测可以的哦,作者确实牛逼
3.身份证识别,这里没有加上识别的代码,其实拍出了图片一切都好做了
我用的是腾讯优图,他上传图片,识别后返回信息给你,免费的哦,不过这种实现方式不太理想,要是能做成像扫二维码那样扫描获得图片估计就会好很多,可惜扫描取出图片我还不会😭,望有研究过的同学指点一二。
如果真的需要身份证识别功能的,推荐参考光学识别身份证OC,这个我测了是可以用的,就是不知道扫描后的数据被谁获取了,所以有隐患,如果对数据有保密需求的还是自行决定吧。