vendredi 29 mai 2015

QR-encode a String to Image in Android project using zxing

I am trying to create a simple Android app, which would display a QR-coded image - by using zxing library.

So I have installed HomeBrew, ant and maven at my Mac OS Yosemite notebook and pointed the ANDROID_HOME environment variable to the location of Android SDK.

Then I have checked out latest zxing from GitHub and built it (seeming without any errors) with the command mvn package (and using javac version 1.8.0_45).

After that I have created a new Android project with blank Activity in Eclipse and copied the 3 jar files into its libs directory:

  • android/libs/core-3.2.1-SNAPSHOT.jar
  • android-core/target/android-core-3.2.1-SNAPSHOT.jar
  • android/target/android-4.7.4.jar

Unfortunately, my simple code in MainActivity.java does not compile:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView imageView = (ImageView) findViewById(R.id.qrCode);
    String qrData = "Data I want to encode in QR code";
    int qrCodeDimention = 500;
    QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(qrData, 
            null,
            Contents.Type.TEXT, 
            BarcodeFormat.QR_CODE.toString(), 
            qrCodeDimention);

    try {
        Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
        imageView.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}

The errors are (here fullscreen):

eclipse screenshot

BarcodeFormat cannot be resolved
Contents cannot be resolved to a variable
QRCodeEncoder cannot be resolved to a type
QRCodeEncoder cannot be resolved to a type
WriterException cannot be resolved to a type

But at the same time I can see these (supposedly not found by Eclipse) classes by calling tar tool:

# tar tvfz libs/core-3.2.1-SNAPSHOT.jar | grep -i WriterException
-rwxrwxrwx  0 0 0 0 28 Mai 20:35 com/google/zxing/WriterException.class
# tar tvfz libs/core-3.2.1-SNAPSHOT.jar | grep -i BarcodeFormat
-rwxrwxrwx  0 0 0 0 28 Mai 20:35 com/google/zxing/BarcodeFormat.class
# tar tvfz libs/android-4.7.4.jar | grep -i QRCodeEncoder
-rwxrwxrwx  0 0 0 0 28 Mai 20:39 com/google/zxing/client/android/encode/QRCodeEncoder.class

What am I doint wrong please, why can't Eclipse find the classes?

I have asked my question at GitHub as well.

Aucun commentaire:

Enregistrer un commentaire