簡體   English   中英

iPhone從插座讀取圖像

[英]iPhone read image from socket

我正在根據用於制作Android應用程序的想法為iPhone設計一個小應用程序。
為了測試,顯然我使用了模擬器,但是模擬器不支持內置攝像頭。 Android對此進行測試的想法是使用台式機中的WebCamBroadcaster Java應用程序從內置網絡攝像頭捕獲幀並將其通過套接字傳遞。 然后在應用程序中,您只需讀取字節並轉換為圖像即可。

好吧,我試圖用iPhone Simulator做同樣的事情。 在網上搜索時,發現了一個可用於異步套接字( cocoaasyncsocket )的類。 但是我不能使它工作。

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

此版本的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);

所以我嘗試為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

和實施

#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

這段代碼很好地連接到服務器,並初始化讀取過程,然后關閉。.套接字斷開連接,應用程序關閉。

我還不能測試de getImage方法...

有想法嗎

提前致謝...

我認為您需要在-onSocket:didConnectToHost:port中調用-beginRead:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM