Search code examples
javaandroidandroid-studioface-recognition

How to directly upload images to server without click button?


Hye.I'm doing apps that comparing two faces between image in ID Card and live capture face image.User capture image of ID card at UploadActivity.Then,front camera at LivenessActivity will be prompted to capture face image.Then UploadActivity will automatically appear along with both images that were captured.User need to click button "verify" and it will show progress bar and upload the images to server to compare them.But,what code I have to put so that it can upload both images to the server without clicking the "verify" button?Perhaps after images appear at UploadActivity,it will directly show progress bar and upload the images to the server .This is my code for your references.Thank you in advance.

UploadActivity:

btnCaptureId.setOnClickListener(new View.OnClickListener(){

@Override
        public void onClick(View v) {

            captureImage();
        }


    });


    // boolean flag to     identify the media type, image or video
    final boolean isImage = i.getBooleanExtra("isImage",true);
    previewMedia(isImage);

    if (fileUri != null )
    {
        //go to LivenessActivity to caoture image of face
        Intent intent = new Intent(this, LivenessActivity.class);
        startActivityForResult(intent, 2);
    }


    btnverify.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {



            // uploading the file to server
            try {
                new UploadFileToServer(Config.IMAGE_DOC, Config.IMAGE_FACE).execute();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    });

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

LivenessActivity:

@Override
public Detector.DetectionType onDetectionSuccess(DetectionFrame validFrame) {

    FaceIDDataStruct dataStruct = mDetector.getFaceIDDataStruct();

    if (dataStruct != null) {

        face = dataStruct.images.get("image_best");
        Intent returnIntent = new Intent();
        returnIntent.putExtra("image_best",face);
     //result go to UploadActivity
        setResult(UploadActivity.PAGE_INTO_LIVENESS, returnIntent);
        finish();

    }

    if (face == null) {
        face = validFrame.getCroppedFaceImageData();
    }
  //do something with liveness face
    return DetectionType.DONE;


}

Solution

  • This is the code that I add to make it automatically upload without click button.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image
        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
    
                // successfully captured the image
                // launching upload activity
                launchUploadActivity(true);
    
    
            } else if (resultCode == RESULT_CANCELED) {
    
                // user cancelled Image capture
                Toast.makeText(getApplicationContext(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();
    
            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    
        if (requestCode == 2) {
            if (resultCode == PAGE_INTO_LIVENESS) {
    
                Bundle extras = getIntent().getExtras();
                byte[] face = extras.getByteArray("image_best");
    
                viewImage();
    //automatically upload to server
                try {
                    new UploadFileToServer(Config.IMAGE_DOC, Config.IMAGE_FACE).execute();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
    
            } else if (resultCode == RESULT_CANCELED) {
    
                // user cancelled recording
                Toast.makeText(getApplicationContext(),
                        "User cancelled video recording", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    
    }