- 从https://repo1.maven.org/maven2/com/google/zxing/core/获取最新的 core-xxxjar
- 在 Android Studio 中切换到项目文件夹视图并将 .jar 复制到您的
app/libs
文件夹中
- 右键单击 .jar 并选择“添加为库...”
这将自动将依赖项添加到您的build.gradle
,因此您可以开始使用该库,例如生成和显示条形码,就像本例中一样简单:
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
...
ImageView qrImg = (ImageView)findViewById(R.id.qrImageView);
int width = 512;
int height = 512;
QRCodeWriter writer = new QRCodeWriter();
try {
BitMatrix bitMatrix = writer.encode("Hello World", BarcodeFormat.QR_CODE,width,height);
Bitmap bmp = Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
// Copy pixel-by-pixel
bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
qrImg.setImageBitmap(bmp);
} catch (WriterException e) {
// Handle exception
}