zoukankan      html  css  js  c++  java
  • [iOS 图像处理相关]

    //使用CGImage获取并修改图片像素

    #define
    Mask8(x) ( (x) & 0xFF ) #define R(x) ( Mask8(x) ) #define G(x) ( Mask8(x >> 8 ) ) #define B(x) ( Mask8(x >> 16) ) #define A(x) ( Mask8(x >> 24) ) #define RGBAMake(r, g, b, a) ( Mask8(r) | Mask8(g) << 8 | Mask8(b) << 16 | Mask8(a) << 24 ) - (UIImage *)processUsingPixels:(UIImage*)inputImage { // 1. Get the raw pixels of the image //定义最高32位整形指针 *inputPixels UInt32 * inputPixels; //转换图片为CGImageRef,获取参数:长宽高,每个像素的字节数(4),每个R的比特数 CGImageRef inputCGImage = [inputImage CGImage]; NSUInteger inputWidth = CGImageGetWidth(inputCGImage); NSUInteger inputHeight = CGImageGetHeight(inputCGImage); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSUInteger bytesPerPixel = 4; NSUInteger bitsPerComponent = 8; //每行字节数 NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth; //开辟内存区域,指向首像素地址 inputPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32)); //根据指针,前面的参数,创建像素层 CGContextRef context = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight, bitsPerComponent, inputBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); //根据目前像素在界面绘制图像 CGContextDrawImage(context, CGRectMake(0, 0, inputWidth, inputHeight), inputCGImage); //接来下就是重点了!!!像素处理-------------------------------------------------------- for (NSUInteger j = 0; j < inputHeight; j++) { for (NSUInteger i = 0; i < inputWidth; i++) { UInt32 * currentPixel = inputPixels + (j * inputWidth) + i; UInt32 color = *currentPixel; UInt32 br,thisR,thisG,thisB,thisA; //这里直接移位获得RBGA的值,以及输出写的非常好! thisR=R(color); thisG=G(color); thisB=B(color); thisA=A(color); //NSLog(@"%d,%d,%d,%d",thisR,thisG,thisB,thisA); br=B(color)-R(color); *currentPixel = RGBAMake(br, br, br, A(color)); } } //创建新图 // 4. Create a new UIImage CGImageRef newCGImage = CGBitmapContextCreateImage(context); UIImage * processedImage = [UIImage imageWithCGImage:newCGImage]; //释放 // 5. Cleanup! CGColorSpaceRelease(colorSpace); CGContextRelease(context); free(inputPixels); return processedImage; }
    //=======图像缩放====================================================================
    - (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
    {
        UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
        [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
        UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        
        NSLog(@"准备图像裁剪》》》》缩放后图片大小:width %f height %f ",scaledImage.size.width,scaledImage.size.height);
        
        UIGraphicsEndImageContext();
        return scaledImage;
    }
  • 相关阅读:
    Spring @EventListener 异步中使用condition的问题
    tomcat启动时报:IOException while loading persisted sessions: java.io.EOFException的解决方案 ZT
    Linux上更换默认的java版本
    Spring Security框架下Restful Token的验证方案
    Restful下的token认证方案
    Spring4.x Jpa + hibernate的配置(废弃JpaTemplate)
    FastCV安装报错---LaunchAnyWhere错误:载入Java VM时Windows出现错误:2
    Spring AOP 开发中遇到问题:Caused by: java.lang.IllegalArgumentException: warning no match for this type name: com.xxx.collector.service.impl.XxxServiceImpl [Xlint:invalidAbsoluteTypeName]
    支付宝APP支付开发- IOException : DER input, Integer tag error
    PowerDesigner新装后的设置
  • 原文地址:https://www.cnblogs.com/rayshen/p/4355403.html
Copyright © 2011-2022 走看看