Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决苹果14等机型近距离不自动对焦的问题。 #184

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions SGQRCode/QRCode/SGScanCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@
/// 播放音效
- (void)playSoundEffect:(NSString *)name;

///打开手电筒
- (void)turnOnTorch;

- (void)turnOffTorch;

@end
64 changes: 59 additions & 5 deletions SGQRCode/QRCode/SGScanCode.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ @interface SGScanCode () <AVCaptureMetadataOutputObjectsDelegate, AVCaptureVideo
@property (nonatomic, strong) NSArray *metadataObjectTypes;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@property (nonatomic, strong) dispatch_queue_t captureQueue;
@property (nonatomic, strong) dispatch_queue_t runQueue;

@end

@implementation SGScanCode
Expand All @@ -40,7 +42,7 @@ + (instancetype)scanCode {
- (instancetype)init {
if ([super init]) {
self.captureQueue = dispatch_queue_create("com.SGQRCode.captureQueue", DISPATCH_QUEUE_CONCURRENT);

self.runQueue = dispatch_queue_create("com.SGQRCode.runqueue", DISPATCH_QUEUE_SERIAL);
/// 将设备输入对象添加到会话对象中
if ([self.session canAddInput:self.deviceInput]) {
[self.session addInput:self.deviceInput];
Expand All @@ -53,8 +55,9 @@ - (instancetype)init {

#pragma mark - - .h公开的属性
- (void)setPreview:(UIView *)preview {
_preview = preview;
[preview.layer insertSublayer:self.videoPreviewLayer atIndex:0];
_preview = preview;
[preview.layer insertSublayer:self.videoPreviewLayer atIndex:0];
[self addEventObserver];
}

- (void)setDelegate:(id<SGScanCodeDelegate>)delegate {
Expand Down Expand Up @@ -124,15 +127,19 @@ - (BOOL)checkCameraDeviceRearAvailable {
}

- (void)startRunning {
dispatch_async(self.runQueue, ^{
if (![self.session isRunning]) {
[self.session startRunning];
}
});
}

- (void)stopRunning {
dispatch_async(self.runQueue, ^{
if ([self.session isRunning]) {
[self.session stopRunning];
}
});
}

- (void)playSoundEffect:(NSString *)name {
Expand All @@ -148,6 +155,41 @@ - (void)playSoundEffect:(NSString *)name {
}


- (void)turnOnTorch {
AVCaptureDevice *device = self.device;
if ([device hasTorch]) {
BOOL locked = [device lockForConfiguration:nil];
if (locked) {
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
}
}
}

- (void)turnOffTorch {
AVCaptureDevice *device = self.device;
if ([device hasTorch]) {
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
}


#pragma mark - 屏幕旋转
- (void)addEventObserver{
__weak typeof(self) weakSelf = self;
void(^result)(NSNotification* ) = ^(NSNotification* notification){
if(nil == weakSelf){return;}
AVCaptureConnection* connection = weakSelf.videoPreviewLayer.connection;
if(NO == connection.isVideoOrientationSupported){return;}
[connection setVideoOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
weakSelf.videoPreviewLayer.frame = weakSelf.preview.bounds;
};
[[NSNotificationCenter defaultCenter]addObserverForName:@"SGScanViewBoundUpdate" object:nil queue:[NSOperationQueue mainQueue] usingBlock:result];
[[NSNotificationCenter defaultCenter]addObserverForName:UIApplicationDidChangeStatusBarOrientationNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:result];
}

#pragma mark - - 内部属性
- (AVCaptureSession *)session {
if (!_session) {
Expand All @@ -159,7 +201,20 @@ - (AVCaptureSession *)session {

- (AVCaptureDevice *)device {
if (!_device) {
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (@available(iOS 13.0, *)) {
AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInDualWideCamera]
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionBack];
// 获取发现的设备数组
NSArray<AVCaptureDevice *> *devices = discoverySession.devices;
// 选择其中一个摄像头设备,例如第一个
if (devices.count > 0) {
AVCaptureDevice *selectedDevice = devices.firstObject;
_device = selectedDevice;
}
}
}
return _device;
}
Expand Down Expand Up @@ -191,7 +246,6 @@ - (AVCaptureVideoPreviewLayer *)videoPreviewLayer {
if (!_videoPreviewLayer) {
_videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_videoPreviewLayer.frame = self.preview.frame;
}
return _videoPreviewLayer;
}
Expand Down
6 changes: 6 additions & 0 deletions SGQRCode/ScanView/SGScanView.m
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,10 @@ - (void)updateUI {
}
}


- (void)layoutSubviews{
[super layoutSubviews];
[[NSNotificationCenter defaultCenter]postNotificationName:@"SGScanViewBoundUpdate" object:NSStringFromCGRect(self.bounds)];
}

@end