如何使用zxing从qr扫描仪获取数据



我正在开发一个应用程序,其中一部分涉及QR扫描。经过大量的研究,我成功地开发了一款独立的扫描应用程序。当用户打开扫描仪并扫描特定的二维码时,他会得到一些值,例如URL。现在我想把扫描时获得的数据存储到我的安卓代码中。有人能帮我怎么做吗?

据我所见,我需要使用Zxing的捕获活动类。然而,我不确定到底需要做什么。我在网上读到的所有博客都指示我使用Intent来称之为条形码扫描。然而,我的应用程序的目的不仅仅是扫描产品。我需要存储扫描产品的信息,然后将其用于其他目的。

请帮帮我。

谢谢,Amey

这是Zxing的代码。。这是处理所有扫描的主要活动。根据我在网上阅读中学到的知识,我需要捕捉扫描条形码时返回的数据。。

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
if (resultCode == RESULT_OK) { 
if (requestCode == HISTORY_REQUEST_CODE) { 
int itemNumber = intent.getIntExtra(Intents.History.ITEM_NUMBER, -1); 
if (itemNumber >= 0) { 
HistoryItem historyItem = historyManager.buildHistoryItem(itemNumber);             
decodeOrStoreSavedBitmap(null, historyItem.getResult()); 
} 
}
} 
} 

if (Intents.Scan.ACTION.equals(action)) { // Scan the formats the intent requested, and   return the result to the calling activity 
source = IntentSource.NATIVE_APP_INTENT; 
decodeFormats = DecodeFormatManager.parseDecodeFormats(intent); 
if (intent.hasExtra(Intents.Scan.WIDTH) && intent.hasExtra(Intents.Scan.HEIGHT)) {
int width = intent.getIntExtra(Intents.Scan.WIDTH, 0); 
int height = intent.getIntExtra(Intents.Scan.HEIGHT, 0); 
if (width > 0 && height > 0) { 
cameraManager.setManualFramingRect(width, height); 
} 
}
}

通过intent使用它是最简单的方法,并且可以存储扫描结果,您只需要自己完成即可。它的工作原理都在Zxing的文档中http://code.google.com/p/zxing/wiki/ScanningViaIntent

从上述链接

首先添加代码以调用Intent:

IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();

其次,将其添加到您的"活动"中以处理结果:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
// handle scan result
//here is where you would get the data from the scanResult
//and store locally by writing to a file or however you 
//intend to store it
}
// else continue with any other code you need in the method
}

我没有使用过这个版本的Zxing,我使用的是至少两年前的版本,但过程是相同的

1-通过Intent启动Zxing2-扫描二维码3-在onActivityResult中从扫描中检索信息。

你好,我终于找到了这个问题的答案。这并没有我想象的那么难(因为Zxing的代码是Zxing团队写的,而不是我写的……无论如何……)

因此,如果你想将qr扫描仪(Zxing提供)捕获的数据存储在你的android代码中(无论出于什么目的…在我的情况下,我想将这些数据发送到web服务器…无论如何…),那么你只需要修改以下功能。。这是扫描活动的结果。。

public void handleDecode(Result rawResult, Bitmap barcode) {
inactivityTimer.onActivity();
lastResult = rawResult;
Log.d("last result", "checking if raw result is what i expect");
System.out.println(lastResult);
ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult);
historyManager.addHistoryItem(rawResult, resultHandler);
}

我添加了日志和打印语句来检查我是否得到了正确的结果。是的,它确实给了我一个正确的答案。。您可以在CaptureActivity类中找到它。

@特里格斯:谢谢你的帮助!你确实让我走上了正轨:-)

我偶然发现您的线程也在搜索这组代码的帮助。在我的情况下,我不得不将信息发送回主应用程序(ZXing是我项目中的一个库——我知道,我已经和我的客户谈过了,但由于业务需求,我们无法使用Intents)。

如果您需要将信息从另一个项目传递回另一个活动,这是我的解决方案。

项目A是主要的应用程序,而ZXing项目将被称为这样的应用程序。

在ZXing的CaptureActivity.java:中编辑handleDecode()

public void handleDecode(Result rawResult, Bitmap barcode) {
inactivityTimer.onActivity();
lastResult = rawResult;
ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult); 
if (source == IntentSource.NATIVE_APP_INTENT) {
Intent resultIntent = new Intent();
resultIntent.putExtra("result", rawResult.toString());
setResult(Activity.RESULT_OK, resultIntent);
finish();
} 
}//end handleDecode()

在您的项目A的活动中,调用CaptureActivity,

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
NullQRCodeDialogFragment dialog = new NullQRCodeDialogFragment();
String result = "";
if (resultCode == RESULT_OK) {
result = intent.getStringExtra("result");
if (result.equals(null)){
//TODO
} else {
//TODO
}
}//end onActivityResult

希望这能有所帮助!这是我在这里的第一篇文章,我很高兴我能贡献=)

这是我正在使用的解决方案。它对我来说运行良好。

Intent intent = new Intent(SelectOptionActivity.this, CaptureActivity.class);
intent.putExtra("SCAN_MODE", "ONE_D_MODE");
intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR,EAN_13,EAN_8,UPC_A,QR_CODE");
intent.setAction(Intents.Scan.ACTION);
startActivityForResult(intent, 1);

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1 && resultCode == RESULT_OK) {
final String contents = intent.getStringExtra(Intents.Scan.RESULT);
final String formatName = intent.getStringExtra(Intents.Scan.RESULT_FORMAT);
}
}

最新更新