zoukankan      html  css  js  c++  java
  • iOS 截屏,openGL ES 截图,以及像素颜色判断

    代码整理了2种截图,类似。(没苹果自带那种截图彻底)

    方法一:

    +(UIImage *)fullScreenshots{
        
        UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
        //    UIGraphicsBeginImageContext(screenWindow.frame.size);//全屏截图,包括window
        UIGraphicsBeginImageContextWithOptions(screenWindow.frame.size,YES,0.0);
        [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return viewImage;
        
    }
    

      方法二:

    + (UIImage*)screenShot
    {
        // Create a graphics context with the target size
        // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
        // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
        CGSize imageSize = [[UIScreen mainScreen] bounds].size;
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        // Iterate over every window from back to front
        for (UIWindow *window in [[UIApplication sharedApplication] windows])
        {
            if ( [window screen] == [UIScreen mainScreen])
            {
                // -renderInContext: renders in the coordinate space of the layer,
                // so we must first apply the layer's geometry to the graphics context
                CGContextSaveGState(context);
                // Center the context around the window's anchor point
                CGContextTranslateCTM(context, [window center].x, [window center].y);
                // Apply the window's transform about the anchor point
                CGContextConcatCTM(context, [window transform]);
                // Offset by the portion of the bounds left of and above the anchor point
                CGContextTranslateCTM(context,
                                      -[window bounds].size.width * [[window layer] anchorPoint].x,
                                      -[window bounds].size.height * [[window layer] anchorPoint].y);
                
                // Render the layer hierarchy to the current context
                [[window layer] renderInContext:context];
                
                // Restore the context
                CGContextRestoreGState(context);
            }
        }
        
        // Retrieve the screenshot image
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        return image;
    } 

    openGL ES 截图

    - (UIImage*) takePicture {
        int s = 1;
        UIScreen* screen = [UIScreen mainScreen];
        if ([screen respondsToSelector:@selector(scale)]) {
            s = (int) [screen scale];
        }
        
        GLint viewport[4];
        glGetIntegerv(GL_VIEWPORT, viewport);
        
        
        int width = viewport[2];
        int height = viewport[3];
        
        int myDataLength = width * height * 4;
        GLubyte *buffer = (GLubyte *) malloc(myDataLength);
        GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
        glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
        for(int y1 = 0; y1 < height; y1++) {
            for(int x1 = 0; x1 <width * 4; x1++) {
                buffer2[(height - 1 - y1) * width * 4 + x1] = buffer[y1 * 4 * width + x1];
            }
        }
        free(buffer);
        
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
        int bitsPerComponent = 8;
        int bitsPerPixel = 32;
        int bytesPerRow = 4 * width;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
        CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
        CGColorSpaceRelease(colorSpaceRef);
        CGDataProviderRelease(provider);
        UIImage *image = [ UIImage imageWithCGImage:imageRef scale:s orientation:UIImageOrientationUp ];
        return image;
    }
    

      

    以及颜色判断:

    + (float)colorAtPixel:(CGPoint)point  image:(UIImage *)image
    {
        // Cancel if point is outside image coordinates
        if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), point)) {
            return 1000.0;
        }
        NSInteger pointX = trunc(point.x);
        NSInteger pointY = trunc(point.y);
        CGImageRef cgImage = image.CGImage;
        NSUInteger width = image.size.width;
        NSUInteger height = image.size.height;
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        int bytesPerPixel = 4;
        int bytesPerRow = bytesPerPixel * 1;
        NSUInteger bitsPerComponent = 8;
        unsigned char pixelData[4] = { 0, 0, 0, 0 };
        CGContextRef context = CGBitmapContextCreate(pixelData,1,1,
                                                     bitsPerComponent,
                                                     bytesPerRow,
                                                     colorSpace,
                                                     kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        CGColorSpaceRelease(colorSpace);
        CGContextSetBlendMode(context, kCGBlendModeCopy);
        // Draw the pixel we are interested in onto the bitmap context
        CGContextTranslateCTM(context, -pointX, pointY - (CGFloat)height);
        CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
        CGContextRelease(context);
        // Convert color values [0..255] to floats [0.0..1.0]
        CGFloat red = (CGFloat)pixelData[0] / 1.0f;
        CGFloat green = (CGFloat)pixelData[1] / 1.0;
        CGFloat blue = (CGFloat)pixelData[2] / 1.0f;
        //    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
        //    NSLog(@" colors: RGB  %f %f %f  %f", red, green, blue, alpha);
        return red + green + blue;
    }
    

      

  • 相关阅读:
    深入理解 Netty编码流程及WriteAndFlush()的实现
    深入理解 Netty-解码器架构与常用解码器
    暑假集训Day 5 P3963 [TJOI2013] 奖学金
    暑假集训日记Day xx
    P3226 [HNOI2012]集合选数 状压dp(思维题)
    线段树(毒瘤)总结
    P3622 [APIO2007]动物园
    暑假集训Day 4 P4163 [SCOI2007]排列 (状压dp)
    暑假集训Day2 互不侵犯(状压dp)
    暑假集训Day2 状压dp 特殊方格棋盘
  • 原文地址:https://www.cnblogs.com/qiyer/p/6439253.html
Copyright © 2011-2022 走看看