Implement show qrcode by core image generator instead of embed a web view.
This commit is contained in:
Charlie Qiu
2016-10-21 23:27:30 +08:00
parent 082c25f5e2
commit d2b8ca1c97
7 changed files with 59 additions and 70 deletions

View File

@ -9,9 +9,9 @@
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface SWBQRCodeWindowController : NSWindowController <WebFrameLoadDelegate>
@interface SWBQRCodeWindowController : NSWindowController
@property (nonatomic, strong) IBOutlet WebView *webView;
@property (nonatomic, copy) NSString *qrCode;
@property (nonatomic, weak) NSImageView *imageView;
@end

View File

@ -7,6 +7,7 @@
//
#import "SWBQRCodeWindowController.h"
@import CoreImage;
@interface SWBQRCodeWindowController ()
@ -18,18 +19,56 @@
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
[self.webView.mainFrame loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"qrcode" withExtension:@"htm"]]];
self.webView.frameLoadDelegate = self;
[self setQRCode:self.qrCode];
}
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
if (self.qrCode) {
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"genCode('%@')", _qrCode]];
}
- (void)setQRCode:(NSString*) qrCode {
CGImageRef cgImgRef = [self createQRImageForString:qrCode size:CGSizeMake(250, 250)];
NSImage *image = [[NSImage alloc]initWithCGImage:cgImgRef size:CGSizeMake(250, 250)];
self.imageView.image = image;
}
-(void)dealloc {
self.webView.frameLoadDelegate = nil;
- (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"];
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
CIContext *context = [CIContext contextWithOptions:nil];
#else
CIContext *context = [CIContext contextWithCGContext:bitmapRef options:nil];
#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;
}
@end

View File

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11201" systemVersion="16A323" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="10117"/>
<plugIn identifier="com.apple.WebKitIBPlugin" version="10117"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11201"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="SWBQRCodeWindowController">
<connections>
<outlet property="webView" destination="ojI-LR-jEw" id="bpE-vv-uIe"/>
<outlet property="imageView" destination="yRy-QH-TEe" id="aa5-Ri-k04"/>
<outlet property="window" destination="QvC-M9-y7g" id="HLy-eJ-5RD"/>
</connections>
</customObject>
@ -16,24 +16,17 @@
<window title="QR Code" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" oneShot="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES"/>
<rect key="contentRect" x="566" y="456" width="250" height="250"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
<view key="contentView" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="250" height="250"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<webView translatesAutoresizingMaskIntoConstraints="NO" id="ojI-LR-jEw">
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="yRy-QH-TEe">
<rect key="frame" x="0.0" y="0.0" width="250" height="250"/>
<webPreferences key="preferences" defaultFontSize="12" defaultFixedFontSize="12">
<nil key="identifier"/>
</webPreferences>
</webView>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" id="iAJ-tw-LNU"/>
</imageView>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="ojI-LR-jEw" secondAttribute="trailing" id="ImJ-r1-FF9"/>
<constraint firstAttribute="bottom" secondItem="ojI-LR-jEw" secondAttribute="bottom" id="LQv-2G-kyf"/>
<constraint firstItem="ojI-LR-jEw" firstAttribute="leading" secondItem="EiT-Mj-1SZ" secondAttribute="leading" id="kok-J1-X08"/>
<constraint firstItem="ojI-LR-jEw" firstAttribute="top" secondItem="EiT-Mj-1SZ" secondAttribute="top" id="voV-aN-Ggt"/>
</constraints>
</view>
</window>
</objects>

File diff suppressed because one or more lines are too long

View File

@ -1,28 +0,0 @@
<!doctype html>
<script src="jquery.min.js">
</script>
<script src="qrcode.min.js">
</script>
<style>
body {
padding: 10px;
margin: 0;
}
#qrcode {
width:230px;
height:230px;
}
</style>
<div id="qrcode"></div>
<script type="text/javascript">
function genCode(code) {
new QRCode("qrcode", {
text: code,
width: 230,
height: 230,
colorDark : "#000000",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H
});
}
</script>

File diff suppressed because one or more lines are too long