Bharath, a long time user of LWUIT asked on Stack Overflow whether Codename One supported QRCode/ZXing. Since he is one of many who asked for this feature I decided now would probably be a good time to push that feature forward.
Initially I planned to add the support directly into Codename One (and I intend to do this soon enough) but right now I wanted to get something going as soon as possible and also show a more realistic example of using the native API's. So I decided to write everything using the standard Codename One native API support which allows me to define a native interface and implement the code in C/Dalvik etc.
The Android implementation was a breeze! Its just an intent:
public void scan() {
result = ZXingNativeCalls.PENDING;
com.codename1.impl.android.CodenameOneActivity ctx = (com.codename1.impl.android.CodenameOneActivity)Zxing.getContext();
android.content.Intent intent = new android.content.Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
ctx.setIntentResultListener(new IntentResultListener() {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == com.codename1.impl.android.CodenameOneActivity.RESULT_OK) {
contents = data.getStringExtra("SCAN_RESULT");
format = data.getStringExtra("SCAN_RESULT_FORMAT");
result = ZXingNativeCalls.OK;
} else if (resultCode == com.codename1.impl.android.CodenameOneActivity.RESULT_CANCELED) {
// Handle cancel
result = ZXingNativeCalls.ERROR;
}
}
}
});
ctx.startActivityForResult(intent, 0);
}
Notice that I store the context in the application then I can just access it easily from Android and work with that. The iOS application seemed simple enough when I started but took me a couple of days mostly because of the inherent complexity of the zxing code base which relies on compiling and linking an underlying C++ library and invoking it from the Objective-C code. I tried following their tutorials repeatedly ending with linker errors (the joys of native code... ugh).
Eventually I decided to just refactor all the code and just take the entire zxing project into the build environment. While this eventually worked it turned out to be pretty difficult to do. The main issue is that due to our current native implementation on iOS hierarchies of source files are flattened when building the project. The C++ code and the Objective-C code rely heavily on hierarchies and have duplicate file names that differ only in their hierarchy. Flattening this hierarchy was painful work but eventually I got the whole thing flattened. The mixing of C++ with Objective-C code was also pretty painful to work with.
Eventually I got a basic project working and you can see the full source code (including the modified Zxing) in the demos directory in the repository.
Monday, April 23, 2012
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment