java - 将二进制数据转换为图像

标签 java image http binary

我有一个问题,我希望你能帮助我。问题如下。我有一个有 http 服务的相机,我正在使用 http 与相机通信。所以我发送 http 请求并收到一个 http 响应,其中我有一个二进制 jpeg 数据。但我不知道如何将这些数据转换成图片。所以我的问题,我的主要问题是如何将二进制数据转换为图片。

到目前为止,这是我的代码,我一直在获取图像。

URL url = new URL("http://10.10.1.154" + GETIMAGESCR());
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
// while ((inputLine = in.readLine()) != null){
// inputLine = in.readLine();
File file = new File("D:\\alphas\\proba.bin");
boolean postoi = file.createNewFile();
FileWriter fstream = new FileWriter("D:\\alphas\\proba.bin");
BufferedWriter out = new BufferedWriter(fstream);
while ((inputLine = in.readLine()) != null){
out.write(in.readLine());
// out.close();
// System.out.println("File created successfully.");
System.out.println(inputLine);
}
System.out.println("File created successfully.");
out.close();
in.close()

;

最佳答案

试试这段代码:

URL url = new URL("http://10.10.1.154" + GETIMAGESCR());
InputStream is = new InputStream(url.openStream());
FileOutputStream out = new FileOutputStream("D:\\alphas\\proba.jpg");
byte[] data = new byte[1024];
int readBytes = 0;
while ((readBytes = is.read(data)) > 0) {
  out.write(data,0,readBytes);
}
out.flush();
out.close();
is.close()

关于java - 将二进制数据转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12848342/

相关文章:

android - Contact Image - Android 中分辨率较低

python - 如何在 python 中使用 urllib2 和 urllib 制作一个自动登录到我的 wordpress 博客的脚本?

python - Python请求中 `data`和 `files`的区别

java - 以编程方式更改 Android 5 (L) 上的 WiFi 配置

java - 如何从绑定(bind)的 JTable 中删除行?

php - 向客户端发送响应后如何执行耗时的任务

Java:沿圆形路径移动标签

javascript - 如何在查询字符串中将 JavaScript 数组编码为 PHP 兼容的 "array arguments"?

java - Hazelcast:我可以为每个 JVM 配置一个映射吗?

java on osx - xmx 被忽略?