我正在尝试在我的Android应用程序中实现条形码扫描程序活动,但我听说如果不要求用户安装ZXing条形码扫描程序就不可能这样做。
所以我所做的是我使用ZXing集成器类来启动扫描,这是我的代码。但是我无法在两个文本视图中显示结果。我必须提到扫描已经开始,结果会显示在条形码扫描器窗口本身,但是ScanCodeActivity布局上没有任何内容,它有两个文本视图。
我也想知道是否可以将结果从eweb返回到我的文本视图中。
我使用此链接作为参考: http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader--mobile-17162
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class ScanCodeActivity extends Activity {
private Button scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scancodelayout);
scan= (Button)findViewById(R.id.scan_button);
Button scanBtn = (Button)findViewById(R.id.scan_button);
final TextView formatTxt = (TextView)findViewById(R.id.scan_format);
final TextView contentTxt = (TextView)findViewById(R.id.scan_content);
scanBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator scanIntegrator = new IntentIntegrator(ScanCodeActivity.this);
scanIntegrator.initiateScan();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
if (scanningResult != null) {
final TextView formatTxt = (TextView)findViewById(R.id.scan_format);
final TextView contentTxt = (TextView)findViewById(R.id.scan_content);
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
Toast toast = Toast.makeText(this, "Content:" + scanContent + " Format:" + scanFormat , Toast.LENGTH_LONG);
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
//we have a result
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Log.i("App","Scan unsuccessful");
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:3)
从您的if (requestCode == 0)
方法中移除onActivityResult()
支票,类似于图书馆wiki page中的示例。
注意:您正在检查错误的requestCode
。该库使用0x0000c0de
(请参阅the source)而不是0
。