IOS取图片的某个位置的像素(ARGB)

来源:互联网 发布:手机淘宝太费流量 编辑:程序博客网 时间:2024/05/20 22:41
//
//  cUIImageData.h
//  HelloWorld
//
//  Created by apple on 13-10-3.
//  Copyright (c) 2013年 apple. All rights reserved.
//

#import <Foundation/Foundation.h>
typedef struct {
    Byte A;
    Byte R;
    Byte G;
    Byte B;
}Color;

@interface cUIImageData : NSObject
-(Byte*)getImageData:(UIImage*)Image;
-(Color)GetImageColorAtPointX:(int)X
                       PointY:(int)Y
                    FromImage:(Byte*)ImageData
                  OfImageSize:(CGSize)ImageSize;
@end

////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  cUIImageData.m
//  HelloWorld
//
//  Created by apple on 13-10-3.
//  Copyright (c) 2013年 apple. All rights reserved.
//

#import "cUIImageData.h"

@implementation cUIImageData
//将UIimage中的数据放入Byte数组中顺序为ARGB.
-(Byte*)getImageData:(UIImage*)Image{
    CGImageRef imageRef= [Image CGImage];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   
    int width = CGImageGetWidth(imageRef);
    int height = CGImageGetHeight(imageRef);
    int bytesPerPixel = 4;
    int bytesPerRow=bytesPerPixel*width;
    int bitsPerComponent = 8;
   
    void* imageData ;//准备用来存储数据的数

    CGContextRef cgContexRef = CGBitmapContextCreate(NULL,
                                                     width,
                                                     height,
                                                     bitsPerComponent,
                                                     bytesPerRow,
                                                     colorSpace,
                                    kCGImageAlphaPremultipliedFirst);

    CGRect theRect = CGRectMake(0,0, width, height);
    CGContextDrawImage(cgContexRef, theRect, imageRef);
   
    //Byte* tempData=(Byte*)CGBitmapContextGetData(cgContexRef);
    CGColorSpaceRelease(colorSpace);

    imageData = new Byte[4*(width-1)+4*(height-1)*width+3];
    memcpy(imageData, (Byte*)CGBitmapContextGetData(cgContexRef), 4*(width-1)+4*(height-1)*width+3);
   //把data做一个深度拷贝,因为原来的数据在cgContexRef里边,要释放,要不会杯具
    CGContextRelease(cgContexRef);
   
   
    return (Byte*)imageData;
}

-(Color)GetImageColorAtPointX:(int)X
                       PointY:(int)Y
                       FromImage:(Byte*)ImageData
                     OfImageSize:(CGSize)ImageSize{
    int index =4*X+4*Y*ImageSize.width;//每个像素由四个分量组成所以要乘以4
    Color c;//不是指针不用主动分配内存=。=
    c.A=ImageData [index];
    c.R=ImageData[index+1];
    c.G=ImageData[index+2];
    c.B=ImageData[index+3];
    return c;
}

@end
///////////////////////////////////

函数原型:

CGContextRef CGBitmapContextCreate (

   void *data,
   size_t width,
   size_t height,
   size_t bitsPerComponent,
   size_t bytesPerRow,
   CGColorSpaceRef colorspace,
   CGBitmapInfo bitmapInfo

);

参数:

data                                    指向要渲染的绘制内存的地址。这个内存块的大小至少是(bytesPerRow*height)个字节,在ios4.0或者OSX10.6以后可以直接用NULL表示自动分配内存块大小。

width                                  bitmap的宽度,单位为像素

height                                bitmap的高度,单位为像素

bitsPerComponent        内存中像素的每个组件的位数.例如,对于32位像素格式和RGB 颜色空间,你应该将这个值设为8.

bytesPerRow                  bitmap的每一行在内存所占的比特数

colorspace                      bitmap上下文使用的颜色空间。

bitmapInfo                       指定bitmap是否包含alpha通道,像素中alpha通道的相对位置,像素组件是整形还是浮点型等信息的字符串。

描述:

当你调用这个函数的时候,Quartz创建一个位图绘制环境,也就是位图上下文。当你向上下文中绘制信息时,Quartz把你要绘制的信息作为位图 数据绘制到指定的内存块。一个新的位图上下文的像素格式由三个参数决定:每个组件的位数,颜色空间,alpha选项。alpha值决定了绘制像素的透明性。

///bitmapInfo详细

CS

BPC

alphaInfo

Resulting Bits and Description

NULL

8

kCGImageAlphaOnly

AAAAAAAA 8 bits per pixel alpha-only destination. Color data is thrown away. Useful for generating alpha-only bitmaps and masks. Available in Mac OS X 10.3 and later.

Gray

8

kCGImageAlphaNone

WWWWWWWW 8 bits per pixel grayscale channel.

RGB

5

kCGImageAlphaNoneSkipFirst

-RRRRRGGGGGBBBBB 16 bits per pixel, 5 bits per RGB component.

RGB

8

kCGImageAlphaNoneSkipFirst

--------RRRRRRRRRGGGGGGGGBBBBBBBB 32 bits per pixel, 8 bits per RGB component where first 8 bits are ignored.

RGB

8

kCGImageAlphaNoneSkipLast

RRRRRRRRRGGGGGGGGBBBBBBBB-------- 32 bits per pixel, 8 bits per RGB component where last 8 bits are ignored.

RGB

8

kCGImageAlphaPremultipliedFirst

AAAAAAAARRRRRRRRRGGGGGGGGBBBBBBBB 32 bits per pixel, 8 bits per ARGB component with premultiplied alpha.

RGB

8

kCGImageAlphaPremultipliedLast

RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA 32 bits per pixel, 8 bits per RGBA component with premultiplied alpha.


//////////////////用例
    cUIImageData* cud=[[cUIImageData alloc]init];
    Byte* data = [cud getImageData:img];
    Color colorAt00=[cud GetImageColorAtPointX:0
                                   PointY:0
                                FromImage:data OfImageSize:CGSizeMake(588, 588)];
    Byte a=data[0];
    Byte b=data[1];
    Byte cc=data[2];
    Byte d =data[3];
    if(a==255)
        cout<<a-colorAt00.A<<endl;
    else
        cout<<"No"<<endl;
   
    cout<<a<<endl
    <<b<<endl
    <<(int)cc<<endl
    <<(int)d<<endl;
0 0
原创粉丝点击