我编写了扫描QR码的代码。但是,我想通过从文本框或任何其他来源获取输入来生成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
对象保持为null,永远不会到达内部块。
这是我的原生实现 -
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;
}
}
答案 0 :(得分:0)
本机界面仅适用于设备本身。
看起来你的本地接口是有效的,因为你从界面返回一个非法的Object。您应该返回byte[]
。
原生参数/返回类型仅限于更好的语言互操作性。
答案 1 :(得分:0)
如果您的应用将在线投放,我建议您使用Google QR code api。它为您生成QR,返回图像并且不需要编写本机代码。 Google QR图片示例here。否则,您应该使用真实设备调试您的应用程序,因为除非您编写java本机代码,否则本机界面不会在模拟器上工作。
下面的代码应该可以帮助您使用本机接口,而不是在stackoverflow上测试和编写:
Android MyNativeImpl.java:
package com.yourpackage.anything;
import com.google.zxing.QRCodeWriter;
public class MyNativeImpl {
public byte[] generateQRFromString(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);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
}
<强> MyNative.java 强>
package com.yourpackage.anything;
import com.codename1.system.NativeInterface;
public interface MyNative extends NativeInterface {
public byte[] generateQRFromString(String str);
}
现在您可以将返回的字节[]转换为CN1代码中的图像。如果您不知道如何,请查看CN1 Javadoc或@Shai是否可以帮助您使用代码段。根据您尝试做的事情,您可以尝试以下方式:
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()) {
byte[] byteData = n.generateQRFromString("Hello world");
if (byteData != null) {
Image img = Image.createImage(byteData, 0, byteData.length);
ImageViewer iv = new ImageViewer(img);
g.removeAll();
g.addComponent(iv);
f.addComponent(BorderLayout.SOUTH, g);
f.show();
} else {
//print My native code returned no image
}
}
}
});
g.addComponent(text);
g.addComponent(qrGen);
f.addComponent(BorderLayout.SOUTH, g);
f.show();