嗨,我知道如何导入一个最小的Android库项目来扫描二维码,但扫描完二维码后我想得到二维码得到的结果(例如网址),但我真的不知道如何检索结果,这就是为什么我要求你的帮助.
我正在尝试使用它:https://github.com/embarkmobile/zxing-android-minimal#custom-layout
我用它来启动扫描仪:
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureLayout(R.layout.custom_layout);
integrator.initiateScan();
Run Code Online (Sandbox Code Playgroud)
在此先感谢我为webview做了这个
wb = (WebView)findViewById(R.id.webView2);
wb.loadUrl(re);
Run Code Online (Sandbox Code Playgroud)
bji*_*ang 28
First
,您的Activity必须实现该方法Activity.onActivityResult(int, int, Intent)
并包含一行代码,如下所示:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
// handle scan result
}
// else continue with any other code you need in the method
...
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处理扫描结果.
Second
,只需调用此方法以响应某个地方的用户操作即可开始扫描过程:
IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();
Run Code Online (Sandbox Code Playgroud)
请注意,initiateScan()
如果提示用户下载应用程序,则返回一个非空的AlertDialog.这使得调用应用程序可以管理对话框.特别是,理想情况下,如果对话框在其Activity.onPause()
方法中仍处于活动状态,则会解除对话框.
您可以使用setTitle(String)
自定义此下载提示对话框的标题(或使用setTitleByID(int)按字符串资源ID设置标题.)同样,可以更改提示消息和是/否按钮标签.
Finally
,您可以使用addExtra(String, Object)
向用于调用扫描程序的Intent添加更多参数.这可用于设置此简化API未直接公开的其他选项.
默认情况下,这将只允许已知响应此意图的应用程序正确执行此操作.可以使用setTargetApplications(List)设置允许响应的应用程序.例如,设置为TARGET_BARCODE_SCANNER_ONLY
仅定位条形码扫描仪应用程序本身.
有关详细信息,请参阅此处.
示例代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class MainActivity extends Activity {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Scanner
mButton = (Button) findViewById(R.id.assistant_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.initiateScan();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
String re = scanResult.getContents();
Log.d("code", re);
}
// else continue with any other code you need in the method
}
}
Run Code Online (Sandbox Code Playgroud)
xml中的一个按钮,然后单击它,扫描条形码,它将返回条形码的原始内容.
归档时间: |
|
查看次数: |
35409 次 |
最近记录: |