【问题标题】:Zxing barcode matching app for androidZxing 条码匹配app for android
【发布时间】:2013-10-05 17:23:51
【问题描述】:

我正在尝试开发一个应用程序,它将扫描条形码,将值存储在变量中,然后扫描另一个条形码并将该值与前一个值匹配并返回“匹配”或“不匹配”结果。感谢foamyguy的帮助,我已经完成了第一部分的工作,这是我使用的初始代码,

package com.barcodesample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BarcodeSample extends Activity {

private Button scanBtn;
private TextView resultsTxt;


/** Called when the activity is first created. */;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    scanBtn = (Button) findViewById(R.id.scanBtn);
    resultsTxt = (TextView) findViewById(R.id.resultsTxt);




    scanBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            resultsTxt.setText("Scanning...");




        Intent intent = new intent("com.google.zxing.client.android.SCAN"); 


            try {
                startActivityForResult(intent, 0);
            } catch (ActivityNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

                new AlertDialog.Builder(v.getContext())
                .setTitle("WARNING:")
                            .setMessage("You don't have Barcode Scanner  installed. Please install it.")
                .setCancelable(false) 
                .setNeutralButton("Install it now", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");  
                        startActivity(new Intent(Intent.ACTION_VIEW, uri));                                         
                    }
                })
                .show();
            }

        }
    });



}

/*Here is where we come back after the Barcode Scanner is done*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            // contents contains whatever the code was
            String contents = intent.getStringExtra("SCAN_RESULT");

            // Format contains the type of code i.e. UPC, EAN, QRCode etc...
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

            // Handle successful scan. In this example I just put the results into the TextView
            resultsTxt.setText(format + "\n" + contents);
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel. If the user presses 'back' before a code is scanned.
            resultsTxt.setText("Canceled");
        }
    }
}

}

现在我如何使用与上述类似的另一个扫描按钮启动另一个意图,最后如何比较两个值并在新屏幕中返回匹配或不匹配?

提前感谢您的宝贵建议

【问题讨论】:

  • 为什么不使用相同的按钮来启动 ZXing 并在返回时与旧保存的值进行比较?
  • 您不是返回到启动扫描活动的同一个活动吗?
  • 是的,同样的活动。但是我该如何重新开始呢? @尼丁
  • @sharj - 我需要两个按钮以避免混淆

标签: java android barcode zxing barcode-scanner


【解决方案1】:

在其他按钮上使用相同的代码,但以下除外。

startActivityForResult(intent, 1); // right now it's 0 for button 1

现在在 onActivityResult 中再添加一个部分,用于返回第二个按钮的结果。

if (requestCode == 1) {
// your code here that will be used for comparing.
} 

【讨论】:

    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 2013-05-25
    • 2018-08-16
    • 1970-01-01
    相关资源
    最近更新 更多