我写了一个代码来扫描QR码。但是,我想通过从文本框或任何其他来源获取输入来生成QR码。我一直在寻找它,但似乎没有这样的选择。我必须编写本机代码,而且我遇到了问题。请指导我。 我已经这样做了 -使用codenameone生成QR码
final TextField text = new TextField();
final SpanButton qrGen = new SpanButton("Generate QR");
qrGen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Comes here.....");
MyNative n = (MyNative)NativeLookup.create(MyNative.class) ;
if(n != null && n.isSupported()) {
String path = (String) evt.getSource();
System.out.println("Comes here2.....");
InputStream is = null;
try {
is = com.codename1.io.FileSystemStorage.getInstance().openInputStream(path);
System.out.println("Comes here3.....");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Image i = null;
try {
i = Image.createImage(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//(n.genQR(text.getText()));
ImageViewer iv = new ImageViewer(i);
g.removeAll();
g.addComponent(iv);
f.addComponent(BorderLayout.SOUTH, g);
f.show();
}
}
});
g.addComponent(text);
g.addComponent(qrGen);
f.addComponent(BorderLayout.SOUTH, g);
f.show();
的MyNative
对象保持空,永远达不到内部块。 这是我的本地实现 -
package com.codename1;
import com.google.zxing.QRCodeWriter;
public class MyNativeImpl implements MyNative {
public Object genQR(String param) {
ImageView imageView = (ImageView) findViewById(R.id.qrCode);
try {
return encodeAsBitmap(STR);
} catch (WriterException e) {
e.printStackTrace();
}
}
Bitmap encodeAsBitmap(String str) throws WriterException {
BitMatrix result;
try {
result = new QRCodeWriter().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;
}
public boolean isSupported() {
return true;
}
}