2011-12-15 64 views
2

有位圖數據傳遞到iOS和返回modfified副本的任何實例。我想一個位圖轉換成JPEG - 類似於此示例在Windows http://blog.magicalhobo.com/2011/09/12/air-3-native-extension-imagepro cessor /但每一次崩潰。ANE,iOS版和BitmapData

我很確定我正在做一些基本的錯誤,但不是一個iOS/Objective C程序員我真的只是嘗試魔術咒語,我找不到任何好的例子。任何幫助表示讚賞?

肖恩

P.S.下面是我的嘗試:

FREObject encodeJPEG(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) 
{ 
    FREObject  objectBitmapData = argv[0]; 
    FREBitmapData bitmapData; 

    FREAcquireBitmapData(objectBitmapData, &bitmapData); 

    int width  = bitmapData.width; 
    int height  = bitmapData.height; 
    int stride  = bitmapData.lineStride32 * 4; 
    uint32_t* input = bitmapData.bits32; 

    FREReleaseBitmapData(objectBitmapData); 


    UIImage *myImage = [UIImage imageWithData:input]; 
    NSData *jpgData = UIImageJPEGRepresentation(myImage, 0.9);  

    FREObject objectByteArray = argv[1]; 
    FREByteArray byteArray; 

    FREObject length; 

    FRENewObjectFromUint32(jpgData.length, &length); 

    FRESetObjectProperty(objectByteArray, (const uint8_t*) "length", length, NULL); 

    FREAcquireByteArray(objectByteArray, &byteArray); 

    memcpy(byteArray.bytes, jpgData.bytes, jpgData.length); 
    FREReleaseByteArray(objectByteArray); 

    return NULL; 

}

+0

作爲一種替代方法,您可以在Ac內部將位圖轉換爲JPEG格式tionScript,使用https://github.com/mikechambers/as3corelib – 2011-12-15 19:44:27

+0

其彎曲移動,並在動作太慢 - 覺得我有這樣的破解會後不久,一次測試。 – user1100434 2011-12-16 00:54:05

回答

0

這有點兒工作,除了紅色爲jpeg藍色和不知道我釋放一切就OK了。

即將明天的戰鬥。

肖恩

FREObject encodeJPEG(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) 
{ 
FREObject  objectBitmapData = argv[ 0 ]; 
FREBitmapData2 bitmapData; 

FREAcquireBitmapData2(objectBitmapData, &bitmapData); 

int width  = bitmapData.width; 
int height  = bitmapData.height; 
int stride  = bitmapData.lineStride32 * 4; 
uint32_t* input = bitmapData.bits32; 

// make data provider from buffer 
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bitmapData.bits32, (width * height * 4), NULL); 

// set up for CGImage creation 
int      bitsPerComponent = 8; 
int      bitsPerPixel  = 32; 
int      bytesPerRow   = 4 * width; 
CGColorSpaceRef   colorSpaceRef  = CGColorSpaceCreateDeviceRGB(); 
CGBitmapInfo   bitmapInfo   = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst; 
CGColorRenderingIntent renderingIntent  = kCGRenderingIntentDefault; 
CGImageRef    imageRef   = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); 

// make UIImage from CGImage 
UIImage *myImage = [UIImage imageWithCGImage:imageRef];  
NSData* jpgData = UIImageJPEGRepresentation(myImage, 0.9);  

FREReleaseBitmapData(objectBitmapData); 

FREObject objectByteArray = argv[ 1 ];    
FREByteArray byteArray; 
FREObject length; 

FRENewObjectFromUint32(jpgData.length, &length); 
FRESetObjectProperty(objectByteArray, (const uint8_t*) "length", length, NULL); 
FREAcquireByteArray(objectByteArray, &byteArray); 

memcpy(byteArray.bytes, jpgData.bytes, jpgData.length); 

FREReleaseByteArray(objectByteArray); 

return NULL; 
} 
3

結局版本 - 似乎確定工作 - 編碼確定,我想以後自己確定清理。

肖恩

FREObject encodeJPEG(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) 
{ 
FREObject  objectBitmapData = argv[ 0 ]; 
FREBitmapData2 bitmapData; 

FREAcquireBitmapData2(objectBitmapData, &bitmapData); 

int width  = bitmapData.width; 
int height  = bitmapData.height; 
int stride  = bitmapData.lineStride32 * 4; 
uint32_t* input = bitmapData.bits32; 

// make data provider from buffer 
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bitmapData.bits32, (width * height * 4), NULL); 

// set up for CGImage creation 
int      bitsPerComponent = 8; 
int      bitsPerPixel  = 32; 
int      bytesPerRow   = 4 * width; 
CGColorSpaceRef   colorSpaceRef  = CGColorSpaceCreateDeviceRGB();  
CGBitmapInfo   bitmapInfo; 

if(bitmapData.hasAlpha) 
{ 
    if(bitmapData.isPremultiplied) 
     bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst; 
    else 
     bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaFirst;    
} 
else 
{ 
    bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst; 
} 

CGColorRenderingIntent renderingIntent  = kCGRenderingIntentDefault; 
CGImageRef    imageRef   = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); 

// make UIImage from CGImage 
UIImage *myImage = [UIImage imageWithCGImage:imageRef];  
NSData* jpgData = UIImageJPEGRepresentation(myImage, 0.9);  

FREReleaseBitmapData(objectBitmapData); 

FREObject objectByteArray = argv[ 1 ];    
FREByteArray byteArray; 
FREObject length; 

FRENewObjectFromUint32(jpgData.length, &length); 
FRESetObjectProperty(objectByteArray, (const uint8_t*) "length", length, NULL); 
FREAcquireByteArray(objectByteArray, &byteArray); 

memcpy(byteArray.bytes, jpgData.bytes, jpgData.length); 

FREReleaseByteArray(objectByteArray); 

// release bits 
CGColorSpaceRelease(colorSpaceRef); 
CGImageRelease(imageRef); 
CGDataProviderRelease(provider);  
return NULL; 

}

0

交易所的紅色和藍色像素

位置你的iOS獲得的格式是:0xAARRGGBB
和一個預計將顯示在AIR應用程序是:0xAABBGGRR