Swiftpack.co - yageek/codekit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by yageek.
yageek/codekit v1.0.0
|||❚|❚|| Create 1D barcode for mobile using Rust :crab:
⭐️ 1
🕓 3 weeks ago
iOS macOS
.package(url: "https://github.com/yageek/codekit.git", from: "v1.0.0")

CodeKit

A toolkit to generate 1D bar code. The generation logic is done on Rust and then wrapped into different platforms/language using FFI or JNI.

You can generate the following types of codes:

  • EAN8
  • EAN13
  • Code39
  • Code93
  • Codabar

The supported platform:

  • iOS/macOS
  • Android

iOS/macOS

A xcframework and docc module are provided. See the releases page to download them.

Install

Download the release asset from the release page or use SPM to add that dependencies to your SPM project:

.package(url: "https://github.com/yageek/codekit.git", from: "1.0.0")

Register the filters

The first step is to register the filters into the system. On iOS, best ways would probably be in the applications delegate.

In objective-c:

#import<CodeKit/CodeKit.h>

/// Registering the filters
[[RCKCodeKit sharedInstance] registerFilters];

In swift:

import CodeKit

/// Registering the filters
RCKCodeKit.shared.registerFilters()

Using a filter

For every filter, you can either use the safe API or the dynamic API.

Safe API

See the different methods and protocol

  • RCKCodeGenerator
  • RCKCodeKit/ean8GeneratorFilter
  • RCKCodeKit/ean13GeneratorFilter
  • RCKCodeKit/code39GeneratorFilter
  • RCKCodeKit/code93GeneratorFilter
  • RCKCodeKit/codabarGeneratorFilter
  • RCKCodeKit/i2of5GeneratorFilter

As an example, let's create a Code39 code:

CIFilter<RCKCodeGenerator> *filter = [RCKCodeKit code93GeneratorFilter];
filter.codeValue = @"TEST93";
CIImage *image = filter.outputImage;
let filter = RCKCodeKit.code93Generator()
filter.codeValue = "TEST93"
let image = filter.outputImage

Dynamic API

The filters can be used using the standard CoreImage filter system. The name of the different filters are:

  • RCKEAN8CodeGenerator
  • RCKEAN13CodeGenerator
  • RCKCode39CodeGenerator
  • RCKICode93odeGenerator
  • RCKI2of5CodeGenerator

Each of this filter has the following properties as float elements to configure the shape of the code:

  • borderWidth: The border of the code. Defaults to 0.
  • quietSpace: The quiet space of the code. Default to 7.
  • barCodeHeight: The bar code height. Default to 50.

To setup the value of the code, use the string parameter codeValue.

As an example, let's create a Code39 code:

CIFilter *filter = [CIFilter filterWithName:@"RCKCode93CodeGenerator"];
[filter setValue:@"TEST93" forKey:@"codeValue"];

CIImage *image = filter.outputImage;

Render to an UIImage

As we are using classic CoreImage filters, we can generate a UIImage using the regular API:

CIFilter<RCKCodeGenerator> *filter = [RCKCodeKit code93GeneratorFilter];
filter.codeValue = @"TEST93";
CIImage *image = filter.outputImage;
CGFloat scaleX = CGRectGetWidth(self.imageView.frame)/CGRectGetWidth(image.extent);
CGAffineTransform tr = CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleX);
CIImage *scaled = [image imageByApplyingTransform:tr];
UIImage *uiImage = [UIImage imageWithCIImage:scaled];
self.imageView.image = uiImage;

Android

Install

You can use gradle/mvn to retrieve the package from Github Package

A android AAR element is provided also in the release page.

The library contains functions to create a string of bars (0/1) from a java string and one function to create a bitmap from those strings.

The available methods are the following:

// Methods to generate a string of bars
public native static String makeEAN8(String code) throws IOException;
public native static String makeEAN13(String code) throws IOException;
public native static String makeCodabar(String code) throws IOException;
public native static String makeCode39(String code) throws IOException;
public native static String makeCode93(String code) throws IOException;
public native static String makeI2Of5(String code) throws IOException;

// Method to generate a bitmap from a string of bar
public static Bitmap convertBitmap(String descriptor, CodeOptions options)

A basic usage could be done as the following:

import net.yageek.codekit.CodeKit;
import net.yageek.codekit.CodeOptions;

try {
    // Creating the string of bars from the code
    String test = CodeKit.makeCode93("TEST93");

    // Creating a code with a height of 200, a quiet space of 7, no border and a bar width of 5
    CodeOptions options = new CodeOptions(200, 7, 0, 5);
    Bitmap bitmap = CodeKit.convertBitmap(test, options);

    this.imageView.setImageBitmap(bitmap);
} catch (IOException e) {
    e.printStackTrace();
}

GitHub

link
Stars: 1
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

First move
3 weeks ago

Add the basic release with the compiled XCFramework for macOS and iOS and AAR for android.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics