无法读取条形码扫描仪的输出

时间:2017-07-08 01:01:10

标签: android

我想将条形码扫描器的结果上传到编辑文本,但它不会显示在编辑文本中。我可以知道问题是什么,我该如何解决?

这是我的代码

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class ScannerActivity extends AppCompatActivity {
private ZXingScannerView scannerView;
private EditText ScanBarcode;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scanner);

    ScanBarcode = (EditText)findViewById(R.id.editscanbar);
}

public void scanCode(View view){
    scannerView = new ZXingScannerView(this);
    scannerView.setResultHandler(new ZXingScannerResultHandler());

    setContentView(scannerView);
    scannerView.startCamera();
}
@Override
public void onPause(){
    super.onPause();
    scannerView.stopCamera();
}

class ZXingScannerResultHandler implements ZXingScannerView.ResultHandler{
            @Override
            public void handleResult(Result result){
                String resultCode = result.getText().toString();
                ScanBarcode.setText(resultCode);
                Toast.makeText(ScannerActivity.this, resultCode, 
Toast.LENGTH_SHORT).show();

                setContentView(R.layout.activity_scanner);
                scannerView.stopCamera();

    }
}
}

我已经改变了它,编辑文本中仍然没有任何内容

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class ScannerActivity extends AppCompatActivity implements 
ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
private EditText ScanBarcode;
@Override
public void onCreate(Bundle State) {
    super.onCreate(State);
    mScannerView = new ZXingScannerView(this);
    setContentView(R.layout.activity_scanner);

    ScanBarcode = (EditText)findViewById(R.id.editscanbar);
}

public void scanCode(View view){
    mScannerView.setResultHandler(this);

    setContentView(mScannerView);
    mScannerView.startCamera();
}
@Override
public void onResume(){
    super.onResume();
    mScannerView.setResultHandler(this);
    mScannerView.startCamera();
}

@Override
public void onPause(){
    super.onPause();
    mScannerView.stopCamera();
}
@Override
public void handleResult(Result rawResult){
    ScanBarcode.setText(rawResult.getText());

    setContentView(R.layout.activity_scanner);
    mScannerView.stopCamera();
}

}

1 个答案:

答案 0 :(得分:0)

你应该在Activity中实现ResultHandler,覆盖回调方法并在EditText中设置扫描结果,例如:

+ data.course_id

希望它有所帮助!