2013-05-29 56 views
0

Android 2.3.3使用ZXing库编码URL

我想使用ZXing库编码一个简单的URL。

我只需要在下面的文件中编码URL字段(id = edittext_url_link)。

数据输入的字段 - >URL Title = My Website. URL = http://www.mywebsite.com

XML文件:::

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="300dp" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textview_url_header" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Enter URL to Encode:" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" /> 

    <TextView 
     android:id="@+id/textview_url_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="3dp" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:text="URL Title" /> 

    <EditText 
     android:id="@+id/edittext_url_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:ems="10" > 
    </EditText> 

    <TextView 
     android:id="@+id/textview_url_link" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="3dp" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:text="URL" /> 

    <EditText 
     android:id="@+id/edittext_url_link" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:ems="10" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="3dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="20dp" 
     android:layout_gravity="center" 
     android:gravity="center" > 

     <Button 
      android:id="@+id/button_url_encode" 
      android:layout_width="0dip" 
      android:layout_height="60dp" 
      android:layout_weight="0.50" 
      android:text="Encode" 
      android:layout_gravity="center" 
      android:gravity="center" /> 

     <Button 
      android:id="@+id/button_url_cancel" 
      android:layout_width="0dip" 
      android:layout_height="60dp" 
      android:layout_weight="0.50" 
      android:text="Cancel" 
      android:layout_gravity="center" 
      android:gravity="center" /> 
    </LinearLayout> 

</LinearLayout> 

Java文件:::

public class URLEncodeActivity extends Activity implements OnClickListener{ 

    EditText edtxtURLTitle, edtxtURL; 
    Button btnEncode, btnCancel; 

    String urlTitle, url; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_url); 

     edtxtURLTitle = (EditText) findViewById(R.id.edittext_url_title); 
     edtxtURL = (EditText) findViewById(R.id.edittext_url_link); 
     btnEncode = (Button) findViewById(R.id.button_url_encode); 
     btnCancel = (Button) findViewById(R.id.button_url_cancel); 


     urlTitle = edtxtURLTitle.getText().toString(); 
     url = edtxtURL.getText().toString(); 

     btnEncode.setOnClickListener(this); 
     btnCancel.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     if(v.getId() == R.id.button_url_encode) 
     { 
      Intent intent = new Intent(Intents.Encode.ACTION); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
      intent.putExtra(Intents.Encode.TYPE, Contents.Type.TEXT); 
      intent.putExtra(Intents.Encode.DATA, url); 
      intent.putExtra(Intents.Encode.FORMAT, BarcodeFormat.QR_CODE.toString()); 
      startActivity(intent); 

     } 
     else 
     { 

     } 

    } 

} 

不过,我不断收到一个对话的说法,“无法从提供的数据中编码条形码“。我做错了什么?


[编辑]

对于未来的参考,但不建议导入斑马线库代码到您的项目,我做到了,这里是我的URL获得的呼吁编码。

  Intent intent = new Intent(
        "com.xxx.xx.android.ENCODE"); 
      String data = edtxtURL.getText().toString(); 
      intent.putExtra(Const.FROM_ACTIVITY, "URL"); 
      intent.putExtra("ENCODE_DATA", data); 
      intent.putExtra("ENCODE_TYPE", Contents.Type.TEXT); 
      intent.putExtra("ENCODE_FORMAT", 
        BarcodeFormat.QR_CODE.toString()); 
      startActivity(intent); 
+0

@瓦米斯查拉你解决了你的问题? – Erum

+0

@ErumHannan,我将库代码导入到我的项目中,并使用上面的代码(在编辑部分)完成我的工作。 –

+0

你可以来http://chat.stackoverflow.com/rooms/19132/java-and-android-era编码问题 – Erum

回答

0

不要尝试这个自己编写,而是使用IntentIntegrator.shareText()https://code.google.com/p/zxing/source/browse/trunk/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java

有可能在这里使用标准库调用避免了一些小问题。

如果我不得不猜测超出这一点,我猜测该URL实际上并没有“http://”或有一个额外的空间或其他东西。

+0

但代码似乎很清楚,数据是好的。在尝试对电子邮件地址,电话号码和其他普通事物进行编码时,我遇到了同样的错误。这个信息,给你一个想法发生了什么? –

+0

上述评论中的小修改。上述(正在讨论的方法)正在处理电话号码和文本。但它给电子邮件和URL带来了错误。 –

+0

我没有看到URL的问题。尝试在条码扫描器中共享书签,或尝试使用ZXing Test。对于电子邮件,我认为该类型需要是“EMAIL_TYPE” –