From d3afeb67083608767e3bbeff22c448481771c8fd Mon Sep 17 00:00:00 2001 From: Alexander Manzer Date: Tue, 17 Oct 2023 17:16:39 +0200 Subject: [PATCH 1/4] Add functionality of encoding binary data into DM --- .../Sources/Wrapper/Writer/ZXIBarcodeWriter.h | 8 ++ .../Wrapper/Writer/ZXIBarcodeWriter.mm | 113 ++++++++++++------ 2 files changed, 86 insertions(+), 35 deletions(-) diff --git a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h index 664ff3dd84..069915b1a9 100644 --- a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h +++ b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h @@ -14,6 +14,14 @@ NS_ASSUME_NONNULL_BEGIN height:(int)height format:(ZXIFormat)format error:(NSError **)error; + +-(nullable CGImageRef)writeData:(NSData *)data + width:(int)width + height:(int)height + format:(ZXIFormat)format + hidden:(nullable NSString *)hidden + error:(NSError *__autoreleasing _Nullable *)error; + @end NS_ASSUME_NONNULL_END diff --git a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm index 430fed9995..d9b641b63d 100644 --- a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm +++ b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm @@ -4,10 +4,10 @@ #import #import "ZXIBarcodeWriter.h" -#import "ZXing/MultiFormatWriter.h" -#import "ZXing/BitMatrix.h" #import "ZXIFormatHelper.h" #import "ZXIErrors.h" +#import "ZXing/MultiFormatWriter.h" +#import "ZXing/BitMatrix.h" #import using namespace ZXing; @@ -18,6 +18,18 @@ sizeof(wchar_t)); } +std::wstring NSDataToStringW(NSData *data) { + std::wstring s; + const unsigned char *bytes = (const unsigned char *) [data bytes]; + size_t len = [data length]; + wchar_t buf[len]; + for (int i = 0; i < len; ++i) { + buf[i] = bytes[i]; + } + s.assign(buf, len); + return s; +} + #ifdef DEBUG std::string ToString(const BitMatrix& matrix, char one, char zero, bool addSpace, bool printAsCString) { @@ -39,48 +51,46 @@ @implementation ZXIBarcodeWriter +-(CGImageRef)writeData:(NSData *)data + width:(int)width + height:(int)height + format:(ZXIFormat)format + error:(NSError *__autoreleasing _Nullable *)error { + return [self encode: NSDataToStringW(data) + width: width + height: height + format: format + encoding: CharacterSet::BINARY + error: error]; +} + -(CGImageRef)write:(NSString *)contents width:(int)width height:(int)height format:(ZXIFormat)format error:(NSError *__autoreleasing _Nullable *)error { + return [self encode: NSStringToStringW(contents) + width: width + height: height + format: format + encoding: CharacterSet::UTF8 + error: error]; +} + +-(CGImageRef)encode:(std::wstring)content + width:(int)width + height:(int)height + format:(ZXIFormat)format + encoding:(CharacterSet)encoding + error:(NSError *__autoreleasing _Nullable *)error { MultiFormatWriter writer { BarcodeFormatFromZXIFormat(format) }; + writer.setEncoding(encoding); // Catch exception for invalid formats try { - BitMatrix result = writer.encode(NSStringToStringW(contents), width, height); - int realWidth = result.width(); - int realHeight = result.height(); - -#ifdef DEBUG -// std::cout << ToString(result, 'X', ' ', false, false); -#endif - - NSMutableData *resultAsNSData = [[NSMutableData alloc] initWithLength:realWidth * realHeight]; - size_t index = 0; - uint8_t *bytes = (uint8_t*)resultAsNSData.mutableBytes; - for (int y = 0; y < realHeight; ++y) { - for (int x = 0; x < realWidth; ++x) { - bytes[index] = result.get(x, y) ? 0 : 255; - ++index; - } - } - - CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray); - - CGImageRef cgimage = CGImageCreate(realWidth, - realHeight, - 8, - 8, - realWidth, - colorSpace, - kCGBitmapByteOrderDefault, - CGDataProviderCreateWithCFData((CFDataRef)resultAsNSData), - NULL, - YES, - kCGRenderingIntentDefault); - return cgimage; + BitMatrix bitMatrix = writer.encode(content, width, height); + return [self inflate:&bitMatrix]; } catch(std::exception &e) { - if(error != nil) { + if (error != nil) { NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: [[NSString alloc] initWithUTF8String:e.what()] }; @@ -90,4 +100,37 @@ -(CGImageRef)write:(NSString *)contents } } +-(CGImageRef)inflate:(BitMatrix *)bitMatrix { + int realWidth = bitMatrix->width(); + int realHeight = bitMatrix->height(); + +#ifdef DEBUG + std::cout << ToString(bitMatrix, 'X', ' ', false, false); +#endif + + NSMutableData *resultAsNSData = [[NSMutableData alloc] initWithLength:realWidth * realHeight]; + size_t index = 0; + uint8_t *bytes = (uint8_t*)resultAsNSData.mutableBytes; + for (int y = 0; y < realHeight; ++y) { + for (int x = 0; x < realWidth; ++x) { + bytes[index] = bitMatrix->get(x, y) ? 0 : 255; + ++index; + } + } + + CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray); + + return CGImageCreate(realWidth, + realHeight, + 8, + 8, + realWidth, + colorSpace, + kCGBitmapByteOrderDefault, + CGDataProviderCreateWithCFData((CFDataRef)resultAsNSData), + NULL, + YES, + kCGRenderingIntentDefault); +} + @end From bc5d28ea62e8ddea199bb42114cb56ed92016da8 Mon Sep 17 00:00:00 2001 From: Alexander Manzer Date: Tue, 17 Oct 2023 17:50:35 +0200 Subject: [PATCH 2/4] Fix debug print in demo project ... and by the way correct also the new prototype function --- wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h | 1 - wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h index 069915b1a9..96d1875885 100644 --- a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h +++ b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h @@ -19,7 +19,6 @@ NS_ASSUME_NONNULL_BEGIN width:(int)width height:(int)height format:(ZXIFormat)format - hidden:(nullable NSString *)hidden error:(NSError *__autoreleasing _Nullable *)error; @end diff --git a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm index d9b641b63d..34646fd03d 100644 --- a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm +++ b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm @@ -31,7 +31,7 @@ } #ifdef DEBUG -std::string ToString(const BitMatrix& matrix, char one, char zero, bool addSpace, bool printAsCString) +std::string ToString(BitMatrix& matrix, char one, char zero, bool addSpace, bool printAsCString) { std::string result; result.reserve((addSpace ? 2 : 1) * (matrix.width() * matrix.height()) + matrix.height()); @@ -105,7 +105,7 @@ -(CGImageRef)inflate:(BitMatrix *)bitMatrix { int realHeight = bitMatrix->height(); #ifdef DEBUG - std::cout << ToString(bitMatrix, 'X', ' ', false, false); + std::cout << ToString(*bitMatrix, 'X', ' ', false, false); #endif NSMutableData *resultAsNSData = [[NSMutableData alloc] initWithLength:realWidth * realHeight]; From ec5317d17dd9dba6c21d7729ebabf6f31c287793 Mon Sep 17 00:00:00 2001 From: Alexander Manzer Date: Wed, 18 Oct 2023 13:52:55 +0200 Subject: [PATCH 3/4] Rename methods to writeText and writeBytes ... and integrate some other feedback --- .../Sources/Wrapper/Writer/ZXIBarcodeWriter.h | 14 +++---- .../Wrapper/Writer/ZXIBarcodeWriter.mm | 40 +++++-------------- .../ios/demo/demo/WriteViewController.swift | 2 +- 3 files changed, 19 insertions(+), 37 deletions(-) diff --git a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h index 96d1875885..f9efc0ce87 100644 --- a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h +++ b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h @@ -9,18 +9,18 @@ NS_ASSUME_NONNULL_BEGIN @interface ZXIBarcodeWriter : NSObject --(nullable CGImageRef)write:(NSString *)contents - width:(int)width - height:(int)height - format:(ZXIFormat)format - error:(NSError **)error; - --(nullable CGImageRef)writeData:(NSData *)data +-(nullable CGImageRef)writeText:(NSString *)contents width:(int)width height:(int)height format:(ZXIFormat)format error:(NSError *__autoreleasing _Nullable *)error; +-(nullable CGImageRef)writeBytes:(NSData *)data + width:(int)width + height:(int)height + format:(ZXIFormat)format + error:(NSError *__autoreleasing _Nullable *)error; + @end NS_ASSUME_NONNULL_END diff --git a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm index f795513c2b..c97d95992f 100644 --- a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm +++ b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm @@ -6,6 +6,7 @@ #import "ZXIBarcodeWriter.h" #import "MultiFormatWriter.h" #import "BitMatrix.h" +#import "BitMatrixIO.h" #import "ZXIFormatHelper.h" #import "ZXIErrors.h" #import @@ -30,32 +31,13 @@ return s; } -#ifdef DEBUG -std::string ToString(BitMatrix& matrix, char one, char zero, bool addSpace, bool printAsCString) -{ - std::string result; - result.reserve((addSpace ? 2 : 1) * (matrix.width() * matrix.height()) + matrix.height()); - for (int y = 0; y < matrix.height(); ++y) { - for (int x = 0; x < matrix.width(); ++x) { - result += matrix.get(x, y) ? one : zero; - if (addSpace) - result += ' '; - } - if (printAsCString) - result += "\\n\""; - result += '\n'; - } - return result; -} -#endif - @implementation ZXIBarcodeWriter --(CGImageRef)writeData:(NSData *)data - width:(int)width - height:(int)height - format:(ZXIFormat)format - error:(NSError *__autoreleasing _Nullable *)error { +-(CGImageRef)writeBytes:(NSData *)data + width:(int)width + height:(int)height + format:(ZXIFormat)format + error:(NSError *__autoreleasing _Nullable *)error { return [self encode: NSDataToStringW(data) width: width height: height @@ -64,11 +46,11 @@ -(CGImageRef)writeData:(NSData *)data error: error]; } --(CGImageRef)write:(NSString *)contents - width:(int)width - height:(int)height - format:(ZXIFormat)format - error:(NSError *__autoreleasing _Nullable *)error { +-(CGImageRef)writeText:(NSString *)contents + width:(int)width + height:(int)height + format:(ZXIFormat)format + error:(NSError *__autoreleasing _Nullable *)error { return [self encode: NSStringToStringW(contents) width: width height: height diff --git a/wrappers/ios/demo/demo/WriteViewController.swift b/wrappers/ios/demo/demo/WriteViewController.swift index 8f1e1f28e8..975699d248 100644 --- a/wrappers/ios/demo/demo/WriteViewController.swift +++ b/wrappers/ios/demo/demo/WriteViewController.swift @@ -15,7 +15,7 @@ class WriteViewController: UIViewController { @IBAction func textFieldChanged(_ sender: UITextField) { guard let text = sender.text, - let image = try? ZXIBarcodeWriter().write(text, width: 200, height: 200, format: .QR_CODE) + let image = try? ZXIBarcodeWriter().writeText(text, width: 200, height: 200, format: .QR_CODE) else { return } From 34d0f3de282af9298e80917a93f6292e827ddc2f Mon Sep 17 00:00:00 2001 From: Alexander Manzer Date: Wed, 18 Oct 2023 15:00:31 +0200 Subject: [PATCH 4/4] Removing the temporarily used buffer --- wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm index c97d95992f..53a2cee6b9 100644 --- a/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm +++ b/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm @@ -23,11 +23,9 @@ std::wstring s; const unsigned char *bytes = (const unsigned char *) [data bytes]; size_t len = [data length]; - wchar_t buf[len]; - for (int i = 0; i < len; ++i) { - buf[i] = bytes[i]; + for (int i = 0; i < len; ++i) { + s.push_back(bytes[i]); } - s.assign(buf, len); return s; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy