Skip to content

Instantly share code, notes, and snippets.

View hasanadil's full-sized avatar

hasanadil

  • Portland, Maine, USA
View GitHub Profile
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
@interface HAColorFinder : NSObject
/*
Return the color in the image that is most dominant.
Where dominant is defined as the most number of pixels which contain the same color.
*/
-(void) fetchDominantColorFromImage:(NSImage*)image
//wait on everything to be completed
dispatch_barrier_async(weakMe.concurrentQueue, ^{
//Get the highest count amont the worker results
NSArray* allCounts = [workerResults allValues];
NSSortDescriptor* countSort = [[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO];
NSArray* sortedCounts = [allCounts sortedArrayUsingDescriptors:@[countSort]];
HAColorComponentsCount* colorComponentCount = [sortedCounts firstObject];
HAColorComponents *components = colorComponentCount.components;
//Divide up the work among workers
NSUInteger numberOfWorkers = 4;
NSUInteger pixelsPerWorker = pixelCount/numberOfWorkers;
for (NSUInteger worker = 0; worker < numberOfWorkers; worker++) {
//Submit work blocks to be executed concurrently
dispatch_async(weakMe.concurrentQueue, ^{
//Add the colors result to a trie data strucuture whose leaf is the counter, O(1) baby
//Divide up the work among workers
NSUInteger numberOfWorkers = 4;
NSUInteger pixelsPerWorker = pixelCount/numberOfWorkers;
for (NSUInteger worker = 0; worker < numberOfWorkers; worker++) {
//Submit work blocks to be executed concurrently
dispatch_async(weakMe.concurrentQueue, ^{
//Add the colors result to a trie data strucuture whose leaf is the counter, O(1) baby
-(void) fetchDominantColorFromImage:(NSImage*)image withCompletion:(void(^)(NSColor *color, NSTimeInterval processingTime))completion {
...
__weak typeof(self) weakMe = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[weakMe pixelsFromImage:image withCompletion:^(UInt32 *pixels, NSUInteger pixelCount) {
....
-(void) pixelsFromImage:(NSImage*)image
withCompletion:(void(^)(UInt32 *pixels, NSUInteger pixelCount))completion {
//Extract pixels from the image in a 1D array
CFDataRef inputData = (__bridge CFDataRef)[image TIFFRepresentation];
CGImageSourceRef inputSource = CGImageSourceCreateWithData(inputData, NULL);
CGImageRef inputCGImage = CGImageSourceCreateImageAtIndex(inputSource, 0, NULL);
NSUInteger width = CGImageGetWidth(inputCGImage);
NSUInteger height = CGImageGetHeight(inputCGImage);
NSUInteger numberOfPixels = height * width;
class 💩💩💩💩 {
func 💩💩💩(😎: Int, 🐯: Int) -> Int {
return 😎 + 🐯;
}
}
let 🐔 = 3;
let 😥 = 🐔 + 2;
let 💩 = 💩💩💩💩();
print(💩.💩💩💩(🐔, 🐯:😥));
let camera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var error : NSError? = nil
let cameraInput : AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(camera, error: &error) as? AVCaptureDeviceInput
self.session.addInput(cameraInput)
if error != nil || cameraInput == nil {
//Awesome error handling :D
println(error!.localizedDescription)
}
else {
let allowedBarcodes = [AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode