ANE, iOS and BitmapData

假如想象 提交于 2019-12-11 00:58:41

问题


Is there any example of passing bitmap data into iOS and returning a modfified copy. I am trying to convert a bitmap into jpeg - similar to this example in Windows http://blog.magicalhobo.com/2011/09/12/air-3-native-extension-imagepro cessor/ but it crashed every time.

I am pretty sure I am doing something basic wrong but not been an iOS / Objective C programmer I am really just trying magic incantations and I can't find any good examples. Any help appreciated?

Sean

P.S. Below is my attempt:

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;

}


回答1:


Finale version - seems to work ok - encodes ok and I think cleans up after itself ok.

Sean

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;

}




回答2:


This kinda works except reds are blues in jpeg and not sure I am releasing everything ok.

Thats tomorrows battle.

Sean

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:


Exchange the position of Red and Blue Pixels

the format you get from iOS is : 0xAARRGGBB
and the one expected to show on an AIR app is : 0xAABBGGRR



来源:https://stackoverflow.com/questions/8524388/ane-ios-and-bitmapdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!