I wrote a small ios app to scan ean13 barcode, there is a strange problem regarding the sensitivity, it can read 100% of those ean13 barcodes from the products for which the barcodes are well printed on the product by the manufacturer, for example, CD box, medicine box, chocolate, butter, tea box..., but if the barcode is printed manually instead of the manufacture's barcode printing. then there is a problem to read the barcode, but these barcodes are a valid ean13 barcode, and it can be read easily by the barcode reader device hardware, and even by some barcode scan app which I download from the apple store, for example, QRbot.
for example, the following image shows a manually printed ean13 barcode, it is a valid ean13 barcode, and can be easily read by barcode reader hardware, and can also be read by the QRbot app downloaded from app store. but it can not be read from the app which I wrote by using the AVFoundation.
Here are the complete codes:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
@IBOutlet weak var scanFrame: UIImageView!
var video = AVCaptureVideoPreviewLayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let session = AVCaptureSession()
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do
{
let input = try AVCaptureDeviceInput(device: captureDevice)
session.addInput(input)
}
catch
{
print ("Error opening device")
}
let output = AVCaptureMetadataOutput()
session.addOutput(output)
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
output.metadataObjectTypes = [AVMetadataObjectTypeEAN13Code]
video = AVCaptureVideoPreviewLayer(session: session)
video.frame = view.layer.bounds
view.layer.addSublayer(video)
self.view.bringSubview(toFront: scanFrame)
session.startRunning()
output.rectOfInterest = video.metadataOutputRectOfInterest(for: scanFrame.frame)
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
if metadataObjects != nil && metadataObjects.count != 0
{
if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
{
if object.type == AVMetadataObjectTypeEAN13Code
{
let alert = UIAlertController(title: "QR Code", message: object.stringValue, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Scan again?", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Copy to clipboard", style: .default, handler: { (nil) in
UIPasteboard.general.string = object.stringValue
}))
print(object.stringValue)
present(alert, animated: true, completion: nil)
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Thanks in advance!