Build the “Hello World” Document Capture View

If you haven't installed the Dynamsoft Camera SDK in your project, please refer here. Once installed, you can add code to implement document capture function in your project.

1. Add the DcsView object in the activity_hello_world.xml file.

<?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=".HelloWorld">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

   // Add the following code:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:textSize="18sp"
            android:id="@+id/tv_title_id"
            android:text="Capture Image"
            android:gravity="center"
            android:background="#5EB7E4"
            android:textColor="@android:color/white"
            android:layout_alignParentTop="true"/>

        <com.dynamsoft.camerasdk.view.DcsView
            android:id="@+id/dcsview_id"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_above="@+id/tv_show_id"
            android:layout_below="@+id/tv_title_id"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:id="@+id/tv_show_id"
            android:text="show camera"
            android:gravity="center"
            android:background="#ff0f9d58"
            android:layout_alignParentBottom="true"/>
    </RelativeLayout>


</android.support.constraint.ConstraintLayout>

2. Open HelloWorld.java file
You can see the default HelloWorld.java as the following:

2.1 Set Dynamsoft Camera SDK license

try {
    DcsView.setLicense(getApplicationContext(),"your license number");
} catch (DcsValueNotValidException e) {
    e.printStackTrace();
}

2.2 Get the dcsView object.

dcsView = findViewById(R.id.dcsview_id);

2.3 Other settings

//Set the next view after the Capture button is clicked on the video view.
dcsView.getVideoView().setNextViewAfterCapture(DcsView.DVE_EDITORVIEW);
//Set the next view after the Cancel button is clicked on the video view.
dcsView.getVideoView().setNextViewAfterCancel(DcsView.DVE_IMAGEGALLERYVIEW);

//Set the next view after the OK button is clicked on the document edit view.
dcsView.getDocumentEditorView().setNextViewAfterOK(DcsView.DVE_IMAGEGALLERYVIEW);
//Set the next view after the Cancel button is clicked on the document edit view.
dcsView.getDocumentEditorView().setNextViewAfterCancel(DcsView.DVE_IMAGEGALLERYVIEW);

3. Review the completed code.


package com.dynamsoft.helloworld;

import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dynamsoft.camerasdk.exception.DcsCameraNotAuthorizedException;
import com.dynamsoft.camerasdk.exception.DcsValueNotValidException;
import com.dynamsoft.camerasdk.view.DcsView;
import com.dynamsoft.camerasdk.view.DcsViewListener;


public class HelloWorld extends AppCompatActivity implements DcsViewListener {

    private DcsView dcsView;
    private TextView btnShowCamera;
    private TextView tvTitle;
    private static final int CAMERA_OK = 10;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_world);
        try {
            DcsView.setLicense(getApplicationContext(),"your license number");
        } catch (DcsValueNotValidException e) {
            e.printStackTrace();
        }
        tvTitle = findViewById(R.id.tv_title_id);
        dcsView = findViewById(R.id.dcsview_id);
        //Close the DcsView logs.
        DcsView.setLogLevel(DcsView.DLLE_OFF);

        //Set the DcsView listener.
        dcsView.setListener(this);
        //Set the current view.
        dcsView.setCurrentView(DcsView.DVE_IMAGEGALLERYVIEW);

        //Set the next view after the Capture button is clicked on the video view.
        dcsView.getVideoView().setNextViewAfterCapture(DcsView.DVE_EDITORVIEW);
        //Set the next view after the Cancel button is clicked on the video view.
        dcsView.getVideoView().setNextViewAfterCancel(DcsView.DVE_IMAGEGALLERYVIEW);

        //Set the next view after the OK button is clicked on the document edit view.
        dcsView.getDocumentEditorView().setNextViewAfterOK(DcsView.DVE_IMAGEGALLERYVIEW);
        //Set the next view after the Cancel button is clicked on the document edit view.
        dcsView.getDocumentEditorView().setNextViewAfterCancel(DcsView.DVE_IMAGEGALLERYVIEW);

        btnShowCamera = findViewById(R.id.tv_show_id);
        btnShowCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dcsView.setCurrentView(DcsView.DVE_VIDEOVIEW);
            }
        });
        requestPermissions();
    }


    @Override
    protected void onResume() {
        super.onResume();
        //Please call preview() here if the current view is dcsVideoView.
        if(dcsView.getCurrentView() == DcsView.DVE_VIDEOVIEW){
            try {
                dcsView.getVideoView().preview();
            } catch (DcsCameraNotAuthorizedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        //Please call stopPreview() here.
        dcsView.getVideoView().stopPreview();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Please call destroyCamera() here.
        dcsView.getVideoView().destroyCamera();
    }


    @Override
    public void onCurrentViewChanged(DcsView dcsView, int i, int i1) {
        if(i1 == DcsView.DVE_IMAGEGALLERYVIEW){
            btnShowCamera.setVisibility(View.VISIBLE);
            tvTitle.setVisibility(View.VISIBLE);
        }else{
            btnShowCamera.setVisibility(View.GONE);
            tvTitle.setVisibility(View.GONE);
        }
    }

    //For API 23 or higher, you need to request the camera permission.
    private void requestPermissions(){
        if (Build.VERSION.SDK_INT>22){
            try {
                if (ContextCompat.checkSelfPermission(HelloWorld.this,"android.permission.WRITE_EXTERNAL_STORAGE")!= PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(HelloWorld.this, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
                }
                if (ContextCompat.checkSelfPermission(HelloWorld.this,android.Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(HelloWorld.this,new String[]{android.Manifest.permission.CAMERA},CAMERA_OK);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            // do nothing
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        try {
            //License needs to be checked after the permission is requested.
            DcsView.setLicense(getApplicationContext(),"your license number");
        } catch (DcsValueNotValidException e) {
            e.printStackTrace();
        }
    }
}

4. See it in action

If you launch the Hello World project, it should look like this:

After clicking "ALLOW", you can click "show camera" to detect a document.

Once the capture button is clicked, the captured document will show up in the built-in editor view:

After editing, you can click "OK" to see the cropped document in the gallery view:

As you can see, there are many other sample applications (source code provided) available for you to explore some of the many features of the Dynamsoft Camera SDK. Enjoy!

Is this page helpful?