Просмотр исходного кода

1.修改iOS Apex & Drivers条码扫描,适配横屏。

Pen Li 8 лет назад
Родитель
Сommit
53007dfbfd

+ 0 - 1
Redant Drivers/Apex And Drivers/Camera/RACameraViewController.m

@@ -228,5 +228,4 @@
     }];
 }
 
-
 @end

+ 98 - 9
Redant Drivers/Apex And Drivers/QRCode/QRCode.storyboard

@@ -32,12 +32,22 @@
                                 <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                                 <constraints>
                                     <constraint firstAttribute="width" secondItem="vCI-Zm-N7X" secondAttribute="height" multiplier="1:1" id="8cj-iq-wvr"/>
+                                    <constraint firstAttribute="height" constant="300" id="tXF-V5-qbK"/>
                                 </constraints>
+                                <variation key="default">
+                                    <mask key="constraints">
+                                        <exclude reference="tXF-V5-qbK"/>
+                                    </mask>
+                                </variation>
+                                <variation key="heightClass=regular-widthClass=regular">
+                                    <mask key="constraints">
+                                        <include reference="tXF-V5-qbK"/>
+                                    </mask>
+                                </variation>
                             </view>
                             <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="0ig-dn-iSa">
-                                <rect key="frame" x="169.5" y="567" width="35" height="30"/>
+                                <rect key="frame" x="170" y="567" width="35" height="30"/>
                                 <state key="normal" title="Scan"/>
-                                <state key="highlighted" title="Scanning"/>
                                 <connections>
                                     <action selector="scannerBtnTouchCancel:" destination="41J-NQ-wiU" eventType="touchCancel" id="z40-QN-qDW"/>
                                     <action selector="scannerBtnTouchDown:" destination="41J-NQ-wiU" eventType="touchDown" id="5TK-sG-LzS"/>
@@ -48,23 +58,102 @@
                         </subviews>
                         <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                         <constraints>
+                            <constraint firstItem="0ig-dn-iSa" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="vCI-Zm-N7X" secondAttribute="trailing" constant="10" id="29i-AW-HSH"/>
                             <constraint firstItem="0ig-dn-iSa" firstAttribute="top" relation="greaterThanOrEqual" secondItem="vCI-Zm-N7X" secondAttribute="bottom" constant="10" id="3gR-v0-uuY"/>
                             <constraint firstItem="4DJ-GK-9l9" firstAttribute="height" secondItem="D8z-rA-Qpf" secondAttribute="height" id="4QJ-Tu-9cj"/>
-                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="centerX" secondItem="TEW-wV-M4S" secondAttribute="centerX" id="8dy-2H-2iw"/>
+                            <constraint firstAttribute="bottom" secondItem="vCI-Zm-N7X" secondAttribute="bottom" constant="81" id="7a1-vL-ZwM"/>
+                            <constraint firstAttribute="trailing" secondItem="0ig-dn-iSa" secondAttribute="trailing" constant="104" id="7eW-A7-Nkb"/>
+                            <constraint firstItem="0ig-dn-iSa" firstAttribute="centerX" secondItem="vCI-Zm-N7X" secondAttribute="centerX" id="7qf-eL-rnv"/>
+                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="centerX" secondItem="aiT-qZ-M5f" secondAttribute="centerX" id="8dy-2H-2iw"/>
                             <constraint firstItem="D8z-rA-Qpf" firstAttribute="width" secondItem="aiT-qZ-M5f" secondAttribute="width" id="BuV-2a-gda"/>
+                            <constraint firstItem="0ig-dn-iSa" firstAttribute="centerY" secondItem="vCI-Zm-N7X" secondAttribute="centerY" id="DcB-0a-gGt"/>
                             <constraint firstItem="4DJ-GK-9l9" firstAttribute="leading" secondItem="D8z-rA-Qpf" secondAttribute="leading" id="OtU-1g-dHc"/>
+                            <constraint firstItem="0ig-dn-iSa" firstAttribute="centerY" secondItem="vCI-Zm-N7X" secondAttribute="centerY" id="R8a-93-kVx"/>
                             <constraint firstItem="D8z-rA-Qpf" firstAttribute="leading" secondItem="aiT-qZ-M5f" secondAttribute="leading" id="RYY-iZ-sc1"/>
                             <constraint firstItem="4DJ-GK-9l9" firstAttribute="width" secondItem="D8z-rA-Qpf" secondAttribute="width" id="S8U-sb-mvg"/>
-                            <constraint firstItem="0ig-dn-iSa" firstAttribute="centerX" secondItem="TEW-wV-M4S" secondAttribute="centerX" id="bkm-1o-EpZ"/>
-                            <constraint firstItem="TEW-wV-M4S" firstAttribute="trailing" secondItem="vCI-Zm-N7X" secondAttribute="trailing" constant="70" id="cSr-ne-Xxd"/>
-                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="centerY" secondItem="TEW-wV-M4S" secondAttribute="centerY" constant="-80" id="fNO-9Q-eMD"/>
-                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="leading" secondItem="TEW-wV-M4S" secondAttribute="leading" constant="70" id="ncF-ii-QJ5"/>
-                            <constraint firstItem="TEW-wV-M4S" firstAttribute="bottom" secondItem="0ig-dn-iSa" secondAttribute="bottom" constant="70" id="qin-xY-VOb"/>
+                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="centerX" secondItem="aiT-qZ-M5f" secondAttribute="centerX" constant="-100" id="V3j-Mb-G9g"/>
+                            <constraint firstItem="0ig-dn-iSa" firstAttribute="centerX" secondItem="vCI-Zm-N7X" secondAttribute="centerX" id="Vi4-ET-WmZ"/>
+                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="centerX" secondItem="aiT-qZ-M5f" secondAttribute="centerX" constant="-80" id="Z6F-qY-EEv"/>
+                            <constraint firstAttribute="trailing" secondItem="vCI-Zm-N7X" secondAttribute="trailing" constant="70" id="cSr-ne-Xxd"/>
+                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="centerY" secondItem="aiT-qZ-M5f" secondAttribute="centerY" constant="-110" id="e2p-ZK-jAC"/>
+                            <constraint firstAttribute="trailing" secondItem="0ig-dn-iSa" secondAttribute="trailing" constant="80" id="eCS-j2-u7d"/>
+                            <constraint firstAttribute="bottom" secondItem="0ig-dn-iSa" secondAttribute="bottom" constant="100" id="exY-By-XcQ"/>
+                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="centerY" secondItem="aiT-qZ-M5f" secondAttribute="centerY" constant="-70" id="fNO-9Q-eMD"/>
+                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="top" secondItem="aiT-qZ-M5f" secondAttribute="top" constant="60" id="gDV-MG-Lxf"/>
+                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="top" secondItem="aiT-qZ-M5f" secondAttribute="top" constant="60" id="mwH-jr-Ixq"/>
+                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="leading" secondItem="aiT-qZ-M5f" secondAttribute="leading" constant="70" id="ncF-ii-QJ5"/>
+                            <constraint firstAttribute="bottom" secondItem="0ig-dn-iSa" secondAttribute="bottom" constant="70" id="qin-xY-VOb"/>
                             <constraint firstItem="D8z-rA-Qpf" firstAttribute="top" secondItem="aiT-qZ-M5f" secondAttribute="top" id="rVq-z9-u0f"/>
+                            <constraint firstItem="vCI-Zm-N7X" firstAttribute="centerX" secondItem="aiT-qZ-M5f" secondAttribute="centerX" id="s29-TR-Yol"/>
                             <constraint firstItem="4DJ-GK-9l9" firstAttribute="top" secondItem="D8z-rA-Qpf" secondAttribute="top" id="sbV-jE-FcH"/>
                             <constraint firstItem="D8z-rA-Qpf" firstAttribute="height" secondItem="aiT-qZ-M5f" secondAttribute="height" id="swA-T5-xSR"/>
+                            <constraint firstAttribute="bottom" secondItem="vCI-Zm-N7X" secondAttribute="bottom" constant="60" id="uOO-Nq-OMU"/>
                         </constraints>
                         <viewLayoutGuide key="safeArea" id="TEW-wV-M4S"/>
+                        <variation key="default">
+                            <mask key="constraints">
+                                <exclude reference="7a1-vL-ZwM"/>
+                                <exclude reference="7eW-A7-Nkb"/>
+                                <exclude reference="cSr-ne-Xxd"/>
+                                <exclude reference="eCS-j2-u7d"/>
+                                <exclude reference="exY-By-XcQ"/>
+                                <exclude reference="qin-xY-VOb"/>
+                                <exclude reference="uOO-Nq-OMU"/>
+                                <exclude reference="8dy-2H-2iw"/>
+                                <exclude reference="V3j-Mb-G9g"/>
+                                <exclude reference="Z6F-qY-EEv"/>
+                                <exclude reference="e2p-ZK-jAC"/>
+                                <exclude reference="fNO-9Q-eMD"/>
+                                <exclude reference="gDV-MG-Lxf"/>
+                                <exclude reference="mwH-jr-Ixq"/>
+                                <exclude reference="ncF-ii-QJ5"/>
+                                <exclude reference="s29-TR-Yol"/>
+                                <exclude reference="29i-AW-HSH"/>
+                                <exclude reference="3gR-v0-uuY"/>
+                                <exclude reference="7qf-eL-rnv"/>
+                                <exclude reference="DcB-0a-gGt"/>
+                                <exclude reference="R8a-93-kVx"/>
+                                <exclude reference="Vi4-ET-WmZ"/>
+                            </mask>
+                        </variation>
+                        <variation key="heightClass=compact-widthClass=compact">
+                            <mask key="constraints">
+                                <include reference="7a1-vL-ZwM"/>
+                                <include reference="7eW-A7-Nkb"/>
+                                <include reference="Z6F-qY-EEv"/>
+                                <include reference="gDV-MG-Lxf"/>
+                                <include reference="29i-AW-HSH"/>
+                                <include reference="DcB-0a-gGt"/>
+                            </mask>
+                        </variation>
+                        <variation key="heightClass=compact-widthClass=regular">
+                            <mask key="constraints">
+                                <include reference="eCS-j2-u7d"/>
+                                <include reference="uOO-Nq-OMU"/>
+                                <include reference="V3j-Mb-G9g"/>
+                                <include reference="mwH-jr-Ixq"/>
+                                <include reference="R8a-93-kVx"/>
+                            </mask>
+                        </variation>
+                        <variation key="heightClass=regular-widthClass=compact">
+                            <mask key="constraints">
+                                <include reference="cSr-ne-Xxd"/>
+                                <include reference="qin-xY-VOb"/>
+                                <include reference="8dy-2H-2iw"/>
+                                <include reference="fNO-9Q-eMD"/>
+                                <include reference="ncF-ii-QJ5"/>
+                                <include reference="3gR-v0-uuY"/>
+                                <include reference="Vi4-ET-WmZ"/>
+                            </mask>
+                        </variation>
+                        <variation key="heightClass=regular-widthClass=regular">
+                            <mask key="constraints">
+                                <include reference="exY-By-XcQ"/>
+                                <include reference="e2p-ZK-jAC"/>
+                                <include reference="s29-TR-Yol"/>
+                                <include reference="7qf-eL-rnv"/>
+                            </mask>
+                        </variation>
                     </view>
                     <connections>
                         <outlet property="maskView" destination="4DJ-GK-9l9" id="naM-uL-H99"/>
@@ -74,7 +163,7 @@
                 </viewController>
                 <placeholder placeholderIdentifier="IBFirstResponder" id="3tF-Yn-TdS" userLabel="First Responder" sceneMemberID="firstResponder"/>
             </objects>
-            <point key="canvasLocation" x="-154" y="162"/>
+            <point key="canvasLocation" x="-155.07246376811597" y="161.41304347826087"/>
         </scene>
     </scenes>
 </document>

+ 77 - 20
Redant Drivers/Apex And Drivers/QRCode/RAQRCodeScannerViewController.m

@@ -26,6 +26,7 @@
 
 @property (nonatomic,strong) CAGradientLayer *scanLineLayer;
 @property (nonatomic,strong) CAShapeLayer *maskLayer;
+@property (nonatomic,strong) CAShapeLayer *rectLayer;
 
 @end
 
@@ -46,19 +47,68 @@
     [self initCapture];
 }
 
+//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
+//    return UIInterfaceOrientationPortrait;
+//}
+
+- (AVCaptureVideoOrientation)captureVideoOrientation {
+    AVCaptureVideoOrientation result;
+    
+    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
+    switch (deviceOrientation) {
+        case UIDeviceOrientationPortrait:
+        case UIDeviceOrientationFaceUp:
+        case UIDeviceOrientationFaceDown:
+            result = AVCaptureVideoOrientationPortrait;
+            break;
+        case UIDeviceOrientationPortraitUpsideDown:
+            //如果这里设置成AVCaptureVideoOrientationPortraitUpsideDown,则视频方向和拍摄时的方向是相反的。
+            result = AVCaptureVideoOrientationPortrait;
+            break;
+        case UIDeviceOrientationLandscapeLeft:
+            result = AVCaptureVideoOrientationLandscapeRight;
+            break;
+        case UIDeviceOrientationLandscapeRight:
+            result = AVCaptureVideoOrientationLandscapeLeft;
+            break;
+        default:
+            result = AVCaptureVideoOrientationPortrait;
+            break;
+    }
+    
+    return result;
+}
+
 - (void)viewDidLayoutSubviews {
     [super viewDidLayoutSubviews];
     
-    self.previewLayer.frame = self.view.bounds;
+    self.previewLayer.frame = self.previewContainer.bounds;
+    AVCaptureVideoOrientation orientation = [self captureVideoOrientation];
+    if (self.previewLayer.connection.isVideoOrientationSupported) {
+        self.previewLayer.connection.videoOrientation = orientation;
+    }
     
-    CGFloat w = CGRectGetWidth(self.view.bounds);
-    CGFloat h = CGRectGetHeight(self.view.bounds);
+    CGFloat w = CGRectGetWidth(self.previewContainer.bounds);
+    CGFloat h = CGRectGetHeight(self.previewContainer.bounds);
     
     /**
-     rectOfInterest 都是按照横屏来计算的 所以当竖屏的情况下 x轴和y轴要交换一下
-     否则在竖屏下不能扫描条形码。
+     rectOfInterest
+     竖屏 x轴和y轴要交换一下
+     Left、Right:Home键反方向X为0
      */
     CGRect rect = CGRectMake(CGRectGetMinY(self.scanerView.frame) / h, CGRectGetMinX(self.scanerView.frame) / w, CGRectGetHeight(self.scanerView.frame) / h, CGRectGetWidth(self.scanerView.frame) / w);
+    if (orientation == AVCaptureVideoOrientationLandscapeRight) {
+        
+        rect = CGRectMake(CGRectGetMinX(self.scanerView.frame) / w, CGRectGetMinY(self.scanerView.frame) / h, CGRectGetWidth(self.scanerView.frame) / w,CGRectGetHeight(self.scanerView.frame) / h);
+        
+    } else if (orientation == AVCaptureVideoOrientationLandscapeLeft){
+        
+        rect = CGRectMake(1 - CGRectGetMaxX(self.scanerView.frame) / w, 1 - CGRectGetMaxY(self.scanerView.frame) / h, CGRectGetWidth(self.scanerView.frame) / w,CGRectGetHeight(self.scanerView.frame) / h);
+        
+    } else {
+        
+        rect = CGRectMake(CGRectGetMinY(self.scanerView.frame) / h, CGRectGetMinX(self.scanerView.frame) / w, CGRectGetHeight(self.scanerView.frame) / h, CGRectGetWidth(self.scanerView.frame) / w);
+    }
     
     [self.output setRectOfInterest:rect];
     
@@ -68,29 +118,36 @@
     self.scanLineLayer.frame = scanlineFrame;
     [self.scanerView.layer insertSublayer:self.scanLineLayer atIndex:0];
     
-    if (!self.maskLayer) {
-        self.maskLayer = [CAShapeLayer layer];
-    }
+    
     UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.maskView.bounds];
     UIBezierPath *subPath = [UIBezierPath bezierPathWithRect:self.scanerView.frame];
     [path appendPath:subPath];
-    
-    self.maskLayer.fillColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.5].CGColor;
+
+    if (!self.maskLayer) {
+        self.maskLayer = [CAShapeLayer layer];
+        self.maskLayer.fillColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.5].CGColor;
+        self.maskLayer.fillRule = kCAFillRuleEvenOdd;
+    }
     self.maskLayer.path = path.CGPath;
+
+    if (!self.rectLayer) {
+        self.rectLayer = [CAShapeLayer layer];
+        self.rectLayer.fillColor = [UIColor clearColor].CGColor;
+        self.rectLayer.strokeColor = [UIColor whiteColor].CGColor;
+        self.rectLayer.lineWidth = 0.5f;
+    }
+    self.rectLayer.path = subPath.CGPath;
     
-    self.maskLayer.fillRule = kCAFillRuleEvenOdd;
-    
-    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
-    shapeLayer.path = subPath.CGPath;
-    shapeLayer.fillColor = [UIColor clearColor].CGColor;
-    shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
-    shapeLayer.lineWidth = 0.5f;
-    
-    [self.maskLayer addSublayer:shapeLayer];
-    
+    [self.maskLayer addSublayer:self.rectLayer];
     [self.maskView.layer addSublayer:self.maskLayer];
 }
 
+- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
+    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
+    
+//    NSLog(@"device orientation: %ld & statusbar orientaion: %ld",[UIDevice currentDevice].orientation,[UIApplication sharedApplication].statusBarOrientation);
+}
+
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];