如何在没有点击按钮的情况下直接将图像上传到服

时间:2018-01-05 03:51:27

标签: java android android-studio face-recognition

Hye。我正在做比较ID卡中的图像和实时捕捉面部图像之间的两个面部的应用程序。在UploadActivity上使用ID卡的用户捕获图像。然后,将提示LivenessActivity的前置摄像头捕获面部图像。然后上传活动将自动与捕获的两个图像一起出现。用户需要单击“验证”按钮,它将显示进度条并将图像上传到服务器进行比较。但是,我必须放置什么代码才能上传两个图像没有点击“验证”按钮到服务器?也许在UploadActivity上出现图像后,它会直接显示进度条并将图像上传到服务器。这是我的代码供你参考。谢谢你。

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;


}

3 个答案:

答案 0 :(得分:0)

button.performClick();以编程方式点击

答案 1 :(得分:0)

这是我添加的代码,使其无需点击按钮即可自动上传。

@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();
        }
    }

}

答案 2 :(得分:0)