d156b20a7dac81e3ec4d650b70fb256c14fb37de
[zxing.git] / javame / src / com / google / zxing / client / j2me / VideoCanvas.java
1 /*
2  * Copyright 2007 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.j2me;
18
19 import javax.microedition.lcdui.Canvas;
20 import javax.microedition.lcdui.Command;
21 import javax.microedition.lcdui.CommandListener;
22 import javax.microedition.lcdui.Displayable;
23 import javax.microedition.lcdui.Graphics;
24
25 /**
26  * The main {@link Canvas} onto which the camera's field of view is painted.
27  * This class manages decoding via {@link SnapshotThread}.
28  *
29  * @author Sean Owen
30  */
31 final class VideoCanvas extends Canvas implements CommandListener {
32
33   private static final Command exit = new Command("Exit", Command.EXIT, 1);
34
35   private final ZXingMIDlet zXingMIDlet;
36   private final SnapshotThread snapshotThread;
37
38   VideoCanvas(ZXingMIDlet zXingMIDlet) {
39     this.zXingMIDlet = zXingMIDlet;
40     addCommand(exit);
41     setCommandListener(this);
42     snapshotThread = new SnapshotThread(zXingMIDlet);
43     new Thread(snapshotThread).start();
44   }
45
46   protected void paint(Graphics graphics) {
47     // do nothing
48   }
49
50   protected void keyPressed(int keyCode) {
51     // Any valid game key will trigger a capture
52     if (getGameAction(keyCode) != 0) {
53       snapshotThread.continueRun();
54     } else {
55       super.keyPressed(keyCode);
56     }
57   }
58
59   public void commandAction(Command command, Displayable displayable) {
60     int type = command.getCommandType();
61     if (type == Command.EXIT || type == Command.STOP || type == Command.BACK || type == Command.CANCEL) {
62       snapshotThread.stop();
63       zXingMIDlet.stop();
64     }
65   }
66 }