Search code examples
javaandroidandroid-studioface-recognition

how to avoid image captured repeatedly


I'm new in android development.I'm currently developing an apps that comparing image from id card with the image of face.From UploadActivity,user will capture id card and after click "OK" user will be prompted to live front camera from LivenessActivity to capture user's face .Supposedly after doing movement which is blink eyes ,face will be successfully captured and automatically prompt the UploadActivity.In the UploadActivity,user will click button "VIEWFACE" and the image of face will appear.But unfortunately,during capturing face image,the LivenessActivity keeps appearing to ask user capturing again .But if user click back button ,then it will prompt UploadActivity.How to avoid the LivenessActivity keep appearing after capture face?

This is my Upload Activity and LivenessActivity for your reference:

UploadActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload);

    txtPercentage = (TextView) findViewById(R.id.txtPercentage);

    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    imgPreview = (ImageView) findViewById(R.id.imgPreview);
    imgFace = (ImageView) findViewById(R.id.imgFace);
    btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);



    btnCapturePicture.setOnClickListener(new View.OnClickListener() {



        @Override
        public void onClick(View v) {

            captureImage();
        }

    });



    Intent i = getIntent();

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



      //call LivenessActivity
        startActivity(new Intent(UploadActivity.this,
        LivenessActivity.class));


    btnviewface = (Button) findViewById(R.id.btnviewface);
    btnviewface.setOnClickListener(new View.OnClickListener() {



        @Override
        public void onClick(View view) {

            Bundle extras = getIntent().getExtras();
            byte[] face = extras.getByteArray("image_best");

            //save byte[] in the sd card, make a folder
            try {
                Config.IMAGE_FACE = "/storage/emulated/0/Pictures/Android 
          File Upload/IMG_face.jpg";
                FileOutputStream stream = new FileOutputStream( 
          Config.IMAGE_FACE);
                stream.write(face);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            Bitmap bmp = BitmapFactory.decodeByteArray(face, 0, 
          face.length);
            imgFace.setImageBitmap(bmp);
            previewMedia(isImage);
            new UploadFileToServer(Config.IMAGE_DOC, 
        Config.IMAGE_FACE).execute();

        }

    } );

LivenessActivity:

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

    FaceIDDataStruct dataStruct = mDetector.getFaceIDDataStruct();
    if (dataStruct != null) {
        face = dataStruct.images.get("image_best");

        Intent intent = new Intent(LivenessActivity.this, UploadActivity.class);
        //facefileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        //intent.putExtra("filePath", facefileUri.getPath());
        intent.putExtra("image_best",face);
        setResult(PAGE_INTO_LIVENESS, intent);

        startActivity(intent);

    }

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

}

Solution

  • Remove startActivity in LivenessActivity instead of this call finish() or LivenessActivity.this.finish()

    @Override
    public Detector.DetectionType onDetectionSuccess(DetectionFrame validFrame) {
    
        FaceIDDataStruct dataStruct = mDetector.getFaceIDDataStruct();
        if (dataStruct != null) {
            face = dataStruct.images.get("image_best");
    
            Intent intent = new Intent(LivenessActivity.this, UploadActivity.class);
            //facefileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
            //intent.putExtra("filePath", facefileUri.getPath());
            intent.putExtra("image_best",face);
            setResult(PAGE_INTO_LIVENESS, intent);
    
            startActivity(intent); // Remove this
            finish(); // LivenessActivity.this.finish()
    
        }
    
        if (face == null) {
            face = validFrame.getCroppedFaceImageData();
        }//do something with liveness face
    return DetectionType.DONE;
    
    }