Skip to content

ios: specify EC level/margin for generation #644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions wrappers/ios/Sources/Wrapper/UmbrellaHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "Reader/ZXIPosition.h"
#import "Reader/ZXIPoint.h"
#import "Reader/ZXIDecodeHints.h"
#import "Writer/ZXIEncodeHints.h"
#import "Writer/ZXIBarcodeWriter.h"
#import "ZXIErrors.h"
#import "ZXIFormat.h"
Expand Down
33 changes: 26 additions & 7 deletions wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,41 @@
// SPDX-License-Identifier: Apache-2.0

#import <Foundation/Foundation.h>
#import "ZXIFormat.h"
#import "ZXIEncodeHints.h"

NS_ASSUME_NONNULL_BEGIN

const int AZTEC_ERROR_CORRECTION_0 = 0;
const int AZTEC_ERROR_CORRECTION_12 = 1;
const int AZTEC_ERROR_CORRECTION_25 = 2;
const int AZTEC_ERROR_CORRECTION_37 = 3;
const int AZTEC_ERROR_CORRECTION_50 = 4;
const int AZTEC_ERROR_CORRECTION_62 = 5;
const int AZTEC_ERROR_CORRECTION_75 = 6;
const int AZTEC_ERROR_CORRECTION_87 = 7;
const int AZTEC_ERROR_CORRECTION_100 = 8;
const int QR_ERROR_CORRECTION_LOW = 2;
const int QR_ERROR_CORRECTION_MEDIUM = 4;
const int QR_ERROR_CORRECTION_QUARTILE = 6;
const int QR_ERROR_CORRECTION_HIGH = 8;
const int PDF417_ERROR_CORRECTION_0 = 0;
const int PDF417_ERROR_CORRECTION_1 = 1;
const int PDF417_ERROR_CORRECTION_2 = 2;
const int PDF417_ERROR_CORRECTION_3 = 3;
const int PDF417_ERROR_CORRECTION_4 = 4;
const int PDF417_ERROR_CORRECTION_5 = 5;
const int PDF417_ERROR_CORRECTION_6 = 6;
const int PDF417_ERROR_CORRECTION_7 = 7;
const int PDF417_ERROR_CORRECTION_8 = 8;

@interface ZXIBarcodeWriter : NSObject

-(nullable CGImageRef)writeString:(NSString *)contents
width:(int)width
height:(int)height
format:(ZXIFormat)format
hints:(ZXIEncodeHints *)hints
error:(NSError *__autoreleasing _Nullable *)error;

-(nullable CGImageRef)writeData:(NSData *)data
width:(int)width
height:(int)height
format:(ZXIFormat)format
hints:(ZXIEncodeHints *)hints
error:(NSError *__autoreleasing _Nullable *)error;

@end
Expand Down
35 changes: 20 additions & 15 deletions wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#import <CoreGraphics/CoreGraphics.h>
#import "ZXIBarcodeWriter.h"
#import "ZXIEncodeHints.h"
#import "MultiFormatWriter.h"
#import "BitMatrix.h"
#import "BitMatrixIO.h"
Expand All @@ -23,7 +24,7 @@
std::wstring s;
const unsigned char *bytes = (const unsigned char *) [data bytes];
size_t len = [data length];
for (int i = 0; i < len; ++i) {
for (int i = 0; i < len; ++i) {
s.push_back(bytes[i]);
}
return s;
Expand All @@ -32,39 +33,43 @@
@implementation ZXIBarcodeWriter

-(CGImageRef)writeData:(NSData *)data
width:(int)width
height:(int)height
format:(ZXIFormat)format
hints:(ZXIEncodeHints *)hints
error:(NSError *__autoreleasing _Nullable *)error {
return [self encode: NSDataToStringW(data)
width: width
height: height
format: format
encoding: CharacterSet::BINARY
format: hints.format
width: hints.width
height: hints.height
margin: hints.margin
ecLevel: hints.ecLevel
error: error];
}

-(CGImageRef)writeString:(NSString *)contents
width:(int)width
height:(int)height
format:(ZXIFormat)format
hints:(ZXIEncodeHints *)hints
error:(NSError *__autoreleasing _Nullable *)error {
return [self encode: NSStringToStringW(contents)
width: width
height: height
format: format
encoding: CharacterSet::UTF8
format: hints.format
width: hints.width
height: hints.height
margin: hints.margin
ecLevel: hints.ecLevel
error: error];
}

-(CGImageRef)encode:(std::wstring)content
encoding:(CharacterSet)encoding
format:(ZXIFormat)format
width:(int)width
height:(int)height
format:(ZXIFormat)format
encoding:(CharacterSet)encoding
margin:(int)margin
ecLevel:(int)ecLevel
error:(NSError *__autoreleasing _Nullable *)error {
MultiFormatWriter writer { BarcodeFormatFromZXIFormat(format) };
writer.setEncoding(encoding);
writer.setMargin(margin);
writer.setEccLevel(ecLevel);
// Catch exception for invalid formats
try {
BitMatrix bitMatrix = writer.encode(content, width, height);
Expand Down
26 changes: 26 additions & 0 deletions wrappers/ios/Sources/Wrapper/Writer/ZXIEncodeHints.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2023 KURZ Digital Solutions GmbH
//
// SPDX-License-Identifier: Apache-2.0

#import <Foundation/Foundation.h>
#import "ZXIFormat.h"

NS_ASSUME_NONNULL_BEGIN

@interface ZXIEncodeHints : NSObject
@property(nonatomic) ZXIFormat format;
@property(nonatomic) int width;
@property(nonatomic) int height;
@property(nonatomic) int ecLevel;
@property(nonatomic) int margin;

- (instancetype)initWithFormat:(ZXIFormat)format;

- (instancetype)initWithFormat:(ZXIFormat)format
width:(int)width
height:(int)height
ecLevel:(int)ecLevel
margin:(int)margin;
@end

NS_ASSUME_NONNULL_END
53 changes: 53 additions & 0 deletions wrappers/ios/Sources/Wrapper/Writer/ZXIEncodeHints.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2023 KURZ Digital Solutions GmbH
//
// SPDX-License-Identifier: Apache-2.0

#import "ZXIEncodeHints.h"

@implementation ZXIEncodeHints

- (instancetype)initWithFormat:(ZXIFormat)format {
self = [super init];
self.format = format;
self.width = 0;
self.height = 0;
self.ecLevel = -1;
self.margin = -1;
return self;
}

- (instancetype)initWithFormat:(ZXIFormat)format
width:(int)width
height:(int)height
ecLevel:(int)ecLevel
margin:(int)margin {
self = [super init];
self.format = format;
self.width = width;
self.height = height;
self.ecLevel = ecLevel;
self.margin = margin;
return self;
}

-(void)setFormat:(ZXIFormat)format {
self.format = format;
}

-(void)setWidth:(int)width {
self.width = width;
}

-(void)setHeight:(int)height {
self.height = height;
}

-(void)setEcLevel:(int)ecLevel {
self.ecLevel = ecLevel;
}

-(void)setMargin:(int)margin {
self.margin = margin;
}

@end
3 changes: 2 additions & 1 deletion wrappers/ios/demo/demo/WriteViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class WriteViewController: UIViewController {
// MARK: - Actions

@IBAction func textFieldChanged(_ sender: UITextField) {
let hints = ZXIEncodeHints(format: .QR_CODE, width: 200, height: 200, ecLevel: QR_ERROR_CORRECTION_LOW, margin: -1)
guard let text = sender.text,
let image = try? ZXIBarcodeWriter().write(text, width: 200, height: 200, format: .QR_CODE)
let image = try? ZXIBarcodeWriter().write(text, hints: hints)
else {
return
}
Expand Down
2 changes: 1 addition & 1 deletion zxing-cpp.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Pod::Spec.new do |s|
ss.frameworks = 'CoreGraphics', 'CoreImage', 'CoreVideo'
ss.source_files = 'wrappers/ios/Sources/Wrapper/**/*.{h,m,mm}'
ss.public_header_files = 'wrappers/ios/Sources/Wrapper/Reader/{ZXIBarcodeReader,ZXIResult,ZXIPosition,ZXIPoint,ZXIDecodeHints}.h',
'wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h',
'wrappers/ios/Sources/Wrapper/Writer/{ZXIBarcodeWriter,ZXIEncodeHints}.h',
'wrappers/ios/Sources/Wrapper/{ZXIErrors,ZXIFormat}.h'
ss.exclude_files = 'wrappers/ios/Sources/Wrapper/UmbrellaHeader.h'
end
Expand Down