Giter VIP home page Giter VIP logo

wuchangqi / syqrcodedemo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from reesun1130/syqrcodedemo

0.0 1.0 0.0 1.4 MB

/**SYQRCode:IOS原生API,需要IOS7.0及以上系统支持。用法: //扫描二维码 SYQRCodeViewController *aaa = [[SYQRCodeViewController alloc] init]; aaa.SYQRCodeSuncessBlock = ^(NSString *qrString){ self.saomiaoLabel.text = qrString; }; aaa.SYQRCodeCancleBlock = ^(SYQRCodeViewController *are){ self.saomiaoLabel.text = @"扫描取消~"; [are dismissViewControllerAnimated:YES completion:nil]; }; [self presentViewController:aaa animated:YES completion:nil];**/

Objective-C 100.00%

syqrcodedemo's Introduction

SYQRCodeDemo

SYQRCode:低仿微信二维码扫描,IOS原生API,需要IOS7.0及以上系统支持。简单易用,使用block做回调处理。 fix crash ---- Terminating app due to uncaught exception NSInvalidArgumentException , reason: [AVCaptureMetadataOutput setMetadataObjectTypes:] unsupported type found. Use availableMetadataObjectTypes.

用法: ###使用前请判断是否允许访问相机:

+ (BOOL)isAVCaptureActive
{
  AVCaptureDevice *aDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

  NSError *inputError = nil;
  AVCaptureDeviceInput *aInput = [AVCaptureDeviceInput deviceInputWithDevice:aDevice error:&inputError];

  if (aInput == nil)
  {
      SYLog(@"init AVCapture fail--%@",inputError);

      return NO;
  }

  return YES;
}

if(isAVCaptureActive)
{
   SYQRCodeViewController *syqrc = [[SYQRCodeViewController alloc] init];
   syqrc.SYQRCodeSuncessBlock = ^(NSString *qrString){
     self.saomiaoLabel.text = qrString;
   };

   syqrc.SYQRCodeCancleBlock = ^(SYQRCodeViewController *aqrc){
     self.saomiaoLabel.text = @"扫描取消~";
     [aqrc dismissViewControllerAnimated:YES completion:nil];
   };
   [self presentViewController:syqrc animated:YES completion:nil];
}

/*AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
if(status == AVAuthorizationStatusAuthorized)
{
   SYQRCodeViewController *syqrc = [[SYQRCodeViewController alloc] init];
   syqrc.SYQRCodeSuncessBlock = ^(NSString *qrString){
     self.saomiaoLabel.text = qrString;
   };

   syqrc.SYQRCodeCancleBlock = ^(SYQRCodeViewController *aqrc){
     self.saomiaoLabel.text = @"扫描取消~";
     [aqrc dismissViewControllerAnimated:YES completion:nil];
   };
   [self presentViewController:syqrc animated:YES completion:nil];
}*/

效果如下:

image

另附IOS7二维码生成方法:

###- (UIImage *)makeQRCodeImage ###{ CIFilter *filter_qrcode = [CIFilter filterWithName:@"CIQRCodeGenerator"]; [filter_qrcode setDefaults];

NSData *data = [@"https://github.com/reesun1130" dataUsingEncoding:NSUTF8StringEncoding];
[filter_qrcode setValue:data forKey:@"inputMessage"];
CIImage *outputImage = [filter_qrcode outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:outputImage
                                   fromRect:[outputImage extent]];

UIImage *image = [UIImage imageWithCGImage:cgImage
                                     scale:1.
                               orientation:UIImageOrientationUp];

//大小控制
UIImage *resized = [self resizeImage:image
                         withQuality:kCGInterpolationNone
                                rate:5.0];

//颜色控制
resized = [self imageBlackToTransparent:resized withRed:30 andGreen:191 andBlue:109];

CGImageRelease(cgImage);
return resized;

###}

###- (UIImage *)resizeImage:(UIImage *)image

withQuality:(CGInterpolationQuality)quality

rate:(CGFloat)rate

###{ UIImage *resized = nil; CGFloat width = image.size.width * rate; CGFloat height = image.size.height * rate;

UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, quality);
[image drawInRect:CGRectMake(0, 0, width, height)];
resized = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return resized;

###}

###void ProviderReleaseData (void *info, const void data, size_t size){ free((void)data); ###}

###- (UIImage*)imageBlackToTransparent:(UIImage*)image withRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue ###{ const int imageWidth = image.size.width; const int imageHeight = image.size.height;

size_t      bytesPerRow = imageWidth * 4;
uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);

// 遍历像素
int pixelNum = imageWidth * imageHeight;
uint32_t *pCurPtr = rgbImageBuf;

for (int i = 0; i < pixelNum; i++, pCurPtr++)
{
    if ((*pCurPtr & 0xFFFFFF00) < 0x99999900) // 将白色变成透明
    {
        // 改成下面的代码,会将图片转成想要的颜色
        uint8_t* ptr = (uint8_t*)pCurPtr;
        ptr[3] = red; //0~255
        ptr[2] = green;
        ptr[1] = blue;
    }
    else
    {
        uint8_t* ptr = (uint8_t*)pCurPtr;
        ptr[0] = 0;
    }
}

// 输出图片
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData);
CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpace, kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);

UIImage *resultUIImage = [UIImage imageWithCGImage:imageRef];

// 清理空间
CGImageRelease(imageRef);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

return resultUIImage;

###}

syqrcodedemo's People

Contributors

reesun1130 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.