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