2015-10-05 129 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();