मेरे पास मेरे ऐप में बारकोड स्कैनिंग काम कर रहा है। बारकोड का पता लगाने के बाद मैं बारकोड की प्रसंस्करण की अनुमति देने के लिए कैप्चर सत्र को रोकता हूं। हालांकि, बारकोड संसाधित होने के बाद मैं स्कैनिंग नियंत्रक को रहना चाहता हूं और अगला बारकोड स्कैन किया गया है। मैंने माना था कि कैप्चर सत्र शुरू करना (startRunning()) ऐसा करेगा लेकिन छवि जमे हुए रहती है। मैं कैप्चर सत्र फिर से कैसे शुरू कर सकता हूं?आईओएस स्विफ्ट बारकोड पढ़ना
उत्तर
सत्र उपयोग इस कोड
self.session.stopRunning()
बंद करो करने के लिए यह फिर से शुरू करने के लिए इस कोड को
self.session.startRunning()
यहाँ सब एक बारकोड स्कैनर लागू करने के लिए कोड है का उपयोग करें ...
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
let session : AVCaptureSession = AVCaptureSession()
var previewLayer : AVCaptureVideoPreviewLayer!
var highlightView : UIView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// Allow the view to resize freely
self.highlightView.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin |
UIViewAutoresizing.FlexibleBottomMargin |
UIViewAutoresizing.FlexibleLeftMargin |
UIViewAutoresizing.FlexibleRightMargin
// Select the color you want for the completed scan reticle
self.highlightView.layer.borderColor = UIColor.greenColor().CGColor
self.highlightView.layer.borderWidth = 3
// Add it to our controller's view as a subview.
self.view.addSubview(self.highlightView)
// For the sake of discussion this is the camera
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
// Create a nilable NSError to hand off to the next method.
// Make sure to use the "var" keyword and not "let"
var error : NSError? = nil
let input : AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(device, error: &error) as? AVCaptureDeviceInput
// If our input is not nil then add it to the session, otherwise we're kind of done!
if input != nil {
session.addInput(input)
}
else {
// This is fine for a demo, do something real with this in your app. :)
println(error)
}
let output = AVCaptureMetadataOutput()
output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
session.addOutput(output)
output.metadataObjectTypes = output.availableMetadataObjectTypes
previewLayer = AVCaptureVideoPreviewLayer.layerWithSession(session) as! AVCaptureVideoPreviewLayer
previewLayer.frame = self.view.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.view.layer.addSublayer(previewLayer)
// Start the scanner. You'll have to end it yourself later.
session.startRunning()
}
// This is called when we find a known barcode type with the camera.
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
var highlightViewRect = CGRectZero
var barCodeObject : AVMetadataObject!
var detectionString : String!
let barCodeTypes = [AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode
]
// The scanner is capable of capturing multiple 2-dimensional barcodes in one scan.
for metadata in metadataObjects {
for barcodeType in barCodeTypes {
if metadata.type == barcodeType {
barCodeObject = self.previewLayer.transformedMetadataObjectForMetadataObject(metadata as! AVMetadataMachineReadableCodeObject)
highlightViewRect = barCodeObject.bounds
detectionString = (metadata as! AVMetadataMachineReadableCodeObject).stringValue
self.session.stopRunning()
self.alert(detectionString)
break
}
}
}
println(detectionString)
self.highlightView.frame = highlightViewRect
self.view.bringSubviewToFront(self.highlightView)
}
func alert(Code: String){
let actionSheet:UIAlertController = UIAlertController(title: "Barcode", message: "\(Code)", preferredStyle: UIAlertControllerStyle.Alert)
// for alert add .Alert instead of .Action Sheet
// start copy
let firstAlertAction:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:
{
(alertAction:UIAlertAction!) in
// action when pressed
self.session.startRunning()
})
actionSheet.addAction(firstAlertAction)
// end copy
self.presentViewController(actionSheet, animated: true, completion: nil)
}
}
यदि यह काम करता है, तो इस उत्तर को सही के रूप में चिह्नित करना न भूलें। –
मैं वही चीज़ कर रहा था लेकिन मेरे पास कोड की दो पंक्तियां बदल गईं। मैं परिणाम हैंडलर को अपने हैंडलर पर भेज रहा था और फिर सत्र को रोक रहा था। यह पुनरारंभ नहीं होगा। अगर मैंने अपने हैंडलर को परिणाम भेजने से पहले सत्र बंद कर दिया तो यह काम किया। –
जोड़ा गया स्विफ्ट 3 संस्करण यहां: http://stackoverflow.com/a/41512546/2108523 – mkhoshpour
स्विफ्ट 2.0:
के लिए उपरोक्त कोड संपादित किया गयाimport UIKit
import AVFoundation
class BarCodeViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
let session : AVCaptureSession = AVCaptureSession()
var previewLayer : AVCaptureVideoPreviewLayer!
@IBOutlet weak var highlightView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Allow the view to resize freely
// self.highlightView.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin |
// UIViewAutoresizing.FlexibleBottomMargin |
// UIViewAutoresizing.FlexibleLeftMargin |
// UIViewAutoresizing.FlexibleRightMargin
//
// Select the color you want for the completed scan reticle
self.highlightView.layer.borderColor = UIColor.greenColor().CGColor
self.highlightView.layer.borderWidth = 3
// Add it to our controller's view as a subview.
self.view.addSubview(self.highlightView)
// For the sake of discussion this is the camera
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
// Create a nilable NSError to hand off to the next method.
// Make sure to use the "var" keyword and not "let"
var error : NSError? = nil
var input: AVCaptureDeviceInput = AVCaptureDeviceInput()
do {
input = try AVCaptureDeviceInput(device: device) as AVCaptureDeviceInput
} catch let myJSONError {
print(myJSONError)
}
// If our input is not nil then add it to the session, otherwise we're kind of done!
if input != AVCaptureDeviceInput() {
session.addInput(input)
}
else {
// This is fine for a demo, do something real with this in your app. :)
print(error)
}
let output = AVCaptureMetadataOutput()
output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
session.addOutput(output)
output.metadataObjectTypes = output.availableMetadataObjectTypes
previewLayer = AVCaptureVideoPreviewLayer(session: session)
previewLayer.frame = self.view.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.view.layer.addSublayer(previewLayer)
// Start the scanner. You'll have to end it yourself later.
session.startRunning()
}
// This is called when we find a known barcode type with the camera.
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
var highlightViewRect = CGRectZero
var barCodeObject : AVMetadataObject!
var detectionString : String!
let barCodeTypes = [AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode
]
// The scanner is capable of capturing multiple 2-dimensional barcodes in one scan.
for metadata in metadataObjects {
for barcodeType in barCodeTypes {
if metadata.type == barcodeType {
barCodeObject = self.previewLayer.transformedMetadataObjectForMetadataObject(metadata as! AVMetadataMachineReadableCodeObject)
highlightViewRect = barCodeObject.bounds
detectionString = (metadata as! AVMetadataMachineReadableCodeObject).stringValue
self.session.stopRunning()
self.alert(detectionString)
break
}
}
}
print(detectionString)
self.highlightView.frame = highlightViewRect
self.view.bringSubviewToFront(self.highlightView)
}
func alert(Code: String){
let actionSheet:UIAlertController = UIAlertController(title: "Barcode", message: "\(Code)", preferredStyle: UIAlertControllerStyle.Alert)
// for alert add .Alert instead of .Action Sheet
// start copy
let firstAlertAction:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:
{
(alertAction:UIAlertAction!) in
// action when pressed
self.session.startRunning()
})
actionSheet.addAction(firstAlertAction)
// end copy
self.presentViewController(actionSheet, animated: true, completion: nil)
}
}
यह बहुत अच्छा है, धन्यवाद! –
स्विफ्ट 3.0 संस्करण:
import UIKit
import AVFoundation
class BarCodeViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
let session : AVCaptureSession = AVCaptureSession()
var previewLayer : AVCaptureVideoPreviewLayer!
@IBOutlet weak var highlightView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Allow the view to resize freely
// self.highlightView.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin |
// UIViewAutoresizing.FlexibleBottomMargin |
// UIViewAutoresizing.FlexibleLeftMargin |
// UIViewAutoresizing.FlexibleRightMargin
//
// Select the color you want for the completed scan reticle
self.highlightView.layer.borderColor = UIColor.green.cgColor
self.highlightView.layer.borderWidth = 3
// Add it to our controller's view as a subview.
self.view.addSubview(self.highlightView)
// For the sake of discussion this is the camera
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
// Create a nilable NSError to hand off to the next method.
// Make sure to use the "var" keyword and not "let"
var error : NSError? = nil
var input: AVCaptureDeviceInput = AVCaptureDeviceInput()
do {
input = try AVCaptureDeviceInput(device: device) as AVCaptureDeviceInput
} catch let myJSONError {
print(myJSONError)
}
// If our input is not nil then add it to the session, otherwise we're kind of done!
if input != AVCaptureDeviceInput() {
session.addInput(input)
}
else {
// This is fine for a demo, do something real with this in your app. :)
print(error!)
}
let output = AVCaptureMetadataOutput()
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
session.addOutput(output)
output.metadataObjectTypes = output.availableMetadataObjectTypes
previewLayer = AVCaptureVideoPreviewLayer(session: session)
previewLayer.frame = self.view.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.view.layer.addSublayer(previewLayer)
// Start the scanner. You'll have to end it yourself later.
session.startRunning()
}
// This is called when we find a known barcode type with the camera.
@nonobjc func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
var highlightViewRect = CGRect()
var barCodeObject : AVMetadataObject!
var detectionString : String!
let barCodeTypes = [AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode
]
// The scanner is capable of capturing multiple 2-dimensional barcodes in one scan.
for metadata in metadataObjects {
for barcodeType in barCodeTypes {
if metadata.type == barcodeType {
barCodeObject = self.previewLayer.transformedMetadataObject(for: metadata as! AVMetadataMachineReadableCodeObject)
highlightViewRect = barCodeObject.bounds
detectionString = (metadata as! AVMetadataMachineReadableCodeObject).stringValue
self.session.stopRunning()
self.alert(Code: detectionString)
break
}
}
}
print(detectionString)
self.highlightView.frame = highlightViewRect
self.view.bringSubview(toFront: self.highlightView)
}
func alert(Code: String){
let actionSheet:UIAlertController = UIAlertController(title: "Barcode", message: "\(Code)", preferredStyle: UIAlertControllerStyle.alert)
// for alert add .Alert instead of .Action Sheet
// start copy
let firstAlertAction:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:
{
(alertAction:UIAlertAction!) in
// action when pressed
self.session.startRunning()
})
actionSheet.addAction(firstAlertAction)
// end copy
self.present(actionSheet, animated: true, completion: nil)
}
} तेज 3.0 में
- 1. बारकोड स्कैनर से पढ़ना
- 2. छवियों से 2 डी बारकोड पढ़ना
- 3. उन्नत आईओएस बारकोड स्कैनर लाइब्रेरी?
- 4. PHP वेब एप्लिकेशन में वेबकैम से बारकोड पढ़ना
- 5. आईओएस स्विफ्ट प्रोजेक्ट
- 6. आईओएस फोनगैप 1.7.0 + बारकोड स्कैनिंग प्लगइन समस्या
- 7. आईओएस स्विफ्ट
- 8. आईओएस स्विफ्ट
- 9. आईओएस: स्विफ्ट
- 10. आईओएस स्विफ्ट
- 11. आईओएस स्विफ्ट
- 12. स्विफ्ट आईओएस
- 13. स्विफ्ट (आईओएस)
- 14. आईओएस स्विफ्ट
- 15. आईओएस स्विफ्ट
- 16. स्विफ्ट आईओएस
- 17. आईओएस - स्विफ्ट
- 18. स्विफ्ट/आईओएस
- 19. स्विफ्ट (आईओएस)
- 20. स्कैनर उत्पाद आईडी और विक्रेता आईडी को ज्ञात नहीं करते हुए यूएसबी बारकोड स्कैनर का उपयोग करके एक बारकोड पढ़ना
- 21. फोनगैप बारकोड स्कैनर
- 22. स्विफ्ट 3 आईओएस संगतता
- 23. आईओएस स्विफ्ट: AFNetworking
- 24. आईओएस स्विफ्ट didBeginContact
- 25. आईओएस स्विफ्ट: क्लाउडकिट
- 26. UISearchController आईओएस 8 स्विफ्ट
- 27. यूआरएल डिकोड आईओएस स्विफ्ट
- 28. स्विफ्ट और आईओएस 8
- 29. आईओएस डब्ल्यू/स्विफ्ट
- 30. आईओएस 10 (स्विफ्ट 3)
बारकोड scaner: https://iosdevcenters.blogspot.com/2017/09/building-barcode-and-qr- कोड-रीडर-इन.html –