2016-06-09 06:59:42 +08:00
|
|
|
//
|
|
|
|
// QRCodeWindowController.m
|
|
|
|
// shadowsocks
|
|
|
|
//
|
|
|
|
// Created by clowwindy on 10/12/14.
|
|
|
|
// Copyright (c) 2014 clowwindy. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "SWBQRCodeWindowController.h"
|
2016-10-21 23:27:30 +08:00
|
|
|
@import CoreImage;
|
2016-06-09 06:59:42 +08:00
|
|
|
|
|
|
|
@interface SWBQRCodeWindowController ()
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SWBQRCodeWindowController
|
|
|
|
|
|
|
|
- (void)windowDidLoad {
|
|
|
|
[super windowDidLoad];
|
|
|
|
|
|
|
|
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
|
2017-09-16 17:16:51 +08:00
|
|
|
[self setQRCode:self.qrCode withOverlayText:@"SIP002"];
|
2016-06-09 06:59:42 +08:00
|
|
|
}
|
|
|
|
|
2017-08-26 02:08:27 +08:00
|
|
|
- (void)setQRCode:(NSString*) qrCode withOverlayText: (NSString*) text {
|
2016-10-21 23:27:30 +08:00
|
|
|
CGImageRef cgImgRef = [self createQRImageForString:qrCode size:CGSizeMake(250, 250)];
|
|
|
|
|
|
|
|
NSImage *image = [[NSImage alloc]initWithCGImage:cgImgRef size:CGSizeMake(250, 250)];
|
2017-08-26 02:08:27 +08:00
|
|
|
if (text) {
|
|
|
|
// Draw overlay text
|
|
|
|
NSDictionary* attrs = @{
|
|
|
|
NSForegroundColorAttributeName: [NSColor colorWithRed:28/255.0 green:155/255.0 blue:71/255.0 alpha:1],
|
|
|
|
NSBackgroundColorAttributeName: [NSColor whiteColor],
|
|
|
|
NSFontAttributeName: [NSFont fontWithName:@"Helvetica" size:(CGFloat)16],
|
|
|
|
};
|
|
|
|
NSMutableAttributedString* attrsText = [[NSMutableAttributedString alloc] initWithString: text
|
|
|
|
attributes: attrs];
|
|
|
|
|
|
|
|
[image lockFocus];
|
2017-09-16 17:16:51 +08:00
|
|
|
[attrsText drawAtPoint: NSMakePoint(100, 5)];
|
2017-08-26 02:08:27 +08:00
|
|
|
[image unlockFocus];
|
|
|
|
}
|
2016-10-21 23:27:30 +08:00
|
|
|
self.imageView.image = image;
|
2016-06-09 06:59:42 +08:00
|
|
|
}
|
|
|
|
|
2016-10-21 23:27:30 +08:00
|
|
|
- (CGImageRef)createQRImageForString:(NSString *)string size:(CGSize)size {
|
|
|
|
// Setup the QR filter with our string
|
|
|
|
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
|
|
|
|
[filter setDefaults];
|
|
|
|
|
|
|
|
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
[filter setValue:data forKey:@"inputMessage"];
|
2017-08-26 02:08:27 +08:00
|
|
|
/*
|
|
|
|
L: 7%
|
|
|
|
M: 15%
|
|
|
|
Q: 25%
|
|
|
|
H: 30%
|
|
|
|
*/
|
|
|
|
[filter setValue:@"Q" forKey:@"inputCorrectionLevel"];
|
|
|
|
|
2016-10-21 23:27:30 +08:00
|
|
|
CIImage *image = [filter valueForKey:@"outputImage"];
|
|
|
|
|
|
|
|
// Calculate the size of the generated image and the scale for the desired image size
|
|
|
|
CGRect extent = CGRectIntegral(image.extent);
|
|
|
|
CGFloat scale = MIN(size.width / CGRectGetWidth(extent), size.height / CGRectGetHeight(extent));
|
|
|
|
|
|
|
|
// Since CoreImage nicely interpolates, we need to create a bitmap image that we'll draw into
|
|
|
|
// a bitmap context at the desired size;
|
|
|
|
size_t width = CGRectGetWidth(extent) * scale;
|
|
|
|
size_t height = CGRectGetHeight(extent) * scale;
|
|
|
|
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
|
|
|
|
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
|
|
|
|
|
|
|
|
#if TARGET_OS_IPHONE
|
2017-10-21 10:09:59 +08:00
|
|
|
CIContext *context = [CIContext contextWithOptions: @{kCIContextUseSoftwareRenderer: true}];
|
2016-10-21 23:27:30 +08:00
|
|
|
#else
|
2017-10-21 10:09:59 +08:00
|
|
|
CIContext *context = [CIContext contextWithCGContext:bitmapRef options:@{kCIContextUseSoftwareRenderer: @true}];
|
2016-10-21 23:27:30 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
|
|
|
|
|
|
|
|
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
|
|
|
|
CGContextScaleCTM(bitmapRef, scale, scale);
|
|
|
|
CGContextDrawImage(bitmapRef, extent, bitmapImage);
|
|
|
|
|
|
|
|
// Create an image with the contents of our bitmap
|
|
|
|
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
CGContextRelease(bitmapRef);
|
|
|
|
CGImageRelease(bitmapImage);
|
|
|
|
|
|
|
|
return scaledImage;
|
2016-06-09 06:59:42 +08:00
|
|
|
}
|
|
|
|
|
2017-01-12 11:58:57 +08:00
|
|
|
- (IBAction) copyQRCode: (id) sender{
|
|
|
|
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
|
|
|
|
[pasteboard clearContents];
|
|
|
|
NSArray *copiedObjects = [NSArray arrayWithObject: self.imageView.image];
|
|
|
|
[pasteboard writeObjects:copiedObjects];
|
|
|
|
}
|
|
|
|
|
2017-08-01 21:40:09 +08:00
|
|
|
- (void)flagsChanged:(NSEvent *)event {
|
|
|
|
NSUInteger modifiers = event.modifierFlags & NSDeviceIndependentModifierFlagsMask;
|
|
|
|
if (modifiers & NSAlternateKeyMask) {
|
2017-09-16 17:16:51 +08:00
|
|
|
[self setQRCode:self.legacyQRCode withOverlayText:@"Legacy"];
|
2017-08-01 21:40:09 +08:00
|
|
|
} else {
|
2017-09-16 17:16:51 +08:00
|
|
|
[self setQRCode:self.qrCode withOverlayText:@"SIP002"];
|
2017-08-01 21:40:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-09 06:59:42 +08:00
|
|
|
@end
|