ANE、iOS 和 BitmapData

发布于 2024-12-21 19:03:11 字数 1361 浏览 2 评论 0原文

是否有将位图数据传递到 iOS 并返回修改后的副本的示例。我正在尝试将位图转换为 jpeg - 类似于 Windows 中的此示例 http://blog.magicalhobo.com/2011/09/12/air-3-native-extension-imageprocessor/ 但每次都会崩溃。

我很确定我在做一些基本的错误,但不是一个 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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

焚却相思 2024-12-28 19:03:11

最终版本 - 似乎工作正常 - 编码正常,我认为在其自身之后清理正常。

肖恩

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;

}

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;

}

听风念你 2024-12-28 19:03:11

这有点有效,除了 jpeg 中的红色是蓝色,并且不确定我是否发布了一切正常。

这就是明天的战斗。

肖恩

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

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;
}
紫罗兰の梦幻 2024-12-28 19:03:11

交换红色和蓝色像素的位置,

您从 iOS 获得的格式是:0xAARRGGBB
预计在 AIR 应用程序上显示的是:0xAABBGGRR

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文