【移動應用開發(fā)技術】iOS如何實現(xiàn)去除圖片背景顏色_第1頁
【移動應用開發(fā)技術】iOS如何實現(xiàn)去除圖片背景顏色_第2頁
【移動應用開發(fā)技術】iOS如何實現(xiàn)去除圖片背景顏色_第3頁
【移動應用開發(fā)技術】iOS如何實現(xiàn)去除圖片背景顏色_第4頁
【移動應用開發(fā)技術】iOS如何實現(xiàn)去除圖片背景顏色_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

付費下載

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發(fā)技術】iOS如何實現(xiàn)去除圖片背景顏色

/upload/information/20200623/126/120316.png-

(UIImage

*)removeColorWithMinHueAngle:(float)minHueAngle

maxHueAngle:(float)maxHueAngle

image:(UIImage

*)originalImage{

CIImage

*image

=

[CIImage

imageWithCGImage:originalImage.CGImage];

CIContext

*context

=

[CIContext

contextWithOptions:nil];//

kCIContextUseSoftwareRenderer

:

CPURender

/**

注意

*

UIImage

通過CIimage初始化,得到的并不是一個通過類似CGImage的標準UIImage

*

所以如果不用context進行渲染處理,是沒辦法正常顯示的

*/

CIImage

*renderBgImage

=

[self

outputImageWithOriginalCIImage:image

minHueAngle:minHueAngle

maxHueAngle:maxHueAngle];

CGImageRef

renderImg

=

[context

createCGImage:renderBgImage

fromRect:image.extent];

UIImage

*renderImage

=

[UIImage

imageWithCGImage:renderImg];

return

renderImage;

}

struct

CubeMap

{

int

length;

float

dimension;

float

*data;

};

-

(CIImage

*)outputImageWithOriginalCIImage:(CIImage

*)originalImage

minHueAngle:(float)minHueAngle

maxHueAngle:(float)maxHueAngle{

struct

CubeMap

map

=

createCubeMap(minHueAngle,

maxHueAngle);

const

unsigned

int

size

=

64;

//

Create

memory

with

the

cube

data

NSData

*data

=

[NSData

dataWithBytesNoCopy:map.data

length:map.length

freeWhenDone:YES];

CIFilter

*colorCube

=

[CIFilter

filterWithName:@"CIColorCube"];

[colorCube

setValue:@(size)

forKey:@"inputCubeDimension"];

//

Set

data

for

cube

[colorCube

setValue:data

forKey:@"inputCubeData"];

[colorCube

setValue:originalImage

forKey:kCIInputImageKey];

CIImage

*result

=

[colorCube

valueForKey:kCIOutputImageKey];

return

result;

}

struct

CubeMap

createCubeMap(float

minHueAngle,

float

maxHueAngle)

{

const

unsigned

int

size

=

64;

struct

CubeMap

map;

map.length

=

size

*

size

*

size

*

sizeof

(float)

*

4;

map.dimension

=

size;

float

*cubeData

=

(float

*)malloc

(map.length);

float

rgb[3],

hsv[3],

*c

=

cubeData;

for

(int

z

=

0;

z

<

size;

z++){

rgb[2]

=

((double)z)/(size-1);

//

Blue

value

for

(int

y

=

0;

y

<

size;

y++){

rgb[1]

=

((double)y)/(size-1);

//

Green

value

for

(int

x

=

0;

x

<

size;

x

++){

rgb[0]

=

((double)x)/(size-1);

//

Red

value

rgbToHSV(rgb,hsv);

//

Use

the

hue

value

to

determine

which

to

make

transparent

//

The

minimum

and

maximum

hue

angle

depends

on

//

the

color

you

want

to

remove

float

alpha

=

(hsv[0]

>

minHueAngle

&&

hsv[0]

<

maxHueAngle)

?

0.0f:

1.0f;

//

Calculate

premultiplied

alpha

values

for

the

cube

c[0]

=

rgb[0]

*

alpha;

c[1]

=

rgb[1]

*

alpha;

c[2]

=

rgb[2]

*

alpha;

c[3]

=

alpha;

c

+=

4;

//

advance

our

pointer

into

memory

for

the

next

color

value

}

}

}

map.data

=

cubeData;

return

map;

}void

rgbToHSV(float

*rgb,

float

*hsv)

{

float

min,

max,

delta;

float

r

=

rgb[0],

g

=

rgb[1],

b

=

rgb[2];

float

*h

=

hsv,

*s

=

hsv

+

1,

*v

=

hsv

+

2;

min

=

fmin(fmin(r,

g),

b

);

max

=

fmax(fmax(r,

g),

b

);

*v

=

max;

delta

=

max

-

min;

if(

max

!=

0

)

*s

=

delta

/

max;

else

{

*s

=

0;

*h

=

-1;

return;

}

if(

r

==

max

)

*h

=

(

g

-

b

)

/

delta;

else

if(

g

==

max

)

*h

=

2

+

(

b

-

r

)

/

delta;

else

*h

=

4

+

(

r

-

g

)

/

delta;

*h

*=

60;

if(

*h

<

0

)

*h

+=

360;

}/upload/information/20200623/126/120317.jpg[[SPImageChromaFilterManager

sharedManager]

removeColorWithMinHueAngle:50

maxHueAngle:170

image:[UIImage

imageWithContentsOfFile:[[NSBundle

mainBundle]

pathForResource:@"nb"

ofType:@"jpeg"]]]/upload/information/20200623/126/120318.pngBitmap/upload/information/20200623/126/120319.jpg32-bit

and

16-bit

pixel

formats

for

CMYK

and

RGB

color

spaces

in

Quartz

2D-

(UIImage

*)removeColorWithMaxR:(float)maxR

minR:(float)minR

maxG:(float)maxG

minG:(float)minG

maxB:(float)maxB

minB:(float)minB

image:(UIImage

*)image{

//

分配內(nèi)存

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);

//

創(chuàng)建context

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++)

{

uint8_t*

ptr

=

(uint8_t*)pCurPtr;

if

(ptr[3]

>=

minR

&&

ptr[3]

<=

maxR

&&

ptr[2]

>=

minG

&&

ptr[2]

<=

maxG

&&

ptr[1]

>=

minB

&&

ptr[1]

<=

maxB)

{

ptr[0]

=

0;

}else{

printf("\n>ptr0:%d

ptr1:%d

ptr2:%d

ptr3:%d<\n",ptr[0],ptr[1],ptr[2],ptr[3]);

}

}

//

將內(nèi)存轉(zhuǎn)成image

CGDataProviderRef

dataProvider

=CGDataProviderCreateWithData(NULL,

rgbImageBuf,

bytesPerRow

*

imageHeight,

nil);

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;

}-

(UIImage

*)removeWhiteColorWithImage:(UIImage

*)image{

return

[self

removeColorWithMaxR:255

minR:250

maxG:255

minG:240

maxB:255

minB:240

image:image];

}-

(UIImage

*)removeBlackColorWithImage:(UIImage

*)image{

return

[self

removeColorWithMaxR:15

minR:0

maxG:15

minG:0

maxB:15

minB:0

image:image];

}/upload/information/20200623/126/120322.png/upload/information/20200623/126/120325.png/upload/information/20200623/126/120328.png/upload/information/20200623/126/120329.jpg-

(UIImage

*)removeColorWithMaxR:(float)maxR

minR:(float)min

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論