Reggie_BE
asked on
Re-Request image from webserver (java.net.URL)
Hi,
I wrote something that reads an image from a website.
For example http://www.myblog.com/mywebcam gives me an image of my webcam. Now that image changes every second.
How can I re-request the image ? Do I have to disconnect and connect again ? Or can I just resend the headers ?
Cause I can't find any info about how to do that using the Java.net.URL class :\ It's HTTP/1.1
I wrote something that reads an image from a website.
For example http://www.myblog.com/mywebcam gives me an image of my webcam. Now that image changes every second.
How can I re-request the image ? Do I have to disconnect and connect again ? Or can I just resend the headers ?
Cause I can't find any info about how to do that using the Java.net.URL class :\ It's HTTP/1.1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ImageIO.write(img, "JPG", outputStream);
ASKER
That only accepts File or BufferedImage.
This is the way I have it now:
while(connected) {
saveImage(getImage(uc), "test" + i++ + ".jpg");
this.sleep(1000);
}
public void saveImage(Image img, String filename) {
try {
ImageIO.write(img, "jpg", new File(filename)); // Can't find symbol method write(javax.awt.Image, String, File)
} catch (Exception e) {
System.out.println(e);
}
}
private void saveBytesToFile(byte[] buff, String filename) {
try {
FileOutputStream fos = new FileOutputStream(new File(filename));
fos.write(buff);
} catch (Exception e) {
System.out.println(e);
}
}
This is how I used to do it, but when I repeat that, he will write the first image, but after that i get "null" as errormessage
while (connected) {
int bufferSize = uc.getContentLength();
DataInputStream dis = new DataInputStream(uc.getInpu tStream()) ;
byte[] buf = new byte[bufferSize];
dis.readFully(buf);
saveBytesToFile(buf, "test_" + i++ + ".jpg");
}
private void saveBytesToFile(byte[] buff, String filename) {
try {
FileOutputStream fos = new FileOutputStream(new File(filename));
fos.write(buff);
} catch (Exception e) {
System.out.println(e);
}
}
This is the way I have it now:
while(connected) {
saveImage(getImage(uc), "test" + i++ + ".jpg");
this.sleep(1000);
}
public void saveImage(Image img, String filename) {
try {
ImageIO.write(img, "jpg", new File(filename)); // Can't find symbol method write(javax.awt.Image, String, File)
} catch (Exception e) {
System.out.println(e);
}
}
private void saveBytesToFile(byte[] buff, String filename) {
try {
FileOutputStream fos = new FileOutputStream(new File(filename));
fos.write(buff);
} catch (Exception e) {
System.out.println(e);
}
}
This is how I used to do it, but when I repeat that, he will write the first image, but after that i get "null" as errormessage
while (connected) {
int bufferSize = uc.getContentLength();
DataInputStream dis = new DataInputStream(uc.getInpu
byte[] buf = new byte[bufferSize];
dis.readFully(buf);
saveBytesToFile(buf, "test_" + i++ + ".jpg");
}
private void saveBytesToFile(byte[] buff, String filename) {
try {
FileOutputStream fos = new FileOutputStream(new File(filename));
fos.write(buff);
} catch (Exception e) {
System.out.println(e);
}
}
>>Now I'm trying to save an Image object to a jpeg file .. can find how to save BufferedImage, but that's not what I need :(
Why has it changed and where does it come in to your original?
Why has it changed and where does it come in to your original?
ASKER
Why has what changed ?
The problem is when I use uc.getInputStream() I can only call it once. The 2nd time it gives me null. So I figured out that I had to use uc.getContent(), but then I can't save the file properly :s
The problem is when I use uc.getInputStream() I can only call it once. The 2nd time it gives me null. So I figured out that I had to use uc.getContent(), but then I can't save the file properly :s
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>The 2nd time it gives me null.
Please post the code you're using to do this
Please post the code you're using to do this
> The problem is when I use uc.getInputStream() I can only call it once. The 2nd time it gives me null. So I figured out that I had to use uc.getContent(), but then I can't save the file properly :s
you'll need to call it once for each file.
ie. each request returns a single frame.
To stream the requests you need to implement some way to mark where each frame ends.
For example send the size of the frame, followed by the b=frame bytes, followed by the size of the next frame etc.
you'll need to call it once for each file.
ie. each request returns a single frame.
To stream the requests you need to implement some way to mark where each frame ends.
For example send the size of the frame, followed by the b=frame bytes, followed by the size of the next frame etc.
ASKER
@CEHJ: the code's above, 2 different ways to fetch and save the image.
@objects:
> For example send the size of the frame, followed by the b=frame bytes, followed by the size of the next frame etc.
Do u mean by sending out Content-Length ? Or do I just have to disconnect and reconnect every second ?
@objects:
> For example send the size of the frame, followed by the b=frame bytes, followed by the size of the next frame etc.
Do u mean by sending out Content-Length ? Or do I just have to disconnect and reconnect every second ?
no by including the frame size in the stream
o/wise you need to reconnect to get each frame.
o/wise you need to reconnect to get each frame.
ASKER
I'm sorry, I'm not following... "including the frame size in the stream", where do I have to send out the frame size ? In the headers ?
private void sendHttpRequest() {
try {
/* Post header */
Ticket ticket = new Ticket(); // The ticket is something that webserver uses...
uc.setRequestProperty("Hos t", this.hostname);
uc.setRequestProperty("Tic ket-Data", ticket.getData());
uc.setRequestProperty("Use r-Agent", "jsyntax");
uc.setRequestProperty("Tic ket-Id", ticket.getId());
uc.connect();
} catch (Exception e) {
System.out.println("sendHt tpRequest: " + e.getMessage());
}
}
public void startReceiving() {
int i = 0;
try {
sendHttpRequest();
while(uc.getContentLength( ) > 0) {
DataInputStream dis = new DataInputStream(uc.getInpu tStream()) ;
int bufferSize = uc.getContentLength();
byte[] buf = new byte[bufferSize];
dis.readFully(buf);
saveBytesToFile(buf, "test_" + i++ + ".jpg");
this.sleep(1000);
}
} catch (Exception e) {
System.out.println("startR eceiving: " + e.getMessage());
}
}
private void sendHttpRequest() {
try {
/* Post header */
Ticket ticket = new Ticket(); // The ticket is something that webserver uses...
uc.setRequestProperty("Hos
uc.setRequestProperty("Tic
uc.setRequestProperty("Use
uc.setRequestProperty("Tic
uc.connect();
} catch (Exception e) {
System.out.println("sendHt
}
}
public void startReceiving() {
int i = 0;
try {
sendHttpRequest();
while(uc.getContentLength(
DataInputStream dis = new DataInputStream(uc.getInpu
int bufferSize = uc.getContentLength();
byte[] buf = new byte[bufferSize];
dis.readFully(buf);
saveBytesToFile(buf, "test_" + i++ + ".jpg");
this.sleep(1000);
}
} catch (Exception e) {
System.out.println("startR
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now I'm trying to save an Image object to a jpeg file .. can find how to save BufferedImage, but that's not what I need :(