使用zxing

时间:2015-05-28 19:32:30

标签: android zxing eclipse-adt

我正在尝试使用a simple Android app创建显示QR编码图像的zxing library

所以我在Mac OS Yosemite笔记本上安装了HomeBrewantmaven,并将ANDROID_HOME环境变量指向Android SDK的位置。

然后我从GitHub检出了最新的zxing并使用命令mvn package(并使用javac版本1.8.0_45)构建了它(看似没有任何错误)。

之后我在Eclipse中创建了一个带有空白Activity的新Android项目,并将3个jar文件复制到其libs目录中:

  • 机器人/库/芯3.2.1-SNAPSHOT.jar
  • 机器人核/目标/机器人核-3.2.1-SNAPSHOT.jar
  • 机器人/目标/机器人-4.7.4.jar

不幸的是,MainActivity.java中的简单代码无法编译:

@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();
    }
}

错误是(此处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

但与此同时,我可以通过调用tar工具看到这些(据称是Eclipse没有找到)类:

# 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

我有什么不对的,为什么Eclipse不能找到这些类?

我也问了我的问题at GitHub

1 个答案:

答案 0 :(得分:2)

好吧,我通过查看android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java来解决我的问题(从字符串生成QR编码图像):

screenshot

这是我的MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageView = (ImageView) findViewById(R.id.qrCode);
    try {
        Bitmap bitmap = encodeAsBitmap(STR);
        imageView.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}

Bitmap encodeAsBitmap(String str) throws WriterException {
    BitMatrix result;
    try {
        result = new MultiFormatWriter().encode(str, 
            BarcodeFormat.QR_CODE, WIDTH, WIDTH, null);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    int w = result.getWidth();
    int h = result.getHeight();
    int[] pixels = new int[w * h];
    for (int y = 0; y < h; y++) {
        int offset = y * w;
        for (int x = 0; x < w; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, w, h);
    return bitmap;
}

另外,我从Maven repository(此处fullscreen)获取了 core.jar

eclipse screenshot

最后,Eclipse无法使用我用Maven构建的jar文件的严重问题来自Java版本 - 要在项目属性 Eclipse设置中修复

JDK screenshot