Skip to content

Commit bf06236

Browse files
alexmanzerAlexander Manzer
andauthored
iOS wrapper: Add functionality of encoding binary data into Barcodes (#635)
* Rename methods to writeText and writeBytes --------- Co-authored-by: Alexander Manzer <alexander.manzer@kurzdigital.com>
1 parent 5e8c82d commit bf06236

File tree

3 files changed

+90
-60
lines changed

3 files changed

+90
-60
lines changed

wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ NS_ASSUME_NONNULL_BEGIN
99

1010
@interface ZXIBarcodeWriter : NSObject
1111

12-
-(nullable CGImageRef)write:(NSString *)contents
13-
width:(int)width
14-
height:(int)height
15-
format:(ZXIFormat)format
16-
error:(NSError **)error;
12+
-(nullable CGImageRef)writeText:(NSString *)contents
13+
width:(int)width
14+
height:(int)height
15+
format:(ZXIFormat)format
16+
error:(NSError *__autoreleasing _Nullable *)error;
17+
18+
-(nullable CGImageRef)writeBytes:(NSData *)data
19+
width:(int)width
20+
height:(int)height
21+
format:(ZXIFormat)format
22+
error:(NSError *__autoreleasing _Nullable *)error;
23+
1724
@end
1825

1926
NS_ASSUME_NONNULL_END

wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#import "ZXIBarcodeWriter.h"
77
#import "MultiFormatWriter.h"
88
#import "BitMatrix.h"
9+
#import "BitMatrixIO.h"
910
#import "ZXIFormatHelper.h"
1011
#import "ZXIErrors.h"
1112
#import <iostream>
@@ -18,69 +19,58 @@
1819
sizeof(wchar_t));
1920
}
2021

21-
#ifdef DEBUG
22-
std::string ToString(const BitMatrix& matrix, char one, char zero, bool addSpace, bool printAsCString)
23-
{
24-
std::string result;
25-
result.reserve((addSpace ? 2 : 1) * (matrix.width() * matrix.height()) + matrix.height());
26-
for (int y = 0; y < matrix.height(); ++y) {
27-
for (int x = 0; x < matrix.width(); ++x) {
28-
result += matrix.get(x, y) ? one : zero;
29-
if (addSpace)
30-
result += ' ';
31-
}
32-
if (printAsCString)
33-
result += "\\n\"";
34-
result += '\n';
22+
std::wstring NSDataToStringW(NSData *data) {
23+
std::wstring s;
24+
const unsigned char *bytes = (const unsigned char *) [data bytes];
25+
size_t len = [data length];
26+
for (int i = 0; i < len; ++i) {
27+
s.push_back(bytes[i]);
3528
}
36-
return result;
29+
return s;
3730
}
38-
#endif
3931

4032
@implementation ZXIBarcodeWriter
4133

42-
-(CGImageRef)write:(NSString *)contents
43-
width:(int)width
44-
height:(int)height
45-
format:(ZXIFormat)format
46-
error:(NSError *__autoreleasing _Nullable *)error {
34+
-(CGImageRef)writeBytes:(NSData *)data
35+
width:(int)width
36+
height:(int)height
37+
format:(ZXIFormat)format
38+
error:(NSError *__autoreleasing _Nullable *)error {
39+
return [self encode: NSDataToStringW(data)
40+
width: width
41+
height: height
42+
format: format
43+
encoding: CharacterSet::BINARY
44+
error: error];
45+
}
46+
47+
-(CGImageRef)writeText:(NSString *)contents
48+
width:(int)width
49+
height:(int)height
50+
format:(ZXIFormat)format
51+
error:(NSError *__autoreleasing _Nullable *)error {
52+
return [self encode: NSStringToStringW(contents)
53+
width: width
54+
height: height
55+
format: format
56+
encoding: CharacterSet::UTF8
57+
error: error];
58+
}
59+
60+
-(CGImageRef)encode:(std::wstring)content
61+
width:(int)width
62+
height:(int)height
63+
format:(ZXIFormat)format
64+
encoding:(CharacterSet)encoding
65+
error:(NSError *__autoreleasing _Nullable *)error {
4766
MultiFormatWriter writer { BarcodeFormatFromZXIFormat(format) };
67+
writer.setEncoding(encoding);
4868
// Catch exception for invalid formats
4969
try {
50-
BitMatrix result = writer.encode(NSStringToStringW(contents), width, height);
51-
int realWidth = result.width();
52-
int realHeight = result.height();
53-
54-
#ifdef DEBUG
55-
// std::cout << ToString(result, 'X', ' ', false, false);
56-
#endif
57-
58-
NSMutableData *resultAsNSData = [[NSMutableData alloc] initWithLength:realWidth * realHeight];
59-
size_t index = 0;
60-
uint8_t *bytes = (uint8_t*)resultAsNSData.mutableBytes;
61-
for (int y = 0; y < realHeight; ++y) {
62-
for (int x = 0; x < realWidth; ++x) {
63-
bytes[index] = result.get(x, y) ? 0 : 255;
64-
++index;
65-
}
66-
}
67-
68-
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray);
69-
70-
CGImageRef cgimage = CGImageCreate(realWidth,
71-
realHeight,
72-
8,
73-
8,
74-
realWidth,
75-
colorSpace,
76-
kCGBitmapByteOrderDefault,
77-
CGDataProviderCreateWithCFData((CFDataRef)resultAsNSData),
78-
NULL,
79-
YES,
80-
kCGRenderingIntentDefault);
81-
return cgimage;
70+
BitMatrix bitMatrix = writer.encode(content, width, height);
71+
return [self inflate:&bitMatrix];
8272
} catch(std::exception &e) {
83-
if(error != nil) {
73+
if (error != nil) {
8474
NSDictionary *userInfo = @{
8575
NSLocalizedDescriptionKey: [[NSString alloc] initWithUTF8String:e.what()]
8676
};
@@ -90,4 +80,37 @@ -(CGImageRef)write:(NSString *)contents
9080
}
9181
}
9282

83+
-(CGImageRef)inflate:(BitMatrix *)bitMatrix {
84+
int realWidth = bitMatrix->width();
85+
int realHeight = bitMatrix->height();
86+
87+
#ifdef DEBUG
88+
std::cout << ToString(*bitMatrix, 'X', ' ', false, false);
89+
#endif
90+
91+
NSMutableData *resultAsNSData = [[NSMutableData alloc] initWithLength:realWidth * realHeight];
92+
size_t index = 0;
93+
uint8_t *bytes = (uint8_t*)resultAsNSData.mutableBytes;
94+
for (int y = 0; y < realHeight; ++y) {
95+
for (int x = 0; x < realWidth; ++x) {
96+
bytes[index] = bitMatrix->get(x, y) ? 0 : 255;
97+
++index;
98+
}
99+
}
100+
101+
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray);
102+
103+
return CGImageCreate(realWidth,
104+
realHeight,
105+
8,
106+
8,
107+
realWidth,
108+
colorSpace,
109+
kCGBitmapByteOrderDefault,
110+
CGDataProviderCreateWithCFData((CFDataRef)resultAsNSData),
111+
NULL,
112+
YES,
113+
kCGRenderingIntentDefault);
114+
}
115+
93116
@end

wrappers/ios/demo/demo/WriteViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class WriteViewController: UIViewController {
1515

1616
@IBAction func textFieldChanged(_ sender: UITextField) {
1717
guard let text = sender.text,
18-
let image = try? ZXIBarcodeWriter().write(text, width: 200, height: 200, format: .QR_CODE)
18+
let image = try? ZXIBarcodeWriter().writeText(text, width: 200, height: 200, format: .QR_CODE)
1919
else {
2020
return
2121
}

0 commit comments

Comments
 (0)