简体   繁体   English

Android Studio条形码扫描仪应用程序。 通过意图扫描

[英]Android Studio Bar Code Scanner App. Scanning Via Intent

I have an android app that has a very simple layout. 我有一个布局非常简单的android应用。 A button (scan_barcode1) that is followed by an edittext (scan_content) and another button (scan_barcode2) followed by another editext (scan_content2). 一个按钮(scan_barcode1),后跟一个edittext(scan_content),另一个按钮(scan_barcode2),后跟另一个editext(scan_content2)。

     <Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/scanner"
    android:id="@+id/scan_barcode1"
    android:layout_below="@+id/spinner7"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="25dp"
    android:onClick="openScanner1" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/scan_content"
    android:layout_below="@+id/scan_barcode1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="@string/serial_number"
    android:layout_alignRight="@+id/spinner7"
    android:layout_alignEnd="@+id/spinner7" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/scanner"
    android:id="@+id/scan_barcode2"
    android:layout_below="@+id/scan_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="openScanner2" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/scan_content2"
    android:layout_below="@+id/scan_barcode2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="@string/serial_number"
    android:layout_alignRight="@+id/scan_content"
    android:layout_alignEnd="@+id/scan_content" />

WHAT I WANT MY APP TO DO: Upon pressing a button, the scan functionality will open, scan the barcode and place the serial number in the edittext below it. 我希望我的应用程序执行的操作:按下按钮后,扫描功能将打开,扫描条形码并将序列号放在其下方的编辑文本中。 For example if you press scan_barcode1 the serial number will be placed in scan_content and if scan_barcode2 is pressed the serial number will appear in scan_content2. 例如,如果您按下scan_barcode1,则序列号将被放置在scan_content中;如果按下scan_barcode2,则序列号将出现在scan_content2中。

WHAT MY APP DOES: Upon pressing a button, the scan functionality will open, scan the bar code is placed in both editext in the activity. 我的应用程序的功能:按下按钮后,扫描功能将打开,扫描条形码被置于活动的两个editext中。 For example, if I press scan_barcode1, the serial number is placed in scan_content and scan_content2. 例如,如果我按scan_barcode1,则序列号将放置在scan_content和scan_content2中。

The following is the code for the intents that call the bar code scanner. 以下是调用条形码扫描器的意图的代码。 I am using the zxing library fyi. 我正在使用zxing库fyi。

    //This method opens the barcode scanner when scan_barcode1 button is pressed

    public void openScanner1 (View view) {
    IntentIntegrator scanIntegrator = new IntentIntegrator (this);
    scanIntegrator.initiateScan();
}



    //This method opens the bar code scanner when scan_barcode2 is pressed

    public void openScanner2 (View view) {
    IntentIntegrator scanIntegrator = new IntentIntegrator(this);
    scanIntegrator.initiateScan();
}

I initialize the two editText as contentTxt and contentTxt2 我将两个editText初始化为contentTxt和contentTxt2

     contentTxt = (EditText) findViewById(R.id.scan_content);
    contentTxt2 = (EditText) findViewById(R.id.scan_content2);

I then begin the OnActivityResult Method which retrieves the edittext and is supposed to place it in the correct edittext (it doesnt) 然后,我开始OnActivityResult方法,该方法检索编辑文本,并应将其放置在正确的编辑文本中(它没有)

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

    //Places barcode in the scan_content editext

    if (scanningResult != null) {
        String scanContent = scanningResult.getContents();
        contentTxt.setText(scanContent);
    }

    //places bar code in the scan_content2 edittext
    if (scanningResult !=null) {
        String scanContent = scanningResult.getContents();
        contentTxt2.setText(scanContent);
    }


    else{
        Toast toast = Toast.makeText(getApplicationContext(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
    }

}

Instead the barcodes just replicate across both of the edittext. 取而代之的是,条形码只是在两个edittext之间复制。 I have been stuck on this problem for some time and Im desperate for a solution. 我已经在这个问题上停留了一段时间,我迫切希望找到一个解决方案。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

So I have updated my code but now instead of retrieving the barcode at all i get null as the result and my toast message pops up. 因此,我已经更新了代码,但是现在我完全没有获取条形码,而是得到了null并弹出了我的吐司消息。 Anyone know what going on? 有人知道发生了什么吗? This happens no matter what button I press. 无论我按什么按钮,都会发生这种情况。

     private int scanFieldPosition = 0;

public void openScanner1 (View view) {
    scanFieldPosition = 1;
    IntentIntegrator scanIntegrator = new IntentIntegrator (this);
    scanIntegrator.initiateScan();

}

public void openScanner2 (View view) {
    scanFieldPosition = 2;
    IntentIntegrator scanIntegrator = new IntentIntegrator(this);
    scanIntegrator.initiateScan();

}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanningResult != null && scanFieldPosition == 1) {
        String scanContent = scanningResult.getContents();
        contentTxt.setText(scanContent);
    }

    if (scanningResult !=null && scanFieldPosition == 2) {
        String scanContent = scanningResult.getContents();
        contentTxt2.setText(scanContent);
    }


    else{
        Toast toast = Toast.makeText(getApplicationContext(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
    }

}

The problem was that I didnt have the variable static. 问题是我没有变量static。 I still get the toast message but my barcode it placed in the right edittext which is strange. 我仍然收到烤面包消息,但是我的条形码将其放在正确的edittext中,这很奇怪。 Anyways, my app works now thanks to all your help! 无论如何,由于您的所有帮助,我的应用现在可以正常工作了!

    private static int scanFieldPosition = 0;

You can have a static flag set for this, and assign 1 when to it when button1 is pressed and assign 2 to it when button2 is pressed. 您可以为此设置一个static标志,并在按下button1时为其分配1 ,并在按下button2时为其分配2 Then you could check for the flag in your onActivityResult() . 然后,您可以在onActivityResult()检查标志。 You could do something like this: 您可以执行以下操作:

public static int editTextNumber = 0;


public void openScanner1 (View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator (this);
scanIntegrator.initiateScan();
editTextNumber  = 1;
}


public void openScanner2 (View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
editTextNumber = 2;
}

And then in your onActivityResult() 然后在你的onActivityResult()

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

//Places barcode in the scan_content editext


Log.e("editTextNumber",""+ editTextNumber ); // check whats value here
if (scanningResult != null && editTextNumber == 1) { // checking for flag here
    String scanContent = scanningResult.getContents();
    contentTxt.setText(scanContent);
}

//places bar code in the scan_content2 edittext
else if (scanningResult !=null && editTextNumber == 2) {  // checking for flag also
    String scanContent = scanningResult.getContents();
    contentTxt2.setText(scanContent);
}


else{
    Toast toast = Toast.makeText(getApplicationContext(),
            "No scan data received!", Toast.LENGTH_SHORT);
    toast.show();
   }

}

If scanningResult is not null (in other words, if the condition of your if clause is true), you're setting the text for both EditTexts. 如果scanningResult不为null(换句话说,如果您的if子句的条件为true),那么您将设置两个EditText的文本。 You need to save some kind of information identifying the EditText to be filled when creating scan Intent, and retrieve that in your onActivityResult , for example : 您需要保存一些信息,以标识创建扫描Intent时要填充的EditText ,并在onActivityResult中进行检索,例如:

private int scanFieldPosition = 0;

public void openScanner1 (View view) {
    scanFieldPosition = 1;
    IntentIntegrator scanIntegrator = new IntentIntegrator (this);
    scanIntegrator.initiateScan();
}


public void openScanner2 (View view) {
    scanFieldPosition = 2;
    IntentIntegrator scanIntegrator = new IntentIntegrator(this);
    scanIntegrator.initiateScan();
}

Then check scanFieldPosition in onActivityResult to know which EditText to fill in. 然后检查onActivityResult scanFieldPosition以知道要填充哪个EditText

You don't show your construction of the scan intent within IntentIntegrator but I'm guessing you're using the same requestCode. 您没有在IntentIntegrator中显示扫描意图的构造,但是我猜您正在使用相同的requestCode。

Change your construction of the scan intent to have different requestCode's (probably by passing a value into the IntentIntegrator in addition to the context) and then when onActivityResult is called you will be able to check this to know which scan button was pressed and populate the relevant EditText. 将扫描意图的结构更改为具有不同的requestCode(可能是通过将上下文中的值传递给IntentIntegrator),然后在调用onActivityResult时,您可以检查该行为以了解按下了哪个扫描按钮并填充相关的EditText。

It's what the requestCode was designed for rather than holding a variable that could lead to state issues. 这就是requestCode的目的,而不是持有可能导致状态问题的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM