我正在尝试从Java客户端向iOS应用程序发送图像。我需要尽快发送图像以获得高fps。我尝试使用套接字,但速度太慢,所以现在我从客户端上传到服务器并使用应用程序下载它。这适用于使用Java接收器,但不适用于应用程序。在应用程序上查看时,图像刚出现黑色,因为图像一次又一次地保存。在java中,图像刷新,你可以看到帧,但在iOS中它只是总是黑色。无论如何我可以解决这个问题吗?或者我是否需要找到更好的方式来传输图像?谢谢!
我的上传代码:
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
}
public void uploadToServer() {
String server = "ip";
int port = 21;
String user = "user";
String pass = "pass";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ByteArrayOutputStream os = new ByteArrayOutputStream();
int w = 765;
int h = 503;
double d = .75;
ImageIO.write(
resize(client.methods.getClientImage(), (int) (w * d),
(int) (h * d)), "JPG", os);
InputStream inputStream = new ByteArrayInputStream(os.toByteArray());
String secondRemoteFile = "public_html/iRS/client_img.jpg";
// System.out.println("Start uploading file");
OutputStream outputStream = ftpClient
.storeFileStream(secondRemoteFile);
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
}
inputStream.close();
outputStream.close();
ftpClient.completePendingCommand();
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
Tools.log("Disconnected");
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
下载代码:
- (void) refreshImage {
client.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"url"]]]; ;
}
答案 0 :(得分:0)
如果你只需要几毫秒就不要存储在文件中。 将文件下载到NSData并将其传递给UIImage。 这是我的HTTP客户端类的一些示例代码。 以下方法启动下载任务:
- (NSURLSessionTask *) startGetTaskForURL: (NSURL *) url {
NSLog(@"startGetTaskForUrl: %@", url);
NSURLSessionTask *task = [self.session dataTaskWithURL:url];
NSMutableData *data = [NSMutableData data];
[task resume];
[self.runningTasks setObject:data forKey:task];
return task;
}
每次有可用数据时,都会调用以下委托方法。这里的数据是在创建任务时创建的NSMutableData中收集的:
- (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTTPClient: Data task received data: %@", dataString);
NSMutableData *runningData = [self dataForTask:dataTask];
if (!runningData) {
NSLog(@"No data found for task");
return;
}
[runningData appendData: data];
}
下载任务完成后,将调用以下委托方法:
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
NSLog(@"HTTPClient: Task completed with error: %@", error);
[self.delegate sessionTask:task completedWithError:error data:[self dataForTask:task]];
}