当qr代码被扫描时,Zxing将恢复为菜单

时间:2012-03-04 15:52:40

标签: android barcode zxing

我将zxing库导入到我的项目中,扫描仪就像一个魅力,但当我扫描一个qr代码时,它说Qr代码找到并返回到我设置的菜单有没有办法显示结果和设置它以打开网址

package com.Qrgolf.App;

import java.util.regex.Pattern;

import com.google.zxing.Result;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Button scan = (Button) findViewById(R.id.SCANBUTTON);

    scan.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
            startActivityForResult(intent, 0);
        }
    });
    };

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                // Handle successful scan
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果答案正确,你应该考虑回到原来的问题并接受答案。

此外,您需要更改onActivityResult()方法,以便对从QR中生成的字符串执行任何操作。

这是一个例子:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            // Handle successful scan
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(android.net.Uri.parse(contents));
            startActivity(intent);
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
}