1

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()

;

8
  • I'd recommend using some kind of jpeg library. Doing it yourself can be a bit of work. But unless you specify some sort of programming language you want to do that with, I don't think I can be more concrete. Assuming you are looking for programming solution. Commented Oct 11, 2012 at 20:56
  • I am working in java. I have menage to write a code that helps me receive the banary jpeg data and I menage so save that binary data in a file, I just don't know how to convert that binary data into an image, using java. Commented Oct 11, 2012 at 21:02
  • You should then add the java tag. Btw those bytes are technically a picture/image in a raw form (compressed with jpeg). Sort of. What exactly do you want to do it them? Save them to a file? Commented Oct 11, 2012 at 21:07
  • I don't need to save it like raw, we need to create the picture and to save the picture like jpeg. It is not necessary to have the raw data, I just need to get and save the picture, but I don't know how to get the picture Commented Oct 11, 2012 at 21:14
  • Just save it as you would normally save data into a file. If it is compressed as jpeg you would have jpeg file. Commented Oct 11, 2012 at 21:16

2 Answers 2

3

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()
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot. But I am quite new in java, so it's a first time I am working with images, so I would appreciate if you could help me a little bit more, with an example maybe, how to fix this, and how not to use reader but to use inputstream directly and to save the picture like jpeg. Thanks in forward.
MY question exactly is how to save all of it and not use reader?
Thank you very very very much!!!! I'll definitely try this I'll tell you if it's working properly. Thanks again for all of your effort. Greeting
consider accepting the answer if it does what you expect it to do
1

You can use javax.imageio.ImageIO

URL url = new URL("http://10.10.1.154" + GETIMAGESCR());
BufferedImage bi = ImageIO.read(url.openStream())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.