Creating QR barcodes with ZXingObjC on Mac - objective-c

I'm trying to use http://github.com/TheLevelUp/ZXingObjC to create QR codes on my Mac app.
It works for every barcode types, but returns nil on QRcode! both 'result' and 'error' is empty. here's my code:
NSError* error = nil;
ZXMultiFormatWriter* writer = [[ZXMultiFormatWriter alloc] init];
ZXBitMatrix* result = [writer encode:#"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678"
format:kBarcodeFormatQRCode
width:1750
height:1750 hints:[[ZXEncodeHints alloc] init] error:&error];
if (result) {
CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];
self.image.image = [[NSImage alloc] initWithCGImage:image size:NSMakeSize(1750, 1750)];
} else {
NSLog(#"error: %#", error);
}
What's wrong on it?

I had the same issue. Here is workaround for this.
Open file ZXingObjC\qrcode\encoder\ZXEncoder.m
Find this row: int minPenalty = NSIntegerMax;. There must be a warning on it: Implicit conversion from 'long' to 'int' changes 9223372036854775807 to -1. That's the reason of the problem. NSIntegerMax returns 9223372036854775807 on my 64-bit Mac and minPenalty gets -1 value (since int type cannot store such a big number).
Replace the NSIntegerMax by INT_MAX. It should return the correct value: 2147483647. That's the number NSIntegerMax returns on 32-bit machines according to the answer to this question.
Run the app and you'll get your QR code!

Try to use another method, not this with HINTS, use just:
[writer encode:#"yourmeganumber" format:kBarcodeFormatQRCode width:xxxx height:xxxx error:&error];
This works for me
Try and let me know

Related

Objective-C EXC-BAD-ACCESS - resolving without ARC?

For reference I'm using Xcode 11.3
I've got an issue with an object that has been released and it's causing EXC BAD ACCESS.
The good news is that I know exactly what the object is.
What I don't know is how to solve for it.
Here's the code where the crash occurs...
- (void)didSendPTPCommand:(NSData*)command inData:(NSData*)data response:(NSData*)response error:(NSError*)error contextInfo:(void*)contextInfo
{
NSLog(#"%# %# %# %#", NSStringFromSelector(_cmd), data, response, error);
PTPOperationRequest* ptpRequest = (__bridge PTPOperationRequest*)contextInfo;
PTPOperationResponse* ptpResponse = NULL;
if ( ptpRequest )
The crash is on:
PTPOperationRequest* ptpRequest = (__bridge PTPOperationRequest*)contextInfo;
This code is being called out of this code:
ptpData = NULL;
PTPOperationRequest* request = [[PTPOperationRequest alloc] init];
request.operationCode = PTPOperationCodeInitiateCapture;
request.numberOfParameters = 0;
commandBuffer = request.commandBuffer;
[camera requestSendPTPCommand:commandBuffer
outData:NULL
sendCommandDelegate:self
didSendCommandSelector:#selector(didSendPTPCommand:inData:response:error:contextInfo:)
contextInfo:(__bridge void * _Nullable)(request)];
where of curse I'm trying to pass "request".
A long time ago I'd have managed this with retain/release - not any more. What do I do now?
David
I think I now have the solution.
__bridge_retained
So the following code is changed thus:
[camera requestSendPTPCommand:commandBuffer
outData:NULL
sendCommandDelegate:self
didSendCommandSelector:#selector(didSendPTPCommand:inData:response:error:contextInfo:)
contextInfo:(__bridge_retained void * _Nullable)(request)];
I have tested and it's working.
David

Persisting bookmark in core-data

I have an OSX application that is supposed to have a list of files from anywhere in the user's disk.
The first version of the app saves the path to these files in a core-data model.
However, if the file is moved or renamed, the tool loses its purpose and the app can crash.
So I decided to use bookmarks. It seems to be working, but every time I try to recover the data, I get the old path of the files. Why is that? What am I missing?
My core-data entity uses a binary data field to persist the bookmark.
The bookmark itself is done like this:
NSData * bookmark = [filePath bookmarkDataWithOptions:NSURLBookmarkCreationMinimalBookmark
includingResourceValuesForKeys:NULL
relativeToURL:NULL
error:NULL];
And on loading the application, I have a loop to iterate all the table and recover the bookmark like this:
while (object = [rowEnumerator nextObject]) {
NSError * error = noErr;
NSURL * bookmark = [NSURL URLByResolvingBookmarkData:[object fileBookmark]
options:NSURLBookmarkResolutionWithoutUI
relativeToURL:NULL
bookmarkDataIsStale:NO
error:&error];
if (error != noErr)
DDLogCError(#"%#", [error description]);
DDLogCInfo(#"File Path: %#", [bookmark fileReferenceURL]);
}
If I rename the file, the path is null. I see no difference between storing this NSData object and a string with the path. So I am obviously missing something.
Edit:
I also often get an error like this: CFURLSetTemporaryResourcePropertyForKey failed because it was passed this URL which has no scheme.
I appreciate any help, thanks!
I can't find any issues in my code, so I changed it.
After looking for the reason of the "no scheme" message, I came to the conclusion some third-party application is required for this code to work, and that's undesirable.
I am now using aliases. This is how I create them:
FSRef fsFile, fsOriginal;
AliasHandle aliasHandle;
NSString * fileOriginalPath = [[filePath absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
OSStatus status = FSPathMakeRef((unsigned char*)[fileOriginalPath cStringUsingEncoding: NSUTF8StringEncoding], &fsOriginal, NULL);
status = FSPathMakeRef((unsigned char*)[fileOriginalPath cStringUsingEncoding: NSUTF8StringEncoding], &fsFile, NULL);
OSErr err = FSNewAlias(&fsOriginal, &fsFile, &aliasHandle);
NSData * aliasData = [NSData dataWithBytes: *aliasHandle length: GetAliasSize(aliasHandle)];
And now I recover the path like this:
while (object = [rowEnumerator nextObject]) {
NSData * aliasData = [object fileBookmark];
NSUInteger aliasLen = [aliasData length];
if (aliasLen > 0) {
FSRef fsFile, fsOriginal;
AliasHandle aliasHandle;
OSErr err = PtrToHand([aliasData bytes], (Handle*)&aliasHandle, aliasLen);
Boolean changed;
err = FSResolveAlias(&fsOriginal, aliasHandle, &fsFile, &changed);
if (err == noErr) {
char pathC[2*1024];
OSStatus status = FSRefMakePath(&fsFile, (UInt8*) &pathC, sizeof(pathC));
NSAssert(status == 0, #"FSRefMakePath failed");
NSLog(#"%#", [NSString stringWithCString: pathC encoding: NSUTF8StringEncoding]);
} else {
NSLog(#"The file disappeared!");
}
} else {
NSLog(#"CardCollectionUserDefault was zero length");
}
}
However, I am still curious on why my previous code failed. I appreciate any thoughts on that. Thanks!

sqlite3 error on compilation

I am coding on ios7 using Xcode5 and the compiler puts me this error.
implicit conversion loses integer precision sqlite3_int64 aka long long to int.
Below code below:
if([self chatId] != nil) {
[chatId release];
}
chatId = [[NSNumber alloc] initWithInt:sqlite3_last_insert_rowid(database)];
sqlite3_finalize(sqlStatement);
Any help?
The SQLite3 Row ID is a unique 64-bit signed integer.
Try this:
chatId = [[NSNumber alloc] initWithLongLong:sqlite3_last_insert_rowid(database)];
see implementation of method sqlite3_last_insert_rowid, it returns a long long int value, so you need to
[NSNumber alloc] initWithLongLong: sqlite3_last_insert_rowid(database)];
SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
return db->lastRowid;
}

Try to create NSString from bytes. It always returns an empty string

I am trying to create a string from bytes a received via network. The NSString I het is always an empty one.
if (stringLength > 0) {
NSData *bytes = [[NSData alloc] initWithBytes:data+1 length:stringLength];
result = [[NSString alloc] initWithData:bytes encoding:NSUTF8StringEncoding];
//result = [[NSString alloc] initWithBytes:data+1 length:stringLength encoding:NSASCIIStringEncoding];
}
As I said I get an empty NSString. The string is a base64 encoded value so it should be valid utf-8 since it only contains ascii symbols.
Maybe your raw bytes aren't UTF-8 after all. Or maybe, because you're passing data+1 instead of data itself, you're causing the encoding attempt to fail because the process thinks there's an improper UTF-8 encoding sequence. (Forgive me if I'm being presumptuous, but you have to take everything into account.) In any case, you're relying heavily on your assumption, and that's a trap we all fall into now and then.
Here's a strategy for you. If your attempt at creating an NSString instance using NSUTF8StringEncoding returns nil, then try using NSISOLatin1StringEncoding. And if that returns nil, then try using NSMacOSRomanStringEncoding.
Even if, after all of that, you get a string that's not quite right, it's still better than a nil string in that it could help to show you if there's some other area in which you've made a mistake.
Good luck to you in your endeavors.
Check if it's really base64 encoded..
CFErrorRef error = NULL;
CFDataRef decodedData;
SecTransformRef decoder;
decoder = SecDecodeTransformCreate(kSecBase64Encoding, &error);
if (error) {
CFShow(error);
exit(-1);
}
SecTransformSetAttribute(decoder,
kSecTransformInputAttributeName,
(CFDataRef *)bytes,
&error);
if (error) {
CFShow(error);
exit(-1);
}
decodedData = SecTransformExecute(decoder, &error);
if (error) {
CFShow(error);
exit(-1);
}

Weird IF THAN not working with Requested data from URL text problem

Hey all, i am checking for an internet connection by checking for a file on my server. The file only has the word LIVE displayed on the page. No HTML or anything else is there, just the word LIVE.
When i run this code, i do get the NSLog as saying "LIVE" but once i go to check it with the IF statement, it fails and i just do not know why???
NSString* myFile = [NSString stringWithFormat:#"http://www.xxx.com/iPodTouchPing.html"];
NSString* myFileURLString = [myFile stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSData *myFileData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myFileURLString]];
NSString *returnedMyFileContents=[[[NSString alloc] initWithData:myFileData encoding:NSASCIIStringEncoding] autorelease];
NSLog(#"%#", returnedMyFileContents);
if (returnedMyFileContents == #"LIVE") {
NSLog(#"LIVE!");
}else{
NSLog(#"Not Live");
}
What am i doing wrong? I can not seem to find the reason??
David
You can't compare strings like that in Objective C - you're just comparing their addresses, not their contents. Change your code to this:
if ([returnedMyFileContents isEqualToString:#"LIVE"]) {
NSLog(#"LIVE!");
} else {
NSLog(#"Not Live");
}