2017-07-08 86 views
0

我想將條形碼掃描儀的結果上傳到編輯文本,但它不顯示在編輯文本中。我可以知道問題是什麼,我該如何解決?無法讀取條形碼掃描儀的輸出

這是我的代碼

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(); 
} 

} 

回答

0

應實現ResultHandler在你的活動,覆蓋回調方法,並在那裏設置掃描的結果在您的EditText中,例如:

public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler { 
private ZXingScannerView mScannerView; 
private EditText editText; 

@Override 
public void onCreate(Bundle state) { 
    super.onCreate(state); 
    mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view 
    setContentView(mScannerView);    // Set the scanner view as the content view 

    EditText editText = (EditText)findViewById(R.id.myedittext); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results. 
    mScannerView.startCamera();   // Start camera on resume 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    mScannerView.stopCamera();   // Stop camera on pause 
} 

@Override 
public void handleResult(Result rawResult) { 
    // Do something with the result here 
    editText.setText(rawResult.getText()); 
    Log.v(TAG, rawResult.getText()); // Prints scan results 
    Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.) 

    // If you would like to resume scanning, call this method below: 
    mScannerView.resumeCameraPreview(this); 
} 
} 

希望它有幫助!