区分Swift中的UPC-E和EAN-8

时间:2014-12-19 20:36:48

标签: swift barcode-scanner

关于条形码, UPC-E EAN-8 具有相同的位数。的(8)

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

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

  • 不需要删除EAN-8的校验位

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

有没有办法可以根据校验位算法区分,或者......

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

结束了解决方案..如果没有连接,请删除并重试。谢谢库巴

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


    }

}