Initial checkin of bug client code from buglabs

git-svn-id: https://zxing.googlecode.com/svn/trunk@473 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-06-24 19:00:09 +00:00
parent 753ca4afbf
commit 8fb592777b
8 changed files with 676 additions and 0 deletions

75
bug/build.xml Normal file
View file

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2008 ZXing authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project name="bug" default="build">
<property file="../build.properties"/>
<target name="init">
<tstamp/>
<fail message="Please build 'core' first">
<condition>
<not>
<available file="../core/core.jar" type="file"/>
</not>
</condition>
</fail>
<fail message="Please install BUG .jar files in lib/ first">
<condition>
<not>
<available file="lib/com.buglabs.common_1.0.0.jar" type="file"/>
<!-- Actually we need even more, just checking for one -->
</not>
</condition>
</fail>
</target>
<target name="build" depends="init">
<mkdir dir="build"/>
<javac srcdir="src"
destdir="build"
source="1.5"
target="1.5"
optimize="true"
debug="true"
deprecation="true">
<classpath>
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement location="../core/core.jar"/>
</classpath>
</javac>
<jar jarfile="bug.jar" basedir="build">
<manifest>
<attribute name="Bundle-Name" value="BugBarcode"/>
<attribute name="Bundle-Vendor" value="ZXing Project"/>
<attribute name="Bundle-SymbolicName" value="BugBarcode"/>
<attribute name="Bundle-Version" value="${version}"/>
<attribute name="Bundle-Activator" value="com.google.zxing.client.bug.Activator"/>
<attribute name="Import-Package" value="com.buglabs.application,com.buglabs.device,com.buglabs.bug.module.camera.pub,com.buglabs.bug.module.lcd.pub,com.buglabs.util,com.google.zxing,com.google.zxing.common,org.osgi.framework,org.osgi.util.tracker"/>
<attribute name="Bug-Bundle-Type" value="Application"/>
</manifest>
</jar>
</target>
<target name="clean">
<delete dir="build"/>
</target>
</project>

201
bug/lib/osgi-LICENSE.txt Normal file
View file

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

BIN
bug/lib/osgi.jar Normal file

Binary file not shown.

View file

@ -0,0 +1,77 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.bug;
import com.google.zxing.ReaderException;
import com.google.zxing.common.BaseMonochromeBitmapSource;
import java.awt.Image;
import java.awt.image.PixelGrabber;
/**
* <p>An implementation based on AWT's {@link Image} representation.
* This can be used on CDC devices or other devices that do not have access to the
* Mobile Information Device Profile and thus do not have access to
* javax.microedition.lcdui.Image.</p>
*
* @author David Albert
* @author Sean Owen
*/
public final class AWTImageMonochromeBitmapSource extends BaseMonochromeBitmapSource {
private final int height;
private final int width;
private final int[] pixels;
public AWTImageMonochromeBitmapSource(Image image) throws ReaderException {
height = image.getHeight(null);
width = image.getWidth(null);
pixels = new int[height * width];
// Seems best in this situation to grab all pixels upfront. Grabbing any individual pixel
// entails creating a relatively expensive object and calling through several methods.
PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
try {
grabber.grabPixels();
} catch (InterruptedException ie) {
throw new ReaderException("Interrupted while reading pixels");
}
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
/**
* See <code>com.google.zxing.client.j2me.LCDUIImageMonochromeBitmapSource</code> for more explanation
* of the computation used in this method.
*/
public int getLuminance(int x, int y) {
int pixel = pixels[x * width + y];
return (((pixel & 0x00FF0000) >> 16) +
((pixel & 0x0000FF00) >> 7) +
(pixel & 0x000000FF )) >> 2;
}
public void cacheRowForLuminance(int y) {
// do nothing; we are already forced to cache all pixels
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.bug;
import com.buglabs.util.ServiceFilterGenerator;
import com.google.zxing.client.bug.servicetracker.BugBarcodeServiceTracker;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.util.tracker.ServiceTracker;
/**
* BundleActivator for BugBarcode.
*
* @author David Albert
*/
public final class Activator implements BundleActivator {
private BugBarcodeServiceTracker barcodeServiceTracker;
private ServiceTracker serviceTracker;
public void start(BundleContext context) throws InvalidSyntaxException {
barcodeServiceTracker = new BugBarcodeServiceTracker(context);
Filter filter =
context.createFilter(ServiceFilterGenerator.generateServiceFilter(barcodeServiceTracker.getServices()));
serviceTracker = new ServiceTracker(context, filter, barcodeServiceTracker);
serviceTracker.open();
}
public void stop(BundleContext context) {
barcodeServiceTracker.stop();
serviceTracker.close();
}
}

View file

@ -0,0 +1,73 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.bug;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
/**
* @author John Connolly
*/
public final class ImageCanvas extends Canvas {
private Image image;
public ImageCanvas(Image image) {
this.image = image;
}
@Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
@Override
public void update(Graphics g) {
paint(g);
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
@Override
public Dimension getPreferredSize() {
int width;
do {
width = image.getWidth(this);
} while (width < 0);
int height;
do {
height = image.getHeight(this);
} while (height < 0);
return new Dimension(width, height);
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
}

View file

@ -0,0 +1,122 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.bug.app;
import com.buglabs.bug.module.camera.pub.ICameraDevice;
import com.buglabs.bug.module.camera.pub.ICameraModuleControl;
import com.buglabs.device.ButtonEvent;
import com.buglabs.device.IButtonEventListener;
import com.buglabs.device.IButtonEventProvider;
import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Reader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.client.bug.AWTImageMonochromeBitmapSource;
import com.google.zxing.client.bug.ImageCanvas;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.io.IOException;
/**
* @author David Albert
*/
public final class BugBarcodeApp implements IButtonEventListener, ImageObserver {
private ICameraDevice camera;
private ICameraModuleControl cameraControl;
private Frame frame;
private Image image;
private ImageCanvas imageCanvas;
private Label barcodeLabel;
private boolean pictureTaken;
private final Reader reader;
public BugBarcodeApp(Frame frame,
ICameraDevice camera,
ICameraModuleControl cameraControl,
IButtonEventProvider buttonProvider) {
this.frame = frame;
this.camera = camera;
this.reader = new MultiFormatReader();
this.cameraControl = cameraControl;
pictureTaken = false;
buttonProvider.addListener(this);
createUI();
}
private void createUI() {
frame.setTitle("BugBarcode");
frame.setBackground(Color.WHITE);
frame.setLayout(new BorderLayout());
barcodeLabel = new Label("Take a picture of a barcode!", Label.CENTER);
frame.add(barcodeLabel, BorderLayout.SOUTH);
imageCanvas = new ImageCanvas(null);
frame.setVisible(true);
}
private void shoot() throws IOException {
// get image from camera for use with physical bug
cameraControl.setLEDFlash(true);
image = Toolkit.getDefaultToolkit().createImage(camera.getImage()).getScaledInstance(400, 300, Image.SCALE_FAST);
cameraControl.setLEDFlash(false);
if (Toolkit.getDefaultToolkit().prepareImage(image, -1, -1, this)) {
drawAndScan();
}
}
private void drawAndScan() {
imageCanvas.setImage(image.getScaledInstance(216, 150, Image.SCALE_FAST));
if (!pictureTaken) {
frame.add(imageCanvas, BorderLayout.CENTER);
pictureTaken = true;
frame.setVisible(true);
}
imageCanvas.repaint();
try {
MonochromeBitmapSource source = new AWTImageMonochromeBitmapSource(image);
Result result = reader.decode(source);
barcodeLabel.setText(result.getText());
} catch (ReaderException re) {
barcodeLabel.setText("I can't find a barcode here");
}
}
public void buttonEvent(ButtonEvent event) {
if (event.getButton() == ButtonEvent.BUTTON_HOTKEY_1 && event.getAction() == 0) {
try {
shoot();
} catch (IOException ioe) {
// continue
}
}
}
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
if ((infoflags & ALLBITS) != 0) {
drawAndScan();
return false;
}
return true;
}
}

View file

@ -0,0 +1,79 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.bug.servicetracker;
import com.buglabs.application.AbstractServiceTracker;
import com.buglabs.bug.module.camera.pub.ICameraDevice;
import com.buglabs.bug.module.camera.pub.ICameraModuleControl;
import com.buglabs.bug.module.lcd.pub.IModuleDisplay;
import com.buglabs.device.IButtonEventProvider;
import com.google.zxing.client.bug.app.BugBarcodeApp;
import org.osgi.framework.BundleContext;
import java.awt.Frame;
import java.util.Collection;
/**
* Service tracker for the BugApp Bundle
*
* @author David Albert
*/
public final class BugBarcodeServiceTracker extends AbstractServiceTracker {
private IButtonEventProvider buttonEventProvider;
private Frame frame;
private BugBarcodeApp app;
public BugBarcodeServiceTracker(BundleContext context) {
super(context);
}
@Override
public void doStart() {
IModuleDisplay display = (IModuleDisplay) getService(IModuleDisplay.class);
ICameraDevice camera = (ICameraDevice) getService(ICameraDevice.class);
ICameraModuleControl cameraControl = (ICameraModuleControl) getService(ICameraModuleControl.class);
buttonEventProvider = (IButtonEventProvider) getService(IButtonEventProvider.class);
frame = display.getFrame();
app = new BugBarcodeApp(frame, camera, cameraControl, buttonEventProvider);
}
/**
* Called when a service that this application depends is unregistered.
*/
@Override
public void doStop() {
buttonEventProvider.removeListener(app);
frame.dispose();
}
/**
* Allows the user to set the service dependencies by
* adding them to services list returned by getServices().
* i.e.nl getServices().add(MyService.class.getName());
*/
@Override
public void initServices() {
Collection<String> appServices = (Collection<String>) getServices();
appServices.add("com.buglabs.bug.module.camera.pub.ICameraDevice");
appServices.add("com.buglabs.bug.module.lcd.pub.IModuleDisplay");
appServices.add(IButtonEventProvider.class.getName());
appServices.add(ICameraModuleControl.class.getName());
}
}