In Android, how to use ZXing to generate QR code

1,032 views
Skip to first unread message

KK

unread,
Nov 5, 2011, 12:22:59 PM11/5/11
to zx...@googlegroups.com
hi All, 
I was trying to generate QR code using the ZXing utility. I was able to do it on my windows system, however, when I tried to use the same code as part of a small android app, it gave me errors. The error message is like this:

11-05 21:50:12.917: ERROR/AndroidRuntime(273): java.lang.NoClassDefFoundError: java.awt.image.BufferedImage
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(MatrixToImageWriter.java:48)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at com.google.zxing.client.j2se.MatrixToImageWriter.writeToStream(MatrixToImageWriter.java:75)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at com.infineon.qrtest.ZXingTestActivity.encodeQR(ZXingTestActivity.java:109)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at com.infineon.qrtest.ZXingTestActivity$1.onClick(ZXingTestActivity.java:70)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at android.view.View.performClick(View.java:2408)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at android.view.View$PerformClick.run(View.java:8816)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at android.os.Handler.handleCallback(Handler.java:587)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at android.os.Looper.loop(Looper.java:123)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at java.lang.reflect.Method.invoke(Method.java:521)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-05 21:50:12.917: ERROR/AndroidRuntime(273):     at dalvik.system.NativeStart.main(Native Method)

I think, this is because AWT is not supported on Android OS, is that correct. In that case what is the way to generate QR code using the features/functionality supported by Android ? 
Would appreciate if someone can point me to some sample code, URL etc. 

The code, I'm using in android is as following:

<code snippet>
public void encodeQR(String text, int height, int width)
throws WriterException, FileNotFoundException, IOException {
// (ImageIO.getWriterFormatNames() returns a list of supported formats)
// could be "gif", "tiff", "jpeg"
String imageFormat = "png";
BitMatrix bitMatrix = new QRCodeWriter().encode(text,
BarcodeFormat.QR_CODE, width, height);
// FileOutputStream fos = new FileOutputStream(new File(sdCard + "/"
// + "myQR.png"));
FileOutputStream fos = openFileOutput("myqr.png", Context.MODE_PRIVATE);
MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, fos);
Log.d(TAG, "Written QR image at:" + sdCard + "myQR.png");

}
<code snippet>
  
Thanks in advance. 

Regards, 
KK

Sean Owen

unread,
Nov 5, 2011, 1:03:58 PM11/5/11
to zx...@googlegroups.com
That's right, there is no AWT. You should not be using any code in javase/. Please look at how  QR codes are encoded and displayed by the Android client, in android/

KK

unread,
Nov 5, 2011, 1:16:11 PM11/5/11
to zx...@googlegroups.com
okay. In that case, I think, I should spend more time looking at the sample codes, API docs etc. Thanks for your quick response. 

Regards, 
kk

David Williams

unread,
Nov 5, 2011, 3:56:37 PM11/5/11
to KK, zx...@googlegroups.com

use android interface under zxing and use core.jar in your android project
use android client to use response to encoding after scanning via interface
android interface is to interface with zxing app downloaded from market installed
in android
it returns to ai method the barcode type and results

On 2011 11 5 13:16, "KK" <dioxide....@gmail.com> wrote:

okay. In that case, I think, I should spend more time looking at the sample codes, API docs etc. Thanks for your quick response. 

Regards, 

kk



On Sat, Nov 5, 2011 at 10:33 PM, Sean Owen <sro...@gmail.com> wrote:
>

> That's right, there is no...

Reply all
Reply to author
Forward
0 new messages