forked from ItBitCo/JavaFlyCam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera2File.java
365 lines (339 loc) · 14.9 KB
/
Camera2File.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package flycam2file; // 2017 March 24
import java.io.File;
import java.io.FileOutputStream;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Insets; // not available at win creation
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import fly2cam.FlyCamera; // (import instead fly0cam.FlyCamera for off-cam dev't)
public class Camera2File extends JFrame implements MouseListener {
private static final long serialVersionUID = 1L; // don't need it but Java insists {
final static String fiName = "FlyCapped"; // => "FlyCapped.By8";
final boolean PauseReco = false, MiniLog = false, NoisyFaker = false, TestColorz = false;
final static int BigCam = 2, fps = 15, FrameTime = 66, // 15/second
ImHi = 240*BigCam, ImWi = 320*BigCam, nPixels = ImHi*ImWi,
Lin2 = ImWi*2, Lin6 = Lin2*6, CornerBox = 32;
private BufferedImage theImag = null; // (needed in myAction)..
private static JFrame theWindow = null; // bottom-up right-to-left = Little-Endian..
public static class RunSoon implements Runnable { @Override
public void run() {starting();}} //~RunSoon
private FlyCamera theVideo = null;
private FileOutputStream theFile = null;
private Timer TickTock = null;
private BufferedImage theBuff = null;
private byte[] theHead = null;
private byte[] camBytes = null;
private int[] thePixels = null;
private final int[] Digitz = {0x696,0x9F1,0xB95,0x9DA,0x62F,0xDDA,0x6D2,0x8BC,0xA5A,0x4B6};
private class myAction implements ActionListener { @Override
public void actionPerformed(ActionEvent evt) {
theImag = null; // so paint will get next img
if (theWindow != null) theWindow.repaint();}} //~myAction
private myAction doOften = null; // private boolean TestColorz = true;
private int IzFile, tile, twosie, CamFPS, FrameNo, nFiles, toRecord;
public void mouseEntered(MouseEvent evt) {} // we don't need these but Java wants them..
public void mouseExited(MouseEvent evt) {}
public void mousePressed(MouseEvent evt) {}
public void mouseReleased(MouseEvent evt) {}
public boolean IsRecording() {return toRecord>0;}
public void StopRecording() {
System.out.println("** StopRecord " + (theFile != null) + " " + (theHead != null)
+ " +" + toRecord + " **");
if (theFile==null) return;
try { if (theHead != null) {
theHead[0] = (byte) 0xFC;
theHead[1] = (byte) 0xB8;
theHead[2] = (byte) 0;
theHead[3] = (byte) 0; // WB trashes the data..
theFile.write(theHead);}
theFile.flush();
theFile.close();
} catch (Exception ex) {}
theFile = null;
toRecord = 0;} //~StopRecording
public void WriteFrame() {
if (theFile==null) return;
if (camBytes==null) return;
if (theHead==null) return;
if (!PauseReco) if (toRecord>0) try {
theHead[0] = (byte) ((tile>>1)+0xFC); // =FD for tile=3
theHead[1] = (byte) 0xB8;
theHead[2] = (byte) (ImHi>>4);
theHead[3] = (byte) (ImWi>>4); // WB trashes the data..
theFile.write(theHead);
theFile.write(camBytes);
toRecord--;}
catch (Exception ex) {toRecord = 0;}
if (toRecord <= 0) StopRecording();} //~WriteFrame
public void ShoFrameNum() { //
int bitz, here, thar = ImWi*10-7, whom = FrameNo, colo = 0xFFCC00; // orange
int[] myPix = thePixels;
if (toRecord>0) if (!PauseReco) {
colo = 0xFF0000; // red
whom = toRecord;}
if (myPix == null) return;
while (true) {
bitz = whom/10;
here = whom-bitz*10;
whom = bitz;
if (here<0) break;
if (here>9) break;
bitz = Digitz[here]|0x1110000;
while (bitz>0x1110) {
if ((bitz&1) !=0)
myPix[thar] = colo;
bitz = bitz>>1;
if ((bitz&0x1000)==0) thar = thar-ImWi;
else thar = thar+ImWi*3-1;}
if (whom==0) break;
thar = thar-1;}} //~ShoFrameNum // (in Camera2File)
public void AddRedDot() {
int thar = ImWi*5+5;
int[] myPix = thePixels;
if ((toRecord&8) !=0) if (!PauseReco) return;
if (myPix == null) return;
// myPix[thar] = 0xFF0000;
myPix[thar+1] = 0xFF0000;
myPix[thar+2] = 0xFF0000;
myPix[thar+ImWi] = 0xFF0000;
if (!PauseReco) { // red dot is hollow if paused..
myPix[thar+ImWi+1] = 0xFF0000;
myPix[thar+ImWi+2] = 0xFF0000;}
myPix[thar+ImWi+3] = 0xFF0000;
thar = thar+ImWi*2;
myPix[thar] = 0xFF0000;
myPix[thar+3] = 0xFF0000;
myPix[thar+ImWi+1] = 0xFF0000;
myPix[thar+ImWi+2] = 0xFF0000;
if (PauseReco) return;
myPix[thar+1] = 0xFF0000;
myPix[thar+2] = 0xFF0000;} //~AddRedDot
public boolean CamSize(FlyCamera myVid) { // true if good
int hi = 0, wi = 0;
twosie = 0;
tile = 0;
if (myVid != null) {
tile = myVid.PixTile();
hi = myVid.Dimz();}
if (tile != 1) if (tile != 3) return false;
if (hi==0) return false;
wi = hi&0xFFFF;
hi = hi>>16;
if (wi != ImWi) {
if (hi+hi == ImHi) {
if (wi+wi == ImWi) twosie++;} // gotta double what cam gives us
else if (ImHi+ImHi == hi) if (ImWi+ImWi == wi)
twosie--; // gotta halve what cam gives us
if (twosie !=0) return true;}
else if (hi == ImHi) return true;
System.out.println("Wrong image size, should be "
+ wi + "x" + hi + " / " + ImWi + "x" + ImHi);
return false;} //~CamSize
public boolean RestartCam() { // true if did
boolean dunit = false;
FlyCamera myVid = theVideo;
if (camBytes != null) if (myVid != null) if (IzFile == 1) {
IzFile = -1;
myVid.Finish();
if (myVid.Connect(CamFPS)) if (CamSize(myVid)) dunit = myVid.NextFrame(camBytes);}
return dunit;} //~RestartCam
public boolean GetCameraImg() { // -> thePixels, =T if OK // (in Camera2File)
int rx, cx, zx = 0, whom = 0, here = 0, thar = 0;
boolean gotit = false;
byte[] myBy = camBytes; // local vars visible in debugger ;-)
int[] myPix = thePixels;
FlyCamera myVid = theVideo;
if (myBy != null) if (myPix != null) if (myVid != null)
gotit = myVid.NextFrame(myBy);
if (!gotit) {
if (toRecord==0) return false;
if (IzFile != 1) return false;
if (!RestartCam()) return false;}
if (IzFile <= 0) if (myVid != null) {
if(myVid.Live()) IzFile = 2;
else IzFile = 1;}
for (rx=0; rx <= ImHi-1; rx++) {
if (twosie>0) { // double what cam gives us..
if ((rx&1) !=0) { // recopy even lines onto odd..
for (cx=0; cx <= ImWi-1; cx++) {
myPix[thar] = myPix[thar-ImWi];
thar++;} //~for (cx)
continue;} //~if (rx&1)
zx = ImWi>>1;} //~if (twosie>0)
else zx = ImWi;
for (cx=zx-1; cx >= 0; cx += -1) {
if (myBy == null) break; // can't, Java throws an exception instead
if (twosie<0) zx = here+Lin2+Lin2; // (cam rows are double-wide)
else if (twosie>0) zx = here+ImWi; // (cam rows are half-wide)
else zx = here+Lin2;
if (tile==1) whom = ((((( (int) myBy[here])&255)<<8) // RG/GB
| ( (int) myBy[here+1])&255)<<8)
| ( (int) myBy[zx+1])&255; // (ignore 2nd green)
else whom = ((((( (int) myBy[zx])&255)<<8) // GB/RG
| ( (int) myBy[here])&255)<<8)
| ( (int) myBy[here+1])&255; // if (tile==3)
if (twosie<0) here = here+4; // (skip alternate pixels)
else here = here+2;
myPix[thar] = whom; // <- unsaturated result
if (twosie>0) { // gotta H-double what cam gives us..
thar++;
myPix[thar] = whom;}
thar++;} //~for (cx)
if (twosie<0) here = here+Lin6; // =ImWi*4*3: skip alternate cam lines
else if (twosie>0) here = here+ImWi; // (cam rows are half-wide)
else here = here+Lin2;} //~for (rx)
if (TestColorz) {
myBy[0] = 0; myBy[Lin2] = 0; // black..
myBy[1] = 0; myBy[Lin2+1] = 0;
if (tile==1) { // (RG/GB) // red..
myBy[2] = (byte) 255; myBy[Lin2+2] = 0;}
else {myBy[2] = 0; myBy[Lin2+2] = (byte) 255;}
myBy[3] = 0; myBy[Lin2+3] = 0;
if (tile==1) { // green..
myBy[4] = 0; myBy[Lin2+4] = (byte) 255;
myBy[5] = (byte) 255; myBy[Lin2+5] = 0;}
else {
myBy[4] = (byte) 255; myBy[Lin2+4] = 0;
myBy[5] = 0; myBy[Lin2+5] = (byte) 255;}
myBy[6] = 0; myBy[Lin2+6] = 0; // blue..
if (tile==1) {
myBy[7] = 0; myBy[Lin2+7] = (byte) 255;}
else {myBy[7] = (byte) 255; myBy[Lin2+7] = 0;}
myBy[8] = 119; myBy[Lin2+8] = 119; // gray..
myBy[9] = 119; myBy[Lin2+9] = 119;
myBy[10] = (byte) 255; myBy[Lin2+10] = (byte) 255; // white..
myBy[11] = (byte) 255; myBy[Lin2+11] = (byte) 255;}
return true;} //~GetCameraImg
public BufferedImage Int2BufImg(int[] pixels, int width, int height)
throws IllegalArgumentException {
int lxx = 0;
int[] theData = null; // Raster raz = null; DataBufferInt DBI = nell;
BufferedImage bufIm = null;
if (pixels != null) lxx = pixels.length;
if (lxx==0) return null;
if (width==ImWi) if (height==ImHi) bufIm = theBuff;
if (bufIm==null) {
bufIm = new BufferedImage(width,height, BufferedImage. TYPE_INT_RGB);
}
if (bufIm==null) return null;
theData = ((DataBufferInt) bufIm.
getRaster().getDataBuffer()). getData();
System. arraycopy(pixels,0,theData,0,lxx);
return bufIm;} //~Int2BufImg // (in Camera2File)
// @Override // unneeded, just gotta spell it correctly
public void paint(Graphics graf) {
Insets edges;
edges = getInsets(); // should be valid now
super.paint(graf);
if (theImag == null) try {
if (thePixels != null) { // already got default blue-gray or prior image
if (GetCameraImg()) { // ..camBytes trash if GetCam fails
ShoFrameNum();
if (toRecord>0) AddRedDot();}
else if (toRecord>0) StopRecording();
theImag = Int2BufImg(thePixels,ImWi,ImHi); // WriteFr writes (&trashes) camBytes..
if (toRecord>0) if (!PauseReco) if ((((FrameTime>>6)|IzFile|FrameNo)&1) !=0)
WriteFrame();} // assumes fps=15
if (theImag == null) {
System.out.println("Got null camera image " + FrameNo);
System.exit(-1);}}
catch (Exception ex) {theImag = null;}
if (theImag != null) if (graf != null) if (edges != null) {
FrameNo++;
graf.drawImage(theImag,edges.left,edges.top,null);}} //~paint
@Override public void mouseClicked(MouseEvent evt) {
int Vx = 0, Hx = 0; // we only implement/o'ride this one
String aName = ".By8";
File myFile = null;
FlyCamera myVid = theVideo;
if (evt != null) {
Hx = evt.getX();
Vx = evt.getY();}
System.out.println("Got click @ " + Vx + "," + Hx + " IzFi=" + IzFile + " toRec=" + toRecord
+ " nFi=" + nFiles + " theFi=" + (theFile != null) + " 2x=" + twosie);
if (IzFile==0) return; // ignore if no image on-screen
if (toRecord==0) {
if (theFile != null) StopRecording(); // sets it =null
if (evt==null) return;
if (theHead==null) return;
if (IzFile<0) if (!RestartCam()) return; // cam file stopped, can't restart
if (Vx<CornerBox) {
if (Hx<CornerBox) toRecord = 1; // top-left: do one frame
else if (Hx>ImWi-CornerBox) toRecord = 8;} // top-right: do 8 frames
else if (Vx>ImHi-CornerBox) {
if (Hx<CornerBox) toRecord = fps*20; // botm-left: do 20 secs
else if (Hx>ImWi-CornerBox) toRecord = fps*60;} // botm-right: do one minute
if (toRecord==0) toRecord = (fps*5)|1; // anywhere else: do 5 secs
if (toRecord>fps) toRecord = (toRecord)&-8|8; // always show red dot 1st & last 1/2 sec..
if (nFiles>0) aName = nFiles + aName;
else if (myVid != null) if (!myVid.Live()) aName = "0.By8";
aName = fiName + aName;
nFiles++; // if fails, probably can't write there..
myFile = new File(aName); try {
if (myFile != null) theFile = new FileOutputStream(myFile);
} catch (Exception ex) {theFile = null;}
// TestColorz = true;
System.out.println(".. open " + aName + " toRec=" + toRecord
+ " " + (myFile != null) + " " + (theFile != null));
if (theFile==null) toRecord = 0;} //~if (toRecord=0)
else StopRecording();} //~mouseClicked
private static void starting() {theWindow = new Camera2File();}
public static void main(String[] args) {
Runnable runFrameLater = new RunSoon();
System.out.println("(main) image size " + ImWi + "x" + ImHi + " = " + nPixels);
SwingUtilities.invokeLater(runFrameLater);}
Camera2File() { // doesn't get called (soon enough) in Java !?
int nx = nPixels;
boolean dunit = false;
Timer titok;
FlyCamera myVid;
String sayso = "= ";
int[] myPix;
tile = 0;
IzFile = 0;
nFiles = 0;
twosie = 0;
FrameNo = 0;
toRecord = 0; // number of frames to go (while recording)
CamFPS = 0;
if (fps==15) CamFPS = FlyCamera. FrameRate_15;
else if (fps==30) CamFPS = FlyCamera. FrameRate_30;
myPix = new int[nPixels];
thePixels = myPix;
while (nx>0) {
nx--;
myPix[nx] = 0x6699CC;} //~while // prefill with blue-gray
theHead = new byte[4];
theVideo = new FlyCamera();
doOften = new myAction();
TickTock = new Timer(FrameTime,doOften);
theBuff = new BufferedImage(ImWi,ImHi, BufferedImage. TYPE_INT_RGB);
camBytes = new byte[nPixels*4];
titok = TickTock;
myVid = theVideo;
try {
if (myVid.Connect(CamFPS)) dunit = CamSize(myVid);
} catch (Exception ex) {dunit = false;}
if (!dunit) {
System.out.println("Connect failed");
System.exit(-1);
return;}
else if (titok != null) titok.start(); // too noisy in log
sayso = sayso + ImWi + "x" + ImHi;
if (twosie>0) sayso = "doubled " + sayso;
else if (twosie<0) sayso = "halved " + sayso;
if (tile==1) sayso = "RG/GB " + sayso;
else if (tile==3) sayso = "GB/RG " + sayso;
System.out.println("Connected " + sayso);
setTitle("FlyCapture Camera"); // was: this.setTitle etc..
setSize(ImWi+18,ImHi+40); // make it larger for insets to come
setDefaultCloseOperation( JFrame. EXIT_ON_CLOSE);
addMouseListener(this); // (MouseListener)
setVisible(true);}} //~Camera2File (flycam2file) (CF)