我寫了一個代碼來掃描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;
}
}