4 package it
.lilik
.capturemjpeg
;
6 import java
.io
.BufferedOutputStream
;
7 import java
.io
.ByteArrayInputStream
;
9 import java
.io
.FileNotFoundException
;
10 import java
.io
.FileOutputStream
;
11 import java
.io
.IOException
;
12 import java
.io
.PipedInputStream
;
13 import java
.io
.PipedOutputStream
;
15 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
18 * @author Alessio Caiazza
19 * @author Cosimo Cecchi
22 public class CaptureToFile
{
26 private AsyncProducer client
;
31 public static void main(String
[] args
) {
32 if (args
.length
< 2 || args
.length
== 3 || args
.length
> 4) {
33 System
.err
.println("Wrong parameters");
34 System
.out
.println("Usage:");
37 String user
= null , pass
= null;
38 if (args
.length
== 4) {
42 CaptureToFile ctf
= new CaptureToFile(args
[0],
46 //start capturing images from buffer
49 } catch (FileNotFoundException e
) {
54 } catch (InterruptedException e
) {
60 public CaptureToFile(String path
, String url
,
61 String user
, String password
) {
62 if (path
.lastIndexOf('/') == path
.length())
65 this.path
= path
+ "/";
68 client
= new AsyncProducer(new GetMethod(url
),
72 public void capture(int images
) throws FileNotFoundException
{
75 BufferedOutputStream os
;
76 ByteArrayInputStream img
;
77 byte buff
[] = new byte[1024];
79 for( int i
= 0; i
< images
; i
++ ) {
80 fout
= new File(getPath() + String
.valueOf(i
) + ".jpg");
81 fos
= new FileOutputStream(fout
);
82 os
= new BufferedOutputStream(fos
);
84 //TODO: is this the better way?
85 while( !client
.isImageAvailable() ) {
91 while((readBytes
= img
.read(buff
)) != -1) {
92 os
.write(buff
, 0, readBytes
);
96 } catch (IOException e
) {
105 public synchronized String
getPath() {
109 public CaptureToFile(String path
, String url
) {
110 this(path
, url
, null, null );
113 public void start() {
117 public void stop() throws InterruptedException
{
118 client
.setShouldStop(true);