The Preview of Dynamsoft Android Camera SDK
Dynamsoft Android Camera SDK is coming soon. In this article, let’s preview the SDK and learn how to write an Android document scanning app with a few lines of Kotlin code.
Features
- Automatic border detection
- Trimming and perspective correction
- Image enhancement
- iOS image processing
- Offline
- I/O and PDF
- Customizable UI
- Upload to Web Server
Building Mobile Document Scanner with Android Camera SDK
License
Register a new Dynamsoft account.
Click DCS Mobile License Center and fill in the information.
Once the step is done, you will get a valid license. The trial license is free for 30 days.
Sample Code
Create a new project including Kotlin support. If you prefer Java, uncheck the Kotlin option.
Copy dynamsoftcamerasdk1.0.0.aar to app/libs folder.
Include aar files in app/build.gradle:
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
Press Ctrl+SHIFT+ALT+S to open project settings. Click app > Dependencies to add dependent libraries: recyclerview and exifinterface.
Note: without the two libraries, Dynamsoft camera SDK cannot work.
Open activity_main.xml to add a DCS view and a button.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.dynamsoft.camerasdk.view.DcsView
android:id="@+id/dcsview_id"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_above="@+id/bt_show_id" />
<Button
android:id="@+id/bt_show_id"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/ic_camera"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
Open MainActivity.kt. In onCreate() function, initialize DCS with a valid license.
try {
DcsView.setLicense(applicationContext, "your license number")
} catch (e: DcsValueNotValidException) {
e.printStackTrace()
}
Dynamsoft Camera SDK provides three kinds of widget including image gallery view, image editor view, and camera view. You can switch views as follows:
dcsView.currentView = DcsView.DVE_IMAGEGALLERYVIEW
dcsView.currentView = DcsView.DVE_EDITORVIEW
dcsView.currentView = DcsView.DVE_VIDEOVIEW
The widgets have done everything for you. Here is a screenshot of camera view:
You can quickly set the touch event:
dcsView.videoView.nextViewAfterCancel = DcsView.DVE_IMAGEGALLERYVIEW
dcsView.videoView.nextViewAfterCapture = DcsView.DVE_EDITORVIEW
After taking a picture, navigate to the image editor: The last thing, do not forget to request camera and storage permissions programmatically.
private fun requestPermissions() {
if (Build.VERSION.SDK_INT > 22) {
try {
if (ContextCompat.checkSelfPermission(this@MainActivity, "android.permission.WRITE_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this@MainActivity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE)
}
if (ContextCompat.checkSelfPermission(this@MainActivity, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(android.Manifest.permission.CAMERA), CAMERA_OK)
}
} catch (e: Exception) {
e.printStackTrace()
}
} else {
// do nothing
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
try {
DcsView.setLicense(applicationContext, "your license number")
} catch (e: DcsValueNotValidException) {
e.printStackTrace()
}
}
Now you can build and run the app: