二次元コード読み取り機能のAndroid実装(a) - ZXingプラグインアクセス
最新の更新情報
2019年の最新版です。 QRコード読み取り機能のAndroid実装(V) - ラッピングとZXing掃出しライブラリへのアクセス アクセスや修正がより便利になりました。
はじめに
AndroidでQRコードを読み取る実装については、様々な情報があります。比較検討した結果、前任者のZXingを改造して直接アクセスする方法を選んだので、全体の流れを紹介するためにこのデモを作りました。
(最新版) この記事で説明したアクセス方法は、初めて開発する人にとっては少し難しいので、よりアクセスしやすいライブラリ版のコードスキャン・ライブラリを作りましたので、細かいことを気にしない人は、直接 QRコード読み取り機能のAndroid実装(V) - ZXing sweepコードライブラリのパッケージングとアクセス方法 .
効果プレビュー
まずは写真で効果を確認しましょう(シミュレーターにはカメラがないので、録画がうまくいかないので、見るだけにしてください)。
統合のステップ
1. このプロジェクトのデモから com.google.zxing5 パッケージをコピーして、自分のプロジェクトに導入してください。
2. レイアウト activity_scanner.xml と toolbar_scanner.xml をこのプロジェクトのデモからコピーしてください。
3. 3. リソースディレクトリをこのプロジェクトにコピーしてください。
4. 4. ファイルの内容 attrs.xml/colors.xml/ids.xml の3つのファイルをコピーまたはマージする。
5、build.gradle ファイルでリファレンスを追加する
compile 'com.google.zxing:core:3.3.0'
6. Rファイルの参照パスを変更する
Rファイルの参照先で、プロジェクトのRを参照して、以下の4つのファイルを修正します。
com.google.zxing.activity.CaptureActivity
com.google.zxing.decoding.CaptureActivityHandler
com.google.zxing.decoding.DecodeHandler
com.google.zxing.view.ViewfinderView
これで統合の部分は終わりです。ここでは、機能をどのように実装するかを見ていきます。
パーミッションの設定
AndroidManifest.xmlにパーミッションリクエストコードを追加します。
After Android 6.0 Camera needs to include dynamic permission request code, which is given in the implementation section below.
Function implementation
After completing the above integration, the sweep function can be implemented by calling CaptureActivity.
MainActivity source code section.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnQrCode; // Sweep code
TextView tvResult; // result
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btnQrCode = (Button) findViewById(R.id.btn_qrcode);
btnQrCode.setOnClickListener(this);
tvResult = (TextView) findViewById(R.id.txt_result);
}
// Start the code sweep
private void startQrCode() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ! = PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
Toast.makeText(MainActivity.this, "Please go to the permission center to turn on camera access for this app", Toast.LENGTH_LONG).show();
}
// Request Permissions
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, Constant.REQ_PERM_CAMERA);
return;
}
// QR code scanning
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(intent, Constant.REQ_QR_CODE);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_qrcode:
startQrCode();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//scan result callback
if (requestCode == Constant.REQ_QR_CODE && resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
String scanResult = bundle.getString(Constant.INTENT_EXTRA_KEY_QR_SCAN);
// display the scanned information
tvResult.setText(scanResult);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case Constant.REQ_PERM_CAMERA:
// Camera permission request
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Get authorization
startQrCode();
} else {
// disabled
Toast.makeText(MainActivity.this, "Please go to the permission center to turn on camera access for this app", Toast.LENGTH_LONG).show();
}
break;
}
}
}
Before launching CaptureActivity, you need to request dynamic permissions
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ! = PackageManager.PERMISSION_GRANTED) {
// Request permission
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, Constant.REQ_PERM_CAMERA);
return;
}
The application result processing is in the onRequestPermissionsResult method, which can be referenced in the comments.
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case Constant.REQ_PERM_CAMERA:
// Camera permission request
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Get authorization
startQrCode();
} else {
// disabled
Toast.makeText(MainActivity.this, "Please go to the permission center to turn on camera access for this app", Toast.LENGTH_LONG).show();
}
break;
}
}
Get scan results
zxing's CaptureActivity has implemented the scan and result return perfectly, we just need to handle the return result on the caller side.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//scan result callback
if (requestCode == Constant.REQ_QR_CODE && resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
String scanResult = bundle.getString(Constant.INTENT_EXTRA_KEY_QR_SCAN);
// display the scanned information
tvResult.setText(scanResult);
}
}
A TextView is used here to render the result of the sweep.
Conclusion
This is the end of the Android sweep function, and can be used directly in the project by calling the camera to sweep the code in real time and process the result. Thanks to the open source authors for making it so easy to integrate sweeping.
References
http://www.jianshu.com/p/e80a85b17920
Source code download
Upload the source code to csdn, go download it if you need it. (old version)
http://download.csdn.net/detail/ahuyangdong/9915661
Github project address (keep it up to date), which has largely solved the adaptation problem for album selection.
https://github.com/ahuyangdong/QrCodeDemo4
関連
-
Androidアップグレード/ブラシ戦略(GoogleネイティブシステムPixel)
-
AndroidStdioのインストール時に「メインクラスが見つからない com/intelligi/idea/Main」というエラーが発生する。
-
Android Studioプロジェクトをインポートすると、無効なgradle jdkコンフィギュレーションが見つかったと報告される
-
エラー:SSLピアが正しくシャットダウンされない
-
エラーの解決方法 URIがandroid studioのxmlファイルに登録されていない。
-
テスト、Eclipseは "エラーが発生しました、詳細はエラーログを参照してください。 java.lang.NullPointerException" を報告します。
-
-bash : gradlew コマンドが見つかりません。
-
Android ビューの階層構造
-
Android studioでTextViewのフォントを変更する2つの方法(例:模造歌、公式スクリプト)。
-
AndroidManifest.xml ファイルが見つからない 解決策
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン