您的位置:首页 > 产品设计 > UI/UE

美图案例,相册,AES加密,MD5, UIDevice_设备 ZXing

2016-01-05 00:00 543 查看
摘要: IOS7原生API进行二维码条形码的扫描

//
// ViewController.m
// 我的美图软件
//
// Created by DC020 on 16/1/5.
// Copyright (c) 2016年 Bill. All rights reserved.
//

#import "ViewController.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
UIImagePickerController *_imagePickerController;//系统照片选择控制器
//Core Image
CIContext *_context;//CI的上下文
CIImage *_oldImage;//需要编辑前的图片
CIImage *_newImage;//编辑后
CIFilter *_filter;//滤镜
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//滤镜
// [self showAllFilters];
//初始化
[self myinit];
}
-(void)myinit{
_imagePickerController = [[UIImagePickerController alloc]init];
_imagePickerController.delegate = self;

//初始化CI上下文
_context = [CIContext contextWithOptions:nil];
//初始化滤镜
_filter = [CIFilter filterWithName:@"CIColorControls"];

}

//查看所有内置的滤镜
-(void)showAllFilters{
NSArray *array = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
NSLog(@"%@",array);
NSLog(@"%ld",[array count]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)open:(id)sender {
//打开相册
[self presentViewController:_imagePickerController animated:YES completion:nil];
}

- (IBAction)save:(id)sender {
//保存图片到相册
UIImageWriteToSavedPhotosAlbum(_imageView.image, nil, nil, nil);

//保存提示
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"信息" message:@"成功保存" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark 代理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//关闭相册
[self dismissViewControllerAnimated:YES completion:nil];
//获取选择的图片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
_imageView.image = image;
//初始化CIImage
_oldImage = [CIImage imageWithCGImage:[image CGImage]];
//设置滤镜的输入图片
[_filter setValue:_oldImage forKey:@"inputImage"];
}
//CI->CG->UI
-(void)setImage{
//取得输出图像
_newImage = [_filter outputImage];
//
CGImageRef temp = [_context createCGImage:_newImage fromRect:[_oldImage extent]];
_imageView.image = [UIImage imageWithCGImage:temp];
CGImageRelease(temp); //释放CGImage对象
}

//饱和度
- (IBAction)changeStaturation:(UISlider *)sender {

[_filter setValue:[NSNumber numberWithFloat:sender.value] forKey:@"inputSaturation"];
[self setImage];
}
//亮度
- (IBAction)changeBrightness:(UISlider *)sender {
[_filter setValue:[NSNumber numberWithFloat:sender.value] forKey:@"inputBrightness"];
[self setImage];
}
//对比度
- (IBAction)changeContrast:(UISlider *)sender {
[_filter setValue:[NSNumber numberWithFloat:sender.value] forKey:@"inputContrast"];
[self setImage];
}
@end

//
// ViewController.m
// 相册
//
// Created by DC020 on 16/1/5.
// Copyright (c) 2016年 Bill. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
UIImagePickerController *_imagePickerContrller;//系统照片选择控制器
UIImageView *_imageView;//用来显示选择的图片
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self layout];
}
-(void)layout{
//初始化图片选择器
_imagePickerContrller = [[UIImagePickerController alloc]init];
_imagePickerContrller.delegate = self;
//初始化图片显示
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 64, 355, 593)];
_imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:_imageView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)open:(id)sender {
[self presentViewController:_imagePickerContrller animated:YES completion:nil];

}
#pragma mark 图片选择器选择图片代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//关闭相册
[self dismissViewControllerAnimated:YES completion:nil];
//取得选择的图片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
_imageView.image = image;
}
@end

//
// main.m
// AES加密
//
// Created by DC020 on 16/1/5.
// Copyright (c) 2016年 Bill. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "NSData+AES.h"
int main() {
NSString *key = @"12";//钥匙
NSString *secret = @"加密内容";//准备加密的内容
//加密
NSData *plain = [secret dataUsingEncoding:NSUTF8StringEncoding];
NSData *cipher = [plain AES256EncryptWithKey:key];
NSLog(@"加密后:%@",cipher);

NSString *result = [[NSString alloc]initWithData:cipher encoding:NSUTF8StringEncoding];
NSLog(@"----%@",result);

//解密
for (int i = 1; i <= 100; i++) {

plain = [cipher AES256DecryptWithKey:[NSString stringWithFormat:@"%d",i]];
result = [[NSString alloc]initWithData:plain encoding:NSUTF8StringEncoding];

if (result != NULL) {
NSLog(@"解密后:%@",result);
NSLog(@"%d",i);

}
}
return 0;
}

//
// main.m
// MD5
//
// Created by DC020 on 16/1/5.
// Copyright (c) 2016年 Bill. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "NSString+Extend.h"
int main(int argc, const char * argv[]) {
[@"习近平" sayHi];
NSLog(@"%@",[@"SunshineBoy" md5]);

//IOS7 以后提供了 base64 的转码方式
//编码
NSString *password = @"123456";
NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
NSString * result = [passwordData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
NSLog(@"%@",result);
//解码
NSData *decodeData = [[NSData alloc]initWithBase64EncodedString:result options:0];
NSString *decodeStr = [[NSString alloc]initWithData:decodeData encoding:NSASCIIStringEncoding];
NSLog(@"%@",decodeStr);
return 0;
}

#import <Foundation/Foundation.h>

@interface NSString(Extend)
-(void)sayHi;
-(NSString *)md5;
@end

#import "NSString+Extend.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NSString(Extend)
-(void)sayHi{
NSLog(@"%@ 说你好",self);
}
-(NSString *)md5{
//转换成c语言的字符串
const char *cStr = [self UTF8String];
//md5加密的结果是128位,需要开辟一个16字节的空间
unsigned char result[16];
//调用加密函数
CC_MD5(cStr, (unsigned int)strlen(cStr), result);
//通过上面这个方法获取的MD5是一个16个字符的数组--->32位的MD5值
//%02X
NSMutableString *str = [NSMutableString stringWithCapacity:10];
for (int i = 0; i < 16; i++) {
[str appendString:[NSString stringWithFormat:@"%02X",result[i]]];
}
return str;
}
@end

//
// ViewController.m
// UIDevice_设备
//
// Created by DC020 on 16/1/5.
// Copyright (c) 2016年 Bill. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
UIDevice *mine = [UIDevice currentDevice];
NSLog(@"name:%@",mine.name);
NSLog(@"model:%@",mine.model);
NSLog(@"localizedModel:%@",mine.localizedModel);
NSLog(@"systemName:%@",mine.systemName);
NSLog(@"systemVersion:%@",mine.systemVersion);
mine.batteryMonitoringEnabled = YES;
NSLog(@"当前电池容量为:%f%%",mine.batteryLevel *100);
mine.proximityMonitoringEnabled = YES;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

#import "ViewController.h"
#import <ZXingObjC/ZXingObjC.h>

@interface ViewController ()
{
UIImage *imageResult;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self encoding];
[self decoding];
}

//Quick Response Code
#pragma mark 编码
- (void)encoding{
NSError *error = nil;
ZXMultiFormatWriter *writer = [ZXMultiFormatWriter writer];
ZXBitMatrix* result = [writer encode:@"http://www.sina.com.cn"
format:kBarcodeFormatQRCode
width:500
height:500
error:&error];
if (result) {
CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];

// This CGImageRef image can be placed in a UIImage, NSImage, or written to a file.
imageResult = [UIImage imageWithCGImage:image];
UIImageView *result = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 345, 345)];
result.image = imageResult;
[self.view addSubview:result];
} else {
NSString *errorMessage = [error localizedDescription];
NSLog(@"出错:%@",errorMessage);
}
}

#pragma mark 解码
- (void)decoding{
CGImageRef imageToDecode = [imageResult CGImage]; // Given a CGImage in which we are looking for barcodes

ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode];
ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];

NSError *error = nil;

// There are a number of hints we can give to the reader, including
// possible formats, allowed lengths, and the string encoding.
ZXDecodeHints *hints = [ZXDecodeHints hints];

ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];
ZXResult *result = [reader decode:bitmap
hints:hints
error:&error];
if (result) {
// The coded result as a string. The raw data can be accessed with
// result.rawBytes and result.length.
NSString *contents = result.text;
NSLog(@"二维码结果:%@",contents);
// The barcode format, such as a QR code or UPC-A
ZXBarcodeFormat format = result.barcodeFormat;
NSLog(@"该码格式为:%u", format);
} else {
// Use error to determine why we didn't get a result, such as a barcode
// not being found, an invalid checksum, or a format inconsistency.
}
}

//需要真机
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>//用于处理采集信息的代理
{
AVCaptureSession * session;//输入输出的中间桥梁
}
@end
@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//获取摄像设备
AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//创建输入流
AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
//创建输出流
AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
//设置代理 在主线程里刷新
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

//初始化链接对象
session = [[AVCaptureSession alloc]init];
//高质量采集率
[session setSessionPreset:AVCaptureSessionPresetHigh];

// 1、这个CGRect参数和普通的Rect范围不太一样,它的四个值的范围都是0-1,表示比例。
// 2、经过测试发现,这个参数里面的x对应的恰恰是距离左上角的垂直距离,y对应的是距离左上角的水平距离。
// 3、宽度和高度设置的情况也是类似。
// 3、举个例子如果我们想让扫描的处理区域是屏幕的下半部分,我们这样设置
// output.rectOfInterest=CGRectMake(0.5,0,0.5, 1);

[session addInput:input];
[session addOutput:output];
//设置扫码支持的编码格式(如下设置条形码和二维码兼容)
output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

//创建摄像头显示图层
AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session];
layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
layer.frame=self.view.layer.bounds;
[self.view.layer insertSublayer:layer atIndex:0];
//开始捕获
[session startRunning];
}

#pragma mark 信息捕获代理方法
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects.count>0) {
//[session stopRunning];
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
//输出扫描字符串
NSLog(@"%@",metadataObject.stringValue);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: