4

我正在尝试创建一个简单的 EAN13 图像来显示字符串中的条形码。

我尝试使用此代码,但它只能生成 code128。我可以使用什么来生成 EAN13?

class Barcode {

  class func fromString(string : String) -> UIImage? {

      let data = string.dataUsingEncoding(NSASCIIStringEncoding)
      let filter = CIFilter(name: "CICode128BarcodeGenerator")
      filter.setValue(data, forKey: "inputMessage")
      return UIImage(CIImage: filter.outputImage)
  }
}

let img = Barcode.fromString("1234567890123")
4

2 回答 2

2

你可以试试这个EAN13BarcodeGenerator

用法很简单:

BarCodeView *barCodeView = [[BarCodeView alloc] initWithFrame:kBarCodeFrame];
[self.view addSubview:barCodeView];
[barCodeView setBarCode:GetNewRandomEAN13BarCode()];
于 2016-04-26T20:46:25.127 回答
-4

我的osx两分钱..

func barCodeFromString(string : String, destSize: NSSize) -> NSImage? {
    let data = string.data(using: .ascii)
    guard let filter = CIFilter(name: "CICode128BarcodeGenerator") else{
        return nil
    }

    filter.setValue(data, forKey: "inputMessage")
    guard let ciImage : CIImage = filter.outputImage else{
        return nil
    }

    let c_size = ciImage.extent.size

    let w_ratio = destSize.width/c_size.width
    let h_ratio = destSize.height/c_size.height
    let ratio = w_ratio>h_ratio ? h_ratio : w_ratio
    let transform = CGAffineTransform(scaleX: ratio, y: ratio)
    let scaled = ciImage.transformed(by: transform)

    let rep = NSCIImageRep(ciImage: scaled)
    let nsImage = NSImage(size: rep.size)
    nsImage.addRepresentation(rep)
    return nsImage
}
于 2019-03-17T18:17:08.380 回答