Android StudioでZXingを使う。
タイトル通り、です。
Android StudioでZXingライブラリなるものを使ってみよう!ってことです。
やることとしては(準備って意味では)
- build.gradle(app) にrepositoriesとdependenciesを書き足す
- AndroidManifest.xmlにpermission追加
これだけでした。
build.gradle(app)に追加
apply plugin: 'com.android.application' android { //略 } buildTypes { release { //略 } } } //追加-↓ repositories { mavenCentral(); maven{ url "http://dl.bintray.com/journeyapps/maven" } } //-----↑ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) //4行追加-↓ compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar' compile 'com.journeyapps:zxing-android-legacy:2.0.1@aar' compile 'com.journeyapps:zxing-android-integration:2.0.1@aar' compile 'com.google.zxing:core:3.0.1' }
そしてAndroid Studioのうえの方にある、Sync now !!! をする。
何にもエラーが出なければ成功。
(あくまでZXingいれるところまでは)
参考にしたサイトは
https://github.com/journeyapps/zxing-android-embedded
ココのREADME.mdってところ
AndroidManifest.xmlにpermission追加
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.FLASHLIGHT"/>
使い方も書いてあるから早速起動してみる。
mBtnCamera.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ IntentIntegrator integrator = new IntentIntegrator(SearchActivity.this); integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES); integrator.setPrompt("Scan a barcode"); integrator.setResultDisplayDuration(0); integrator.setWide(); integrator.setCameraId(0); integrator.initiateScan(); } });
これでカメラ起動して真ん中に赤い線が入ってる状態。
(何とか横向きにできないものかな、、)
まとめ
思っていたよりかなり優しかった。
いつもこういうの手こずるタイプだから嬉しい。^^
ではではー。