Reputation: 21
I have a problem and I hope you can help me. The problem is the following. I have a camera that has an http service, and I am communicating with the camera using the http. So I send http request and I receive an http response in which I have a binary jpeg data. But I do not know how to convert that data into picture. So my question, and my main problem is how can I convert that binary data into picture.
This is my code so far, I am stuck in getting the image.
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()
;
Upvotes: 1
Views: 8884
Reputation: 12296
Try this code:
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()
Upvotes: 3
Reputation: 891
You can use javax.imageio.ImageIO
URL url = new URL("http://10.10.1.154" + GETIMAGESCR());
BufferedImage bi = ImageIO.read(url.openStream())
Upvotes: 1