6
6
#import " ZXIBarcodeWriter.h"
7
7
#import " MultiFormatWriter.h"
8
8
#import " BitMatrix.h"
9
+ #import " BitMatrixIO.h"
9
10
#import " ZXIFormatHelper.h"
10
11
#import " ZXIErrors.h"
11
12
#import < iostream>
18
19
sizeof (wchar_t ));
19
20
}
20
21
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]);
35
28
}
36
- return result ;
29
+ return s ;
37
30
}
38
- #endif
39
31
40
32
@implementation ZXIBarcodeWriter
41
33
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 {
47
66
MultiFormatWriter writer { BarcodeFormatFromZXIFormat (format) };
67
+ writer.setEncoding (encoding);
48
68
// Catch exception for invalid formats
49
69
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];
82
72
} catch (std::exception &e) {
83
- if (error != nil ) {
73
+ if (error != nil ) {
84
74
NSDictionary *userInfo = @{
85
75
NSLocalizedDescriptionKey : [[NSString alloc ] initWithUTF8String: e.what ()]
86
76
};
@@ -90,4 +80,37 @@ -(CGImageRef)write:(NSString *)contents
90
80
}
91
81
}
92
82
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
+
93
116
@end
0 commit comments