Struts2進修筆記(3)-DMI靜態挪用方法。本站提示廣大學習愛好者:(Struts2進修筆記(3)-DMI靜態挪用方法)文章只能為提供參考,不一定能成為您想要的結果。以下是Struts2進修筆記(3)-DMI靜態挪用方法正文
現在二維碼到處可見,不管是什物商品照樣各類禮券都少不了二維碼的身影。而手機等挪動裝備又成為二維碼的一個很好的運用平台,不論是生成二維碼照樣掃碼二維碼。本篇文章從生成二維碼、掃描二維碼睜開剖析,經由過程內容剖析二維碼用起來也很easy了。
起首說下生成二維碼
二維碼可以寄存純文本、咭片或許URL
其次生成二維碼的步調:
導入CoreImage框架
再次經由過程濾鏡CIFilter生成二維碼
1、創立過濾器
2、恢復濾鏡的默許屬性
3、設置內容
4、獲得輸入文件
5、顯示二維碼
代碼完成 CoreImage
// 二維碼的生成 // 1、創立過濾器 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2、恢復濾鏡的默許屬性 [filter setDefaults]; // 3、設置內容 NSString *str = @"這是一個二維碼的生成成果"; NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; // 應用KVO設置屬性 [filter setValue:data forKey:@"inputMessage"]; // 4、獲得輸入文件 CIImage *outputImage = [filter outputImage]; // 5、顯示二維碼 self.imageView.image = [UIImage imageWithCIImage:outputImage];
如許顯示的圖片不是很清楚,可以本身重畫圖片
從新生成高清圖片:網上找便可,詳細進程可臨時不關懷
/**
* 依據CIImage生成指定年夜小的UIImage
*
* @param image CIImage
* @param size 圖片寬度
*/
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1.創立bitmap;
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);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 2.保留bitmap到圖片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
還有就是設置內容為網址時,假如帶有協定頭的話,會主動翻開網頁。
NSString *str = @http://www.百度.com;
必需帶有協定頭能力翻開
回到頂部
掃描二維碼
AVFoundation框架
二維碼的掃描進程
1、創立捕獲會話AVCaptureSession
添加輸出裝備(數據從攝像頭輸出) AVCaptureDevice AVCaptureDeviceInput
2、添加輸入數據(示例對象-->類對象-->元類對象-->根元類對象) AVCaptureMetadataOutput
2.1.設置輸出元數據的類型(類型是二維碼數據) setMetadataObjectTypes
3、添加掃描圖層 AVCaptureVideoPreviewLayer
4、開端掃描 startRunning
5、完成回調署理辦法,獲得掃描成果 captureOutput: :
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController () <AVCaptureMetadataOutputObjectsDelegate>
/**顯示圖層*/
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *layer;
/**捕獲會話*/
@property (nonatomic, strong) AVCaptureSession *session;
@end
@implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 二維碼的掃描
// 1、創立捕獲會話
AVCaptureSession *session = [[AVCaptureSession alloc] init];
self.session = session;
// 2.添加輸出裝備(數據從攝像頭輸出)
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
[session addInput:input];
// 3、添加輸入數據(示例對象-->類對象-->元類對象-->根元類對象)
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[session addOutput:output];
// 3.1.設置輸出元數據的類型(類型是二維碼數據)
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
// 4、添加掃描圖層
AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session];
layer.frame = self.view.bounds;
[self.view.layer addSublayer:layer];
self.layer = layer;
// 5、開端掃描
[session startRunning];
}
/**
* 完成output的回調辦法
*/
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
// 數組metadataObjects中寄存成果數據
if (metadataObjects.count > 0) {
// 獲得終究的讀取成果
AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
NSLog(@"%@",object.stringValue);
[self.session stopRunning];
[self.layer removeFromSuperlayer];
}
else
{
NSLog(@"沒有掃描到數據");
}
}
總結一句話:這個二維碼應用起來也不難
本文就到此為止,IOS筆記061之二維碼的生成和掃描願望在往後的任務和進修可以或許贊助到年夜家。上面文章給年夜家分享若何在蘋果iOS裝備上應用二維碼,須要懂得的同伙點擊這裡。