1+ package com .extrieve .quickcapture .docappjava ;
2+
3+ import androidx .activity .result .ActivityResult ;
4+ import androidx .activity .result .ActivityResultLauncher ;
5+ import androidx .activity .result .contract .ActivityResultContracts ;
6+ import androidx .annotation .NonNull ;
7+ import androidx .appcompat .app .AppCompatActivity ;
8+ import androidx .core .app .ActivityCompat ;
9+ import androidx .core .content .ContextCompat ;
10+
11+ import android .app .Activity ;
12+ import android .content .ContextWrapper ;
13+ import android .content .Intent ;
14+ import android .content .pm .PackageManager ;
15+ import android .graphics .Bitmap ;
16+ import android .graphics .BitmapFactory ;
17+ import android .net .Uri ;
18+ import android .os .Build ;
19+ import android .os .Bundle ;
20+ import android .os .Environment ;
21+ import android .provider .Settings ;
22+ import android .util .Log ;
23+ import android .widget .ImageView ;
24+ import android .widget .Toast ;
25+
26+ /*DEV_HELP : Import SDK from QuickCapture lib with : com.extrieve.quickcapture.sdk.**/
27+ import com .extrieve .quickcapture .sdk .*;
28+ import com .extrieve .quickcapture .sdk .CameraSupport .CamConfigClass ;
29+
30+ import java .io .File ;
31+ import java .io .IOException ;
32+ import java .util .ArrayList ;
33+
34+ public class MainActivity extends AppCompatActivity {
35+
36+ private static final int REQUEST_CODE_PERMISSIONS = 1001 ;
37+ /*DEV_HELP : Declare variables for the classes from SDK*/
38+ CameraHelper CameraHelper = null ;
39+ ImgHelper ImageHelper = null ;
40+
41+ /*DEV_HELP : Declare variables for ActivityResultLauncher to accept result from camera activity
42+ * As CameraHelper is an activity based class*/
43+ private ActivityResultLauncher <Intent > captureActivityResultLauncher ;
44+
45+
46+ private final String [] REQUIRED_PERMISSIONS =
47+ new String []{"android.permission.CAMERA" };
48+ private static final int REQUEST_CODE_FILE_RETURN = 1004 ;
49+
50+ ArrayList <String > FileCollection ;
51+
52+ @ Override
53+ protected void onCreate (Bundle savedInstanceState ) {
54+ super .onCreate (savedInstanceState );
55+ setContentView (R .layout .activity_main );
56+ CheckAndPromptPermissions ();
57+
58+ /*DEV_HELP : Initialise object of ImgHelper class.Pass the current activity context*/
59+ ImageHelper = new ImgHelper (this );
60+ /*DEV_HELP : Initialise object CameraHelper*/
61+ CameraHelper = new CameraHelper ();
62+
63+ /*DEV_HELP : assign registerForActivityResult for getting result from CameraHelper*/
64+ captureActivityResultLauncher = registerForActivityResult (
65+ new ActivityResultContracts .StartActivityForResult (),
66+ result -> handleCaptureActivityResult (result ));
67+
68+ /*DEV_HELP : Capture Document with SDK Button click handler*/
69+ findViewById (R .id .getPictureButton ).setOnClickListener (v -> {
70+ SetConfig ();
71+ OpenCameraActivity ();
72+ });
73+ }
74+
75+ /*DEV_HELP : Basic permission for App/SDK to work*/
76+ private void CheckAndPromptPermissions () {
77+ for (String permission : REQUIRED_PERMISSIONS ) {
78+ if (ContextCompat .checkSelfPermission (this , permission ) != PackageManager .PERMISSION_GRANTED ) {
79+ ActivityCompat .requestPermissions (this , REQUIRED_PERMISSIONS , REQUEST_CODE_PERMISSIONS );
80+ }
81+ }
82+ }
83+
84+ @ Override
85+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
86+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
87+ if (requestCode == REQUEST_CODE_PERMISSIONS ) {
88+ for (String permission : REQUIRED_PERMISSIONS ) {
89+ if (ContextCompat .checkSelfPermission (this , permission ) != PackageManager .PERMISSION_GRANTED ) {
90+ Toast .makeText (this , "Permissions not granted by the user." , Toast .LENGTH_SHORT ).show ();
91+ this .finish ();
92+ }
93+ //Got permission
94+ }
95+ }
96+ }
97+
98+ /*DEV_HELP : SetUp SDKConfig - Refer tech. Doc. for further info.*/
99+ private void SetConfig () {
100+
101+ ImageHelper .SetPageLayout (4 );//A1-A7(1-7),PHOTO,CUSTOM,ID(8,9,10)
102+
103+ ImageHelper .SetImageQuality (1 );//0,1,2 - Photo_Quality, Document_Quality, Compressed_Document
104+
105+ ImageHelper .SetDPI (200 );//int dpi_val = 100, 150, 200, 300, 500, 600;
106+
107+ //can set output file path
108+ CamConfigClass .OutputPath = BuildStoragePath ();
109+ }
110+
111+ /*DEV_HELP : BuildStoragePath*/
112+ private String BuildStoragePath () {
113+ ContextWrapper c = new ContextWrapper (this );
114+ String path = c .getExternalFilesDir (".GoNoGoImages" ).getAbsolutePath ();
115+ return path ;
116+ }
117+
118+ /*DEV_HELP : handleCaptureActivityResult definition*/
119+ private void handleCaptureActivityResult (ActivityResult result ){
120+ {
121+ int resultCode = result .getResultCode ();
122+ if (resultCode != Activity .RESULT_OK ) {
123+ return ;
124+ }
125+ Intent data = result .getData ();
126+ Boolean Status = null ;
127+ if (data != null ) {
128+ Status = (Boolean ) data .getExtras ().get ("STATUS" );
129+ }
130+ String Description = (String ) data .getExtras ().get ("DESCRIPTION" );
131+ if (!Status ) {
132+ String imageCaptureLog = "Description : " + Description +
133+ ".Exception: " + CameraSupport .CamConfigClass .LastLogInfo ;
134+ Log .d ("INFO" , imageCaptureLog );
135+ Toast .makeText (this , imageCaptureLog , Toast .LENGTH_LONG ).show ();
136+ finishActivity (REQUEST_CODE_FILE_RETURN );
137+ return ;
138+ }
139+ FileCollection = (ArrayList <String >) data .getExtras ().get ("fileCollection" );
140+ if (FileCollection == null || FileCollection .isEmpty ()) return ;
141+ try {
142+ showImages (FileCollection );
143+ } catch (IOException e ) {
144+ e .printStackTrace ();
145+ }
146+ finishActivity (REQUEST_CODE_FILE_RETURN );
147+ }
148+ }
149+
150+ /*DEV_HELP : showImages*/
151+ private void showImages (ArrayList <String > FilesPath ) throws IOException {
152+ int FileCollectionLength = FilesPath .size ();
153+ for (int i = 0 ; i < FileCollectionLength ; i ++) {
154+ String dir = FilesPath .get (i );
155+ File imgFile = new File (dir );
156+ //notifyMediaStoreScanner(imgFile);
157+ if (imgFile .exists ()) {
158+ Bitmap myBitmap = BitmapFactory .decodeFile (imgFile .getAbsolutePath ());
159+ ImageView myImage = findViewById (R .id .displayImageView );
160+ myImage .setImageBitmap (myBitmap );
161+ }
162+ Toast .makeText (this , "SDK captured " + FileCollectionLength + " images." , Toast .LENGTH_SHORT ).show ();
163+ }
164+ }
165+
166+ /*DEV_HELP : OpenCameraActivity*/
167+ private void OpenCameraActivity () {
168+
169+ /*DEV_HELP : Check basic permissions for camera if needed*/
170+ //if (!MainActivity.this.allPermissionsGranted()) {
171+ //Toast.makeText(MainActivity.this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show();
172+ /*DEV_HELP : TODO : handle invalid permission*/
173+ // return;
174+ // }
175+ try {
176+ /*DEV_HELP :redirecting to camera*/
177+ Intent CameraIntent = new Intent (this , Class .forName ("com.extrieve.quickcapture.sdk.CameraHelper" ));
178+ Uri photoURI = Uri .parse (CamConfigClass .OutputPath );
179+ this .grantUriPermission (this .getPackageName (), photoURI ,
180+ Intent .FLAG_GRANT_WRITE_URI_PERMISSION | Intent .FLAG_GRANT_READ_URI_PERMISSION );
181+ if (Build .VERSION .SDK_INT <= Build .VERSION_CODES .LOLLIPOP ) {
182+ CameraIntent .addFlags (Intent .FLAG_GRANT_WRITE_URI_PERMISSION );
183+ }
184+ captureActivityResultLauncher .launch (CameraIntent );
185+ } catch (Exception ex ) {
186+ /*DEV_HELP : TODO : handle invalid Exception*/
187+ Toast .makeText (this , "Failed to open camera -" + ex .getMessage (), Toast .LENGTH_LONG ).show ();
188+ }
189+
190+ }
191+
192+ }
0 commit comments