Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.codewaves:code-scanner:1.7.6'
}
dependencies {
implementation("com.github.codewaves:code-scanner:1.7.6")
}
<dependency>
<groupId>com.github.codewaves</groupId>
<artifactId>code-scanner</artifactId>
<version>1.7.6</version>
</dependency>
libraryDependencies += "com.github.codewaves" % "code-scanner" % "1.7.6"
:dependencies [[com.github.codewaves/code-scanner "1.7.6"]]
Code scanner library for Android, based on ZXing
| 1D product | 1D industrial | 2D | ---------- | ------------- | -------------- | UPC-A | Code 39 | QR Code | UPC-E | Code 93 | Data Matrix | EAN-8 | Code 128 | Aztec | EAN-13 | Codabar | PDF 417 | | ITF | MaxiCode | | RSS-14 | | | RSS-Expanded |
Add dependency:
dependencies {
implementation 'com.budiyev.android:code-scanner:1.7.3'
}
Add camera permission to AndroidManifest.xml (Don't forget about dynamic permissions on API >= 23):
<uses-permission android:name="android.permission.CAMERA"/>
Define a view in your layout file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.budiyev.android.codescanner.CodeScannerView
android:id="@+id/scanner_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:frameColor="@android:color/white"
app:frameCornersSize="50dp"
app:frameWidth="2dp"
app:maskColor="#77000000"
app:squareFrame="true"
app:autoFocusButtonColor="@android:color/white"
app:flashButtonColor="@android:color/white"
app:autoFocusButtonVisible="true"
app:flashButtonVisible="true"/>
</FrameLayout>
And add following code to your activity:
public class MainActivity extends AppCompatActivity {
private CodeScanner mCodeScanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CodeScannerView scannerView = findViewById(R.id.scanner_view);
// Use builder
mCodeScanner = CodeScanner.builder()
/*camera can be specified by calling .camera(cameraId),
first back-facing camera on the device by default*/
/*code formats*/
.formats(CodeScanner.ALL_FORMATS)/*List<BarcodeFormat>*/
/*or .formats(BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX, ...)*/
/*or .format(BarcodeFormat.QR_CODE) - only one format*/
/*auto focus*/
.autoFocus(true).autoFocusMode(AutoFocusMode.SAFE).autoFocusInterval(2000L)
/*flash*/
.flash(false)
/*decode callback*/
.onDecoded(new DecodeCallback() {
@Override
public void onDecoded(@NonNull final Result result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, result.getText(),
Toast.LENGTH_LONG).show();
}
});
}
})
/*error callback*/
.onError(new ErrorCallback() {
@Override
public void onError(@NonNull final Exception error) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, error.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
}).build(this, scannerView);
// Or use constructor to create scanner with default parameters
// All parameters can be changed after scanner created
// mCodeScanner = new CodeScanner(this, scannerView);
// mCodeScanner.setDecodeCallback(...);
scannerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCodeScanner.startPreview();
}
});
}
@Override
protected void onResume() {
super.onResume();
mCodeScanner.startPreview();
}
@Override
protected void onPause() {
mCodeScanner.releaseResources();
super.onPause();
}
}