Iphone上开发ARToolkit应用的注意事项总结

原创文章,欢迎转载,转载时务必注明原文地址及作者

1. 如何调整uiimage的大小
//改变图片到指定的尺寸
-(UIImage*)resizedImage:(UIImage*)inImage  inRect:(CGRect)thumbRect {
	
	// Creates a bitmap-based graphics context and makes it the current context.
	UIGraphicsBeginImageContext(thumbRect.size);
	[inImage drawInRect:thumbRect];
	return UIGraphicsGetImageFromCurrentImageContext();
}

2. 如何取得uiimage图像中的RGB数据
//uiimage编码成ARGB
- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {
	
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;
	
    // Get image width, height. We'll use the entire image.
    size_t pixelsWide = CGImageGetWidth(inImage);
    size_t pixelsHigh = CGImageGetHeight(inImage);
	//int pixelsWide, pixelsHigh;
	//ar2VideoGetSize(gVid, &pixelsWide, &pixelsHigh);
	
    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    bitmapBytesPerRow   = (pixelsWide * 4);
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);
	
    // Use the generic RGB color space.
    colorSpace = CGColorSpaceCreateDeviceRGB();
	//colorSpace = CGImageGetColorSpace(inImage);
	
    if (colorSpace == NULL)
    {
        fprintf(stderr, "Error allocating color space\n");
        return NULL;
    }
	
    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }
	
    // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
    // per component. Regardless of what the source image format is
    // (CMYK, Grayscale, and so on) it will be converted over to the format
    // specified here by CGBitmapContextCreate.
    context = CGBitmapContextCreate (bitmapData,
									 pixelsWide,
									 pixelsHigh,
									 8,      // bits per component
									 bitmapBytesPerRow,
									 colorSpace,
									 kCGImageAlphaPremultipliedFirst);// kCGImageAlphaNone
    if (context == NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }
	
    // Make sure and release colorspace before returning
    CGColorSpaceRelease( colorSpace );
	
    return context;
}

3. 如何识别多个marker文件
//配置ar识别marker参数
static int setupMarker(const char *patt_name, int *patt_id, ARHandle *arhandle, ARPattHandle **pattHandle_p)
{	
	//此处修改支持识别多个marker文件
	if (*pattHandle_p == NULL) {
		*pattHandle_p = arPattCreateHandle();
		if (*pattHandle_p == NULL) {
			fprintf(stderr, "setupMarker(): Error: arPattCreateHandle.\n");
			return (FALSE);
		}
		arPattAttach(arhandle, *pattHandle_p);
	}
    
	if ((*patt_id = arPattLoad(*pattHandle_p, patt_name)) < 0) {
		fprintf(stderr, "setupMarker(): Error loading pattern file %s.\n", patt_name);
		arPattDeleteHandle(*pattHandle_p);
		return (FALSE);
	}
	
	return (TRUE);
}

4. 如何取得识别出的marker的外框坐标点
gARHandle->markerInfo[0]. vertex

5. 如何绘画识别出的marker的外框以及对于的图片背景
//画出marker的外框
- (void)drawMarkerRect {
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);//颜色
	CGContextSetLineWidth(context, 2.0);//线宽
	//画线
	CGContextMoveToPoint(context,x1, y1);
	CGContextAddLineToPoint(context, x2, y2);
	CGContextAddLineToPoint(context, x3, y3);
	CGContextAddLineToPoint(context, x4, y4);
	CGContextAddLineToPoint(context, x1, y1);
	CGContextStrokePath(context);
}	
//描绘识别marker外框的模式
	self.backgroundColor = [UIColor colorWithPatternImage:mimage];

6. 如何利用NSTimer来实现异步调用
//延迟加载视频,让画出marker的画面显示一下
			mTimer = [NSTimer scheduledTimerWithTimeInterval:INTERVAL_SEC target:self selector:@selector(playMovie:) userInfo:nil repeats:NO];
//播放视频
- (void)playMovie:(NSTimer *)timer {
	[viewController playMovie:currentTag.movie];
	[mTimer release];
	mTimer = nil;
}

7. 如何保存当前图片到图片文件夹
//保持图片到照片文件夹
-(void)saveToPhotosAlbum:(UIImage *)image {
	CGContextRef context = UIGraphicsGetCurrentContext();
	UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), context);
	UIGraphicsEndImageContext();
}

//保存图片时的回调
-(void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
	if (!error) {
		NSLog(@"image Has Saved");
	} else {
		NSLog(@"image Saving failed");
	}
}

8. 如何链接artoolkit的c实现库(artoolkit4)
设定【Search Paths】下的【Header Search Paths】,值为artoolkit库的头文件目录相对路径,设定【Search Paths】下的【Library Search Paths】,值为artoolkit库的链接文件目录相对路径

你可能感兴趣的:(C++,c,C#)