Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy-budiyev committed May 24, 2022
1 parent 4b57bb2 commit aef2cdc
Showing 1 changed file with 34 additions and 63 deletions.
97 changes: 34 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Code%20Scanner-blue.svg?style=flat)](https://android-arsenal.com/details/1/6095)
[![API](https://img.shields.io/badge/API-19%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=19)

Code scanner library for [Android](https://developer.android.com), based
on [ZXing](https://github.com/zxing/zxing)
Code scanner library for [Android](https://developer.android.com), based on [ZXing](https://github.com/zxing/zxing)

### Features

* Auto focus and flash light control
* Portrait and landscape screen orientations
* Back and front facing cameras
Expand All @@ -17,7 +15,6 @@ on [ZXing](https://github.com/zxing/zxing)
* Touch focus

### Supported formats

| 1D product | 1D industrial | 2D
| ---------- | ------------- | --------------
| UPC-A | Code 39 | QR Code
Expand Down Expand Up @@ -62,25 +59,29 @@ dependencies {
}
```

Add camera permission and hardware feature to AndroidManifest.xml (Don't forget about dynamic
permissions on API >= 23):
Add camera permission and hardware feature to AndroidManifest.xml (Don't forget about dynamic permissions on API >= 23):

```xml

<uses-permission android:name="android.permission.CAMERA"/>

<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature
android:name="android.hardware.camera"
android:required="false"/>
```

Define a view in your layout file:

```xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"/>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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"/>
</FrameLayout>
```

Expand Down Expand Up @@ -117,50 +118,39 @@ flashButtonPosition
And add following code to your activity:

Kotlin

```kotlin
class MainActivity: AppCompatActivity() {
class MainActivity : AppCompatActivity() {
private lateinit var codeScanner: CodeScanner

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val scannerView = findViewById<CodeScannerView>(R.id.scanner_view)

codeScanner = CodeScanner(
this,
scannerView
)


codeScanner = CodeScanner(this, scannerView)

// Parameters (default values)
codeScanner.camera = CodeScanner.CAMERA_BACK // or CAMERA_FRONT or specific camera id
codeScanner.formats = CodeScanner.ALL_FORMATS // list of type BarcodeFormat,
// ex. listOf(BarcodeFormat.QR_CODE)
// ex. listOf(BarcodeFormat.QR_CODE)
codeScanner.autoFocusMode = AutoFocusMode.SAFE // or CONTINUOUS
codeScanner.scanMode = ScanMode.SINGLE // or CONTINUOUS or PREVIEW
codeScanner.isAutoFocusEnabled = true // Whether to enable auto focus or not
codeScanner.isFlashEnabled = false // Whether to enable flash or not

// Callbacks
codeScanner.decodeCallback = DecodeCallback {
runOnUiThread {
Toast.makeText(
this,
"Scan result: ${it.text}",
Toast.LENGTH_LONG
).show()
Toast.makeText(this, "Scan result: ${it.text}", Toast.LENGTH_LONG).show()
}
}
codeScanner.errorCallback = ErrorCallback { // or ErrorCallback.SUPPRESS
runOnUiThread {
Toast.makeText(
this,
"Camera initialization error: ${it.message}",
Toast.LENGTH_LONG
).show()
Toast.makeText(this, "Camera initialization error: ${it.message}",
Toast.LENGTH_LONG).show()
}
}

scannerView.setOnClickListener {
codeScanner.startPreview()
}
Expand Down Expand Up @@ -196,8 +186,7 @@ public class MainActivity extends AppCompatActivity {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, result.getText(), Toast.LENGTH_SHORT)
.show();
Toast.makeText(MainActivity.this, result.getText(), Toast.LENGTH_SHORT).show();
}
});
}
Expand All @@ -207,7 +196,7 @@ public class MainActivity extends AppCompatActivity {
public void onClick(View view) {
mCodeScanner.startPreview();
}
});
});
}

@Override
Expand All @@ -229,38 +218,21 @@ or fragment:
Kotlin

```kotlin
class MainFragment: Fragment() {
class MainFragment : Fragment() {
private lateinit var codeScanner: CodeScanner

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(
R.layout.fragment_main,
container,
false
)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.fragment_main, container, false)
}

override fun onViewCreated(
view: View,
savedInstanceState: Bundle?
) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val scannerView = view.findViewById<CodeScannerView>(R.id.scanner_view)
val activity = requireActivity()
codeScanner = CodeScanner(
activity,
scannerView
)
codeScanner = CodeScanner(activity, scannerView)
codeScanner.decodeCallback = DecodeCallback {
activity.runOnUiThread {
Toast.makeText(
activity,
it.text,
Toast.LENGTH_LONG
).show()
Toast.makeText(activity, it.text, Toast.LENGTH_LONG).show()
}
}
scannerView.setOnClickListener {
Expand Down Expand Up @@ -310,7 +282,7 @@ public class MainFragment extends Fragment {
public void onClick(View view) {
mCodeScanner.startPreview();
}
});
});
return root;
}

Expand All @@ -329,5 +301,4 @@ public class MainFragment extends Fragment {
```

### Preview

![Preview screenshot](https://raw.githubusercontent.com/yuriy-budiyev/code-scanner/master/images/code_scanner_preview.png)

0 comments on commit aef2cdc

Please sign in to comment.