问题
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):
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.
回答1:
Alright, I've fixed my problem (of generating a QR-encoded image from a string) by looking at the android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java:
Here is my 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;
}
Also, I have taken core.jar from Maven repository (here fullscreen):
Finally, riginal problem of Eclipse not being able to use jar files I've built with Maven was coming from the Java version - to be fixed in Project Properties or Eclipse Settings:
来源:https://stackoverflow.com/questions/30515584/qr-encode-a-string-to-image-in-android-project-using-zxing