【问题标题】:Differentiate between UPC-E and EAN-8 in Swift区分 Swift 中的 UPC-E 和 EAN-8
【发布时间】:2014-12-19 20:36:47
【问题描述】:

关于条形码,UPC-EEAN-8 的位数相同。 (8)

当我扫描条形码时,我必须删除校验位。我必须将其删除以与数据库中的条形码匹配。

如何区分这两者? 使用我的其他条形码,我根据数字长度进行区分并删除最后一个数字,但对于这两个我不能,因为它们都是 8 位数字并且只有 1 需要去掉校验位

  • EAN-8 的校验位不需要去掉

  • UPC-E 的校验位是字符索引 7(最后一位)需要删除以及索引 0(第一位)处的数字系统字符。

有没有一种方法可以根据校验位算法进行区分,或者...

【问题讨论】:

  • 您使用什么样的库/API 来读取条形码?

标签: swift barcode-scanner


【解决方案1】:

我不认为你总是可以。 EAN-8 和 UPC-E 具有不同的校验和机制。对于 UPC-E,您首先扩展为 UPC-A,然后计算校验和。对于 EAN-8,您只需使用前 7 位数字。如果校验位仅对 EAN-8 或 UPC-E 有效,那么您已经得到了答案。但是如果两者都匹配(我相信这可能会发生),那么您无法仅根据数字来确定它是 EAN8 还是 UPCE。

【讨论】:

    【解决方案2】:

    我认为这个问题根本与 Swift 无关。我认为条形码扫描仪是通过 USB 端口连接的。扫描仪可能伪装成键盘并“键入”解码数据。您需要参考扫描仪的文档以了解如何重新配置​​扫描仪以提供除解码数据之外的诊断数据。诊断将包括条形码的类型。

    作为一种解决方法,您可以删除最后一个数字,尝试数据库查找,如果失败,删除第一个数字并再次尝试查找。

    【讨论】:

    • 条形码扫描器是用 Swift 实现的,适用于 iPhone。
    • @Machina 确实如此,但问题并不取决于编程语言的选择。等等,你有那个条码扫描器的源代码吗??
    【解决方案3】:

    最后做了一个解决方法.. 如果它没有连接,然后删除并重试。谢谢库巴

    if (barcodeLength == 13) {
    
            // EAN-13 \\
    
            // ********** Database Function ********** Database Function ********** \\
    
    
            // 1
            let urlString: String = "http://\(hostString):\(portString)/barcode.php?&password=\(passString)&db=\(dbString)&barNum=\(self.scannedBarcode)"
            let url: NSURL?  = NSURL(string: urlString)!
            let urlSession = NSURLSession.sharedSession()
    
    
            //2
            let jsonQuery = urlSession.dataTaskWithURL(url!, completionHandler: { data, response, error -> Void in
                if (error != nil) {
                    println("\(error.localizedDescription)")
                }
                var err: NSError?
    
                var jsonResult: Array? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSArray?
    
    
    
                if (err != nil) {
    
    
                    println("Can't connect using credentials")
                    println("JSON Error \(err!.localizedDescription)")
    
    
    
                }
    
                if jsonResult?.count == 0 {
    
                    println("Check digit removed from EAN-13")
    
                    self.scannedBarcode.removeAtIndex(self.scannedBarcode.endIndex.predecessor())
    
                    println(self.scannedBarcode)
    
    
                    self.jsonComp()
    
                    return
    
                }
    
                println("Well, this is 13 digit EAN-13")
    
                // 4
    
                let itemID: String! = jsonResult![0]["ITEM_ID"] as NSString
                let itemName: String! = jsonResult![0]["ITEM_Kitchen_Name"] as NSString
                let itemPrice: String! = jsonResult![0]["ITEM_Sale_Price"] as NSString
    
    
                println(itemID, itemName, itemPrice)
    
                self.selectedID = itemID
                self.selectedName = itemName
                self.selectedPrice = itemPrice
    
    
                dispatch_async(dispatch_get_main_queue(), {
    
    
                    self.performSegueWithIdentifier("editPrice", sender: AnyObject?())
    
                    //
                })
            })
    
            // 5
            jsonQuery.resume()
    
    
        }
    
        else if (barcodeLength == 12) {
    
            // UPC-A \\
    
            // ********** Database Function ********** Database Function ********** \\
    
    
            // 1
            let urlString: String = "http://\(hostString):\(portString)/barcode.php?&password=\(passString)&db=\(dbString)&barNum=\(self.scannedBarcode)"
            let url: NSURL?  = NSURL(string: urlString)!
            let urlSession = NSURLSession.sharedSession()
    
    
            //2
            let jsonQuery = urlSession.dataTaskWithURL(url!, completionHandler: { data, response, error -> Void in
                if (error != nil) {
                    println("\(error.localizedDescription)")
                }
                var err: NSError?
    
                var jsonResult: Array? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSArray?
    
    
    
                if (err != nil) {
    
    
                    println("Can't connect using credentials")
                    println("JSON Error \(err!.localizedDescription)")
    
    
    
                }
    
                if jsonResult?.count == 0 {
    
                    println("Check digit removed from UPC-A")
    
                    self.scannedBarcode.removeAtIndex(self.scannedBarcode.endIndex.predecessor())
    
                    println(self.scannedBarcode)
    
    
                    self.jsonComp()
    
                    return
    
                }
    
                println("Well, this is 12 digit UPC-A")
    
                // 4
    
                let itemID: String! = jsonResult![0]["ITEM_ID"] as NSString
                let itemName: String! = jsonResult![0]["ITEM_Kitchen_Name"] as NSString
                let itemPrice: String! = jsonResult![0]["ITEM_Sale_Price"] as NSString
    
    
                println(itemID, itemName, itemPrice)
    
                self.selectedID = itemID
                self.selectedName = itemName
                self.selectedPrice = itemPrice
    
    
                dispatch_async(dispatch_get_main_queue(), {
    
    
                    self.performSegueWithIdentifier("editPrice", sender: AnyObject?())
    
                    //
                })
            })
    
            // 5
            jsonQuery.resume()
    
    
    
        }
    
        else if (barcodeLength == 8) {
    
    
    
            // ********** Database Function ********** Database Function ********** \\
    
    
            // 1
            let urlString: String = "http://\(hostString):\(portString)/barcode.php?&password=\(passString)&db=\(dbString)&barNum=\(self.scannedBarcode)"
            let url: NSURL?  = NSURL(string: urlString)!
            let urlSession = NSURLSession.sharedSession()
    
    
            //2
            let jsonQuery = urlSession.dataTaskWithURL(url!, completionHandler: { data, response, error -> Void in
                if (error != nil) {
                    println("\(error.localizedDescription)")
                }
                var err: NSError?
    
                var jsonResult: Array? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSArray?
    
    
    
                if (err != nil) {
    
    
                    println("Can't connect using credentials")
                    println("JSON Error \(err!.localizedDescription)")
    
    
    
                }
    
                if jsonResult?.count == 0 {
    
                    println("Well, this is UPC-E")
    
                    self.scannedBarcode.removeAtIndex(self.scannedBarcode.startIndex)
                    self.scannedBarcode.removeAtIndex(self.scannedBarcode.endIndex.predecessor())
    
    
                    println(self.scannedBarcode)
    
    
                    self.jsonComp()
    
                    return
    
                }
    
                println("Well, this is EAN-8")
    
                // 4
    
                let itemID: String! = jsonResult![0]["ITEM_ID"] as NSString
                let itemName: String! = jsonResult![0]["ITEM_Kitchen_Name"] as NSString
    
                if jsonResult![0]["ITEM_Sale_Price"] == nil {
    
                    println("No Sale Price")
    
                    dispatch_async(dispatch_get_main_queue(), {
    
                        HUDController.sharedController.hide(afterDelay: 0.1)
    
                    })
    
                    var refreshAlert = UIAlertController(title: "Camaleon Reports", message: "Barcode does not have a price set", preferredStyle: UIAlertControllerStyle.Alert)
                    refreshAlert.addAction(UIAlertAction(title: "Retry", style: .Default, handler: { (action: UIAlertAction!) in
                        println("Yes Logic")
    
                    }))
    
    
                    self.presentViewController(refreshAlert, animated: true, completion: nil)
    
                    return
    
                }
    
                let itemPrice: String! = jsonResult![0]["ITEM_Sale_Price"] as NSString
    
    
                println(itemID, itemName, itemPrice)
    
                self.selectedID = itemID
                self.selectedName = itemName
                self.selectedPrice = itemPrice
    
    
                dispatch_async(dispatch_get_main_queue(), {
    
    
                    self.performSegueWithIdentifier("editPrice", sender: AnyObject?())
    
                    //
                })
            })
    
            // 5
            jsonQuery.resume()
    
        } else {
    
    
            dispatch_async(dispatch_get_main_queue(), {
    
                HUDController.sharedController.hide(afterDelay: 0.1)
    
            })
    
            var refreshAlert = UIAlertController(title: "Camaleon Reports", message: "Barcode does not exist", preferredStyle: UIAlertControllerStyle.Alert)
            refreshAlert.addAction(UIAlertAction(title: "Retry", style: .Default, handler: { (action: UIAlertAction!) in
                println("Yes Logic")
    
            }))
    
    
            self.presentViewController(refreshAlert, animated: true, completion: nil)
    
            return
    
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 2012-04-25
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      相关资源
      最近更新 更多