使用ZXing Library对URL进行编码

时间:2013-05-29 11:27:11

标签: android zxing

Android 2.3.3

我正在尝试使用ZXing库对一个简单的URL进行编码。

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

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

XML File :::

<?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 File :::

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
        {

        }

    }

}

但是,我一直在说对话,“无法根据提供的数据对条形码进行编码”。我做错了什么?


[编辑]

为了将来的参考,虽然不建议将ZXing库代码导入到您的项目中,但我这样做了,这是我为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);

1 个答案:

答案 0 :(得分:0)

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

这里可能存在一些小问题,即使用标准库调用会避免。

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