Skip to content

iOS wrapper: Add functionality of encoding binary data into Barcodes #635

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 5 commits into from
Oct 19, 2023
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
17 changes: 12 additions & 5 deletions wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +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)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
131 changes: 77 additions & 54 deletions wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#import "ZXIBarcodeWriter.h"
#import "MultiFormatWriter.h"
#import "BitMatrix.h"
#import "BitMatrixIO.h"
#import "ZXIFormatHelper.h"
#import "ZXIErrors.h"
#import <iostream>
Expand All @@ -18,69 +19,58 @@
sizeof(wchar_t));
}

#ifdef DEBUG
std::string ToString(const 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';
std::wstring NSDataToStringW(NSData *data) {
std::wstring s;
const unsigned char *bytes = (const unsigned char *) [data bytes];
size_t len = [data length];
for (int i = 0; i < len; ++i) {
s.push_back(bytes[i]);
}
return result;
return s;
}
#endif

@implementation ZXIBarcodeWriter

-(CGImageRef)write:(NSString *)contents
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
format: format
encoding: CharacterSet::BINARY
error: 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
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()]
};
Expand All @@ -90,4 +80,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
2 changes: 1 addition & 1 deletion wrappers/ios/demo/demo/WriteViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
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