Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何通过TCP将IplImage从服务器发送到iPod客户端UIImage_Ios_Sockets_Uiimage_Istream_Iplimage - Fatal编程技术网

Ios 如何通过TCP将IplImage从服务器发送到iPod客户端UIImage

Ios 如何通过TCP将IplImage从服务器发送到iPod客户端UIImage,ios,sockets,uiimage,istream,iplimage,Ios,Sockets,Uiimage,Istream,Iplimage,我有一个linux服务器,使用,我创建了一个与iPod客户端的TCP连接。我有一个IplImage*img从服务器发送到iPod。我使用写入(socket,/*DATA*/,43200)命令,我试图发送的数据是:reinterpret\u cast(img),img和img->imageData。所有这些选择实际上发送任何类型的数据 在iPod端,我以这种方式接收数据(正如我在SO中看到的,不要介意复杂的东西,它只是用于接收来自单个图像的所有数据。): 在收到整个图像后,我有以下内容: [buf

我有一个linux服务器,使用,我创建了一个与iPod客户端的TCP连接。我有一个
IplImage*img从服务器发送到iPod。我使用
写入(socket,/*DATA*/,43200)命令,我试图发送的数据是:
reinterpret\u cast(img)
img
img->imageData
。所有这些选择实际上发送任何类型的数据

在iPod端,我以这种方式接收数据(正如我在SO中看到的,不要介意复杂的东西,它只是用于接收来自单个图像的所有数据。):

在收到整个图像后,我有以下内容:

[buffer setLength: 43200];
NSData *imagem = [NSData dataWithBytes:buffer length:43200];
UIImage *final= [self UIImageFromIplImage:imagem];
现在。。我知道我可以让openCV在iPod上工作,但我找不到一个简单的解释来说明如何让它工作,所以我使用并调整了它,因为我知道我图像的所有规格(例如,我设置了
CGImageCreate()
函数中的所有变量):

}

问题:当我显示图像时,我得到的图像完全是奇怪的和“随机的”,即使发送的图像总是相同的。我真的不知道怎么了

PS:TCP连接可以很好地处理其他数据,如数字或文字。图像是灰度的


谢谢你的帮助。

我让它这样工作。 在服务器端(使用openframeworks(&ofxOpenCv)的linux中的代码::blocks):

在客户端(iPod 4.3):

也许有一个更简单的方法,但是,嘿,把工作做完。希望这对任何人都有帮助

[buffer setLength: 43200];
NSData *imagem = [NSData dataWithBytes:buffer length:43200];
UIImage *final= [self UIImageFromIplImage:imagem];
- (UIImage *)UIImageFromIplImage:(NSData *)image {

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

// Allocating the buffer for CGImage
NSData *data = [NSData dataWithBytes:image length:43200];

CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);

// Creating CGImage from chunk of IplImage    
size_t width = 240;
size_t height = 180;
size_t depth = 8;             //bitsPerComponent
size_t depthXnChannels = 8;   //bitsPerPixel
size_t widthStep = 240;       //bytesPerRow

CGImageRef imageRef = CGImageCreate(width, height, depth, depthXnChannels, widthStep, colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault,provider, NULL, false, kCGRenderingIntentDefault);

// Getting UIImage from CGImage
UIImage *ret = [UIImage imageWithCGImage:imageRef];
lolView.image = ret;
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return ret;
img.allocate(240, 180, OF_IMAGE_COLOR);                    //ofImage
img2.allocate(240, 180);                                   //ofxCvColorImage
frame = cvCreateImage(cvSize(240,180), IPL_DEPTH_8U, 3);   //IplImage
bw = cvCreateImage(cvSize(240,180), IPL_DEPTH_8U, 1);      //IplImage
gray.allocate(240, 180);                                   //ofxCvGrayscaleImage


///ofImage
img.loadImage("lol.jpg");

///ofImage -> ofxCvColor
img2.setFromPixels(img.getPixels(), 240, 180);

///ofxCvColor -> IplImage
frame = img2.getCvImage();

///IplImage in GRAY
cvCvtColor(frame,bw,CV_RGB2GRAY);
cvThreshold(bw,bw,200,255,CV_THRESH_BINARY);  //It is actually a binary image
gray = bw;
pix = gray.getPixels();

n=write(newsockfd,pix,43200);
-(UIImage *) dataFromIplImageToUIImage:(unsigned char *) rawData;
{
size_t width = 240;
size_t height = 180;
size_t depth = 8;                   //bitsPerComponent
size_t depthXnChannels = 8;         //bitsPerPixel
size_t widthStep = 240;             //bytesPerRow

CGContextRef ctx = CGBitmapContextCreate(rawData, width, height, depth, widthStep,  CGColorSpaceCreateDeviceGray(), kCGImageAlphaNone);

CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];  

CGContextRelease(ctx);  

myImageView.image = rawImage;  
return rawImage;

free(rawData);
}