EFQRCode:自动生成花式二维码

EFQRCode:自动生成花式二维码。# 为开源点赞# —— 由 SwiftLanguage分享

EFQRCode is a tool to generate QRCode UIImage or recognize QRCode from UIImage, in Swift. It is based on CIDetector and CIFilter.

  • Generation: Create pretty two-dimensional code image with input watermark or icon;
  • Recognition: Recognition rate is higher than simply CIDetector.

中文介绍

Overview

Demo

To run the example project, clone the repo, and run pod install from the Example directory first.

Or you can run the following command in terminal:

git clone git@github.com:EyreFree/EFQRCode.git; cd EFQRCode/Example; pod install; open EFQRCode.xcworkspace

Requirements

  • XCode 8.0+
  • Swift 3.0+

Installation

CocoaPods

EFQRCode is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "EFQRCode", '~> 1.2.0'

Usage

1. Import EFQRCode

Import EFQRCode module where you want to use it:

import EFQRCode
2. Recognition

Get QR Codes from UIImage, maybe there are several codes in a image, so it will return an array:

if let testImage = UIImage(named: "test.png") {
    if let tryCodes = EFQRCode.recognize(image: testImage) {
        if tryCodes.count > 0 {
            print("There are \(tryCodes.count) codes in testImage.")
            for (index, code) in tryCodes.enumerated() {
                print("The content of \(index) QR Code is: \(code).")
            }
        } else {
            print("There is no QR Codes in testImage.")
        }
    } else {
        print("Recognize failed, check your input image!")
    }
}
3. Generation

Create QR Code image, quick usage:

// Common parameters:
//                         content: Content of QR Code
// inputCorrectionLevel (Optional): error-tolerant rate
//                                  L 7%
//                                  M 15%
//                                  Q 25%
//                                  H 30%(Default)
//                 size (Optional): Width and height of image
//        magnification (Optional): Magnification of QRCode compare with the minimum size
//                                  (Parameter size will be ignored if magnification is not nil)
//      backgroundColor (Optional): Background color of QRCode
//      foregroundColor (Optional): Foreground color of QRCode
//                 icon (Optional): Icon in the middle of QR Code Image
//             iconSize (Optional): Width and height of icon
//       isIconColorful (Optional): Is icon colorful
//            watermark (Optional): Watermark background image
//        watermarkMode (Optional): Watermark background image mode, like UIViewContentMode
//  isWatermarkColorful (Optional): Is Watermark colorful

// Extra parameters:
//           foregroundPointOffset: Offset of foreground point
//                allowTransparent: Allow transparent
if let tryImage = EFQRCode.generate(
    content: "https://github.com/EyreFree/EFQRCode",
    magnification: 9,
    watermark: UIImage(named: "WWF"),
    watermarkMode: .scaleAspectFill,
    isWatermarkColorful: false
) {
    print("Create QRCode image success!")
} else {
    print("Create QRCode image failed!")
}

Result:

User Guide

1. Recognition

EFQRCode.recognize(image: UIImage)

Or

EFQRCodeRecognizer(image: image).contents

Two way before is exactly the same, because of the possibility of more than one two-dimensional code in the same iamge, so the return value is `[String]? ', if the return is nil means that the input data is incorrect or null. If the return array is empty, it means we can not recognize any two-dimensional code at the image.

2. Generation

EFQRCode.generate(
    content: String, 
    inputCorrectionLevel: EFInputCorrectionLevel, 
    size: CGFloat, 
    magnification: UInt?, 
    backgroundColor: UIColor, 
    foregroundColor: UIColor, 
    icon: UIImage?, 
    iconSize: CGFloat?, 
    isIconColorful: Bool, 
    watermark: UIImage?, 
    watermarkMode: EFWatermarkMode, 
    isWatermarkColorful: Bool
)

Or

EFQRCodeGenerator(
    content: content,
    inputCorrectionLevel: inputCorrectionLevel,
    size: size,
    magnification: magnification,
    backgroundColor: backgroundColor,
    foregroundColor: foregroundColor,
    icon: icon,
    iconSize: iconSize,
    isIconColorful: isIconColorful,
    watermark: watermark,
    watermarkMode: watermarkMode,
    isWatermarkColorful: isWatermarkColorful
).image

Two way before is exactly the same, the return value is UIImage?, if the return is nil means that there is some wrong during the generation.

If you want to use the extra parameters, you must establish a EFQRCodeGenerator object:

let generator = EFQRCodeGenerator(
    content: content,
    inputCorrectionLevel: inputCorrectionLevel,
    size: size,
    magnification: magnification,
    backgroundColor: backColor,
    foregroundColor: frontColor,
    icon: icon,
    iconSize: iconSize,
    isIconColorful: iconColorful,
    watermark: watermark,
    watermarkMode: watermarkMode,
    isWatermarkColorful: watermarkColorful
)
generator.foregroundPointOffset = self.foregroundPointOffset
generator.allowTransparent = self.allowTransparent

// Final two-dimensional code image
generator.image

Parameters explaination:

  • content: String?

Content, compulsive, capacity is limited, 1273 character most, the density of the two-dimensional lattice increases with the increase of the content. Comparison of different capacity is as follows:

10 characters250 characters
  • inputCorrectionLevel: EFInputCorrectionLevel

Error-tolerant rate, optional, 4 different level, L: 7% / M 15% / Q 25% / H 30%, default is H, the definition of EFInputCorrectionLevel:

// EFInputCorrectionLevel
public enum EFInputCorrectionLevel: Int {
    case l = 0;     // L 7%
    case m = 1;     // M 15%
    case q = 2;     // Q 25%
    case h = 3;     // H 30%
}

Comparison of different inputCorrectionLevel:

LMQH
  • size: CGFloat

Two-dimensional code length, optional, default is 256 (PS: if magnification is not nil, size will be ignored).

  • magnification: UInt?

Magnification, optional, default is nil.

Because in accordance with the existence of size scaling two-dimensional code clarity is not high, if you want to get a more clear two-dimensional code, you can use magnification to set the size of the final generation of two-dimensional code. Here is the smallest ratio relative to the two-dimensional code matrix is concerned, if there is a want size but I hope to have a clear and size and have size approximation of the two-dimensional code by using magnification, through maxMagnificationLessThanOrEqualTo (size: CGFloat), andminMagnificationGreaterThanOrEqualTo (size: CGFloat), want to get magnification these two functions the specific value, the use of specific methods are as follows:

let generator = EFQRCodeGenerator(
    content: content,
    inputCorrectionLevel: inputCorrectionLevel,
    size: size,
    magnification: magnification,
    backgroundColor: backColor,
    foregroundColor: frontColor,
    icon: icon,
    iconSize: iconSize,
    isIconColorful: iconColorful,
    watermark: watermark,
    watermarkMode: watermarkMode,
    isWatermarkColorful: watermarkColorful
)

// Want to get max magnification when size is less than or equalTo 600
generator.magnification = generator.maxMagnificationLessThanOrEqualTo(size: 600)

// Or

// Want to get min magnification when size is greater than or equalTo 600
// generator.magnification = generator.minMagnificationGreaterThanOrEqualTo(size: 600)

// Final two-dimensional code image
generator.image
size 300magnification 9
  • backgroundColor: UIColor

BackgroundColor, optional, default is white.

  • foregroundColor: UIColor

ForegroundColor, optional, color of code point, default is black.

ForegroundColor set to redBackgroundColor set to gray
  • icon: UIImage?

Icon image in the center of code image, optional, default is nil.

  • iconSize: CGFloat?

Size of icon image, optional, default is 20% of size:

Default 20% sizeSet to 64
  • isIconColorful: Bool

Is icon colorful, optional, default is true.

  • watermark: UIImage?

Watermark image, optional, default is nil, for example:

12
  • watermarkMode: EFWatermarkMode

The watermark placed in two-dimensional code position, optional, default is scaleAspectFill, refer to UIViewContentMode, you can treat the two-dimensional code as UIImageView, the definition of UIViewContentMode:

// Like UIViewContentMode
public enum EFWatermarkMode: Int {
    case scaleToFill        = 0;
    case scaleAspectFit     = 1;
    case scaleAspectFill    = 2;
    case center             = 3;
    case top                = 4;
    case bottom             = 5;
    case left               = 6;
    case right              = 7;
    case topLeft            = 8;
    case topRight           = 9;
    case bottomLeft         = 10;
    case bottomRight        = 11;
}
  • isWatermarkColorful: Bool

Is watermark colorful, optional, default is true.

  • foregroundPointOffset: CGFloat

Foreground point offset, optional, default is 0, is not recommended to use, may make the two-dimensional code broken:

00.5
  • allowTransparent: Bool

Allow watermark image transparent, optional, default is true:

truefalse

PS

  1. Please select a high contrast foreground and background color combinations;
  2. You should use magnification instead of size if you want to improve the definition of QRCode image, you can also increase the value of them;
  3. Magnification too high/Size too long/Content too much may cause failure;
  4. It is recommended to test the QRCode image before put it into use;
  5. You can contact me if there is any problem, both Issue and Pull requestare welcome.

PS of PS: I wish you can click the Star button if this tool is useful for you, thanks, QAQ...

Author

EyreFree, eyrefree@eyrefree.org

License

EFQRCode is available under the MIT license. See the LICENSE file for more info.

大家好,大家都知道二维码,它是用特定的几何图形按一定规律在平面(二维方向)上分布的黑白相间的图形,是所有信息数据的一把钥匙。在现代商业活动中,可实现的应用十分广泛,如:产品防伪/溯源、广告推送、网站链接、数据下载、商品交易、定位/导航、电子凭证、车辆管理、信息传递、名片交流、wifi共享等。如今智能手机扫一扫(简称313)功能的应用使得二维码更加普遍。今天小编向大家介绍的就是制作二维码的那么一款软件---QR Code Generator。 小编带你看软件: 1.首先我们来看下QR Code Generator这款软件的界面     从图上我们可以大致看出对于第一次操作的人来说相对简单,更值得说明的是这款软件无需安装,所占空间较小 2.下面我们在数据里输入我们所需要的类容:     3.我们点下生成二维码:    4.下面只要保存一下就可以了,我们可以来看下效果图     QR Code Generator 1.1特色: 是一个简单,非常易于使用的图形QR码图像生成器实用程序。这是一个完美的和小尺寸桌面工具实现生成自己的QR码。 首先输入或粘贴任何文本数据字段,如简单的文本数据,短信,电子邮件地址,个人联系资料,电话号码等,如果有必要可以使用一个点击在线URL缩短服务!设置二维图像尺寸后,选择适当的纠错级别,最后点击“生成QR码”按钮。的产生过程是光速度快,后几毫秒的QR码被显示。现在,可以对图像保存到磁盘或可复制到系统剪贴板后使用与其他图像处理软件。 QR Code Generator系统要求操作系统: Windows XP,Vista,Windows7,8(32和64位) 处理器:1 GHz的CPU与支持 SSE2,英特尔奔腾4,奔腾酷睿或Atom,AMD速龙64或以上 系统内存:512 MB 硬盘:5 MB 其他:主动连接互联网   生成时,运行比价缓慢,建议不要打开太多程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值