2015-10-05 105 views
0

我写了一个代码来扫描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; 
} 

} 

回答

0

本机接口只能在设备上工作。

它看起来不像你的本地接口是有效的,因为你从非法接口返回一个对象。你应该返回一个byte[]

本机参数/返回类型受限于更好的语言互操作性。

0

如果您的应用将在线运行,我会建议您使用Google QR code api。它为你生成一个QR,返回一个图像,不需要编写本地代码。 Google QR图片here的示例。否则,您应该使用真实设备调试您的应用程序,因为本机接口不能在模拟器上工作,除非您也编写Java本机代码。下面

代码应该帮助你与你的原生界面,没有经过测试,在这里写上计算器:

的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代码中返回的图像转换为byte []。如果您不知道如何,请查看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();