[英]iPhone read image from socket
I am working in a little app for iphone base on ideas used to make an Android app. 我正在根据用于制作Android应用程序的想法为iPhone设计一个小应用程序。
To test, obviously i use the simulator, but the simulator don't have support for built-in camera. 为了测试,显然我使用了模拟器,但是模拟器不支持内置摄像头。 The Android idea to test this consist in use a WebCamBroadcaster Java app in the desktop to capture frames from built-in webcam and pass it through socket.
Android对此进行测试的想法是使用台式机中的WebCamBroadcaster Java应用程序从内置网络摄像头捕获帧并将其通过套接字传递。 Then in the app you just read the bytes and convert to image.
然后在应用程序中,您只需读取字节并转换为图像即可。
Well i was trying to do the same thing with iPhone Simulator. 好吧,我试图用iPhone Simulator做同样的事情。 Searching in the web a found a class to work with asynchronous sockets ( cocoaasyncsocket ).
在网上搜索时,发现了一个可用于异步套接字( cocoaasyncsocket )的类。 But i can't make it work.
但是我不能使它工作。
The Java App send the frames like this: Java App发送如下框架:
socket = ss.accept();
BufferedImage image = videoCapture.getNextImage();
if (image != null) {
OutputStream out = socket.getOutputStream();
if (RAW) {
image.getWritableTile(0, 0).getDataElements(0, 0, w$
image.releaseWritableTile(0, 0);
DataOutputStream dout = new DataOutputStream(new Bu$
out));
for (int i = 0; i < data.length; i++) {
dout.writeInt(data[i]);
}
dout.close();
} else {
ImageIO.write(image, "JPEG", out);
}
}
The Android version of this use C code to implement de socket reading proccess like this: 此版本的Android版本使用C代码实现de socket读取过程,如下所示:
long read_count, total_read = 0;
while (total_read < readBufSize)
{
read_count = read(sockd, &readBuf[total_read], readBufSize);
if (read_count <= 0 || errno != 0)
{
char buffer[100];
sprintf(buffer, "socket read errorno = %d", errno);
LOGV(buffer);
break;
}
total_read += read_count;
}
// If we read all of the data we expected, we will load the frame from the p$
if (total_read == readBufSize){
frame = loadPixels(readBuf, width, height);}
Where readBufsize = width*height*sizeof(int);
readBuf = (char*)malloc(readBufSize);
So i try to implement the same for iPhone but i have an error in the connection (errno = 2).. Then i find cocoaasyncsocket and i try to use but i have an unknown error and nothing is read: 所以我尝试为iPhone实现相同的功能,但是我在连接中出现错误(errno = 2)。然后我找到了cocoaasyncsocket并尝试使用,但我有一个未知错误,但未读取任何内容:
#import <Foundation/Foundation.h>
#import "AsyncSocket.h"
@interface Captura : NSObject {
NSString *ipserver;
UInt16 port;
NSError *errPtr;
AsyncSocket *socket;
NSMutableData *socketData;
}
@property (nonatomic,retain) NSString *ipserver;
@property (retain) AsyncSocket *socket;
@property (retain) NSError *errPtr;
//will contain de data read from socket
@property (retain) NSMutableData *socketData;
-(id)initWithIp:(NSString*)ip puerto:(UInt16)p;
-(BOOL)open;
-(void)close;
-(void)beginRead;
- (UIImage*)getImage;
@end
and the implementation 和实施
#import "Captura.h"
@implementation Captura
@synthesize ipserver;
@synthesize socket;
@synthesize errPtr;
@synthesize socketData;
-(id)initWithIp:(NSString*)ip puerto:(UInt16)p{
if (self = [super init]) {
ipserver = ip;
port = p;
socket = [[AsyncSocket alloc] initWithDelegate:self];
socketData = [[NSMutableData alloc] init];
}
return self;
}
//Connect
-(BOOL)open{
return [socket connectToHost:ipserver onPort:port error:&errPtr];
}
-(void)beginRead{
NSLog(@"Begin Read");
NSUInteger offset = [socketData length];
[socket readDataWithTimeout:1
tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"Conectado al servidor");
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
NSLog(@"Data leida %u",[data length]);
[socketData appendData:data];
[self beginRead];
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock{
[socketData release];
[ipserver release];
[socket release];
NSLog(@"MutableData length %u", [socketData length]);
NSLog(@"Socket Desconectado");
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err{
NSLog(@"Ocurrió un error desconectando.... %@",err);
}
- (UIImage*)getImage{
NSData *data;
[socketData getBytes:data length:320*480*sizeof(int)];
NSLog(@"Data obtenida %@",[data length]);
if ([socketData length]>320*480*sizeof(int)) {
[socketData replaceBytesInRange:NSMakeRange(0,320*480*sizeof(int)) withBytes:NULL length:0];
}
if (data!=nil && [data length]) {
UIImage *img = [[UIImage alloc] initWithData:data];
[data release];
return img;
}
[data release];
return nil;
}
@end
Well this code connect to the server and initialize the reading process and then close up.. socket is disconnect and the app is close. 这段代码很好地连接到服务器,并初始化读取过程,然后关闭。.套接字断开连接,应用程序关闭。
i can't test de getImage method yet... 我还不能测试de getImage方法...
Some idea? 有想法吗
Thanks in advance... 提前致谢...
我认为您需要在-onSocket:didConnectToHost:port中调用-beginRead:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.