2016年3月1日 星期二

iOS Swift Barcode Reading

http://shrikar.com/implementing-barcode-scanning-in-ios8-with-swift/

http://www.appcoda.com/qr-code-reader-swift/

https://www.hackingwithswift.com/example-code/media/how-to-scan-a-barcode


http://stackoverflow.com/questions/31170750/ios-swift-barcode-reading


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)

    }



}

active_shipping

FedEx

http://www.rubydoc.info/github/Shopify/active_shipping/ActiveMerchant/Shipping/FedEx

gem

https://github.com/Shopify/active_shipping/blob/master/README.md


Getting Shipping Costs With ActiveShipping

tesseract-ocr info


rails gem

https://github.com/meh/ruby-tesseract-ocr

How to install Tesseract OCR on a Amazon EC2 (Free Tier) Linux Machine for Optical Character Recognition





Free Online OCR Tool



Using Non-Ruby Programs With Ruby





Tesseracting + ImageMagcking

To review, you'll have to install the following software in order to create a Ruby script that optimizes scanned images and uses Tesseract to extract their textual content:
  1. The ImageMagick, a library used for command-line image manipulation
  2. The RMagick gem, which provides a Ruby interface for ImageMagick
  3. Tesseract, an open source OCR program that runs from the command-line
Here's example code. Understanding it requires being familiar with RMagick, but there's not much more to it than that. Here is what the code does:
  1. Opens an image file
  2. Optimizes the image for OCR by straightening it and creating a grayscale version that is saved to disk as a TIFF
  3. Executes tesseract from the command line
  4. Reads the contents of the text file created by tesseract
require 'rubygems'
require 'rmagick'

def tessrack(oimg_name, do_gray=true, keep_temp=true)
   fname = oimg_name.chomp(File.extname(oimg_name))
   
   # create a non-crooked version of the image
   tiff = Magick::Image::read(oimg_name).first.deskew 

   # convert to grayscale if do_gray==true
   tiff = tiff.quantize(256, Magick::GRAYColorspace) if do_gray == true
   
   # create a TIFF version of the file, as Tesseract only accepts TIFFs
   tname = "#{fname}--tesseracted.tif"
   tiff.write(tname){|t| t.depth = 8}
   puts "TR:\t#{tname} created"
    
   # Run tesseract
   tc = "tesseract #{tname} #{fname}"
   puts "TR:\t#{tc}"
   `#{tc}`
   
   File.delete(tname) unless keep_temp==true
   File.open("#{fname}.txt"){|txt| txt.read}
end

txt = tessrack("data-hold/tufte-intro.tif")
puts txt
FAQ

https://github.com/tesseract-ocr/tesseract/wiki/FAQ

2016年2月29日 星期一

Client-server version mismatch when multiple versions of spring installed for the current Ruby

spring stop -> bundle update spring -> spring restart

https://github.com/rails/spring/issues/295

Tesseract-ocr gem install on mac os x

https://github.com/meh/ruby-tesseract-ocr



http://stackoverflow.com/questions/32179402/tesseract-ocr-gem-issue-on-mac-os-x

Tesseract developers changed quite some stuff in version 3.0.4 and tesseract-ocr gem lost compatibility with it. The best solution I see is to downgrade for now. You may have previous versions of Tesseract installed on your mac, use brew info tesseract to find that out. If so, just use brew switch. I did brew cleanup recently, so I had to uninstall tesseract and install it from the old formula, like so:
$ brew uninstall tesseract
$ brew install https://raw.githubusercontent.com/Homebrew/homebrew/8ba134eda537d2cee7daa7ebdd9f728389d9c53e/Library/Formula/tesseract.rb
This will install Tesseract 3.02.02_3d that seems to be working. You can find more info and track this issue at tesseract-ocr repo.




http://alangunning.ie/2013/01/13/how-to-install-tesseract-ocr-on-a-amazon-ec2-free-tier-linux-machine-for-optical-character-recognition/

install Tesseract OCR on a Amazon EC2 (Free Tier) Linux Machine for Optical Character Recognition