简体   繁体   English

在 Android 中拍照后验证按钮

[英]Validate buttons after taking a picture in Android

Button validation after taking a picture in Android.在 Android 中拍照后的按钮验证。 In my activity I have implemented two imageview(imageview1 and imageview2) with default images and two buttons(button1 and button2) that open the device camera, when the photo is taken from a button a validation is done to change the image view image(button1 -> imageview1, button2 -> imageview2).在我的活动中,我实现了两个带有默认图像的 imageview(imageview1 和 imageview2)和打开设备相机的两个按钮(button1 和 button2),当从按钮拍摄照片时,会进行验证以更改图像视图图像(button1 -> imageview1, button2 -> imageview2)。

I want to do a third validation where it validates that the photo has already been taken from the two buttons.我想做第三次验证,它验证照片是否已经从两个按钮拍摄。

How can I validate when the photos are already taken on the two buttons?我如何验证何时已经在两个按钮上拍摄了照片?

This is the code I have这是我的代码

ImageView imageV, imageV2;
Button btn1, btn2;
static final int IMAGE_REQUEST = 1;
static final int IMAGE_REQUEST_2 = 2;
private static final int PERMISSION_REQUEST = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

imageV = findViewById(R.id.image_view);
imageV2 = findViewById(R.id.image_view_2);
btn1= findViewById(R.id.button_1);
btn2= findViewById(R.id.button_2);

btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (camara.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(camara, IMAGE_REQUEST);
       }
    }
});

btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (camara.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(camara, IMAGE_REQUEST_2);
       }
    }
 });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == IMAGE_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {
      
        imageV.setImageResource(R.drawable.image_view_2do);

    }
    else if (requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
        
            imageV2.setImageResource(R.drawable.image_view2_2do);
        }
    }
    // This is where the code fails me 
    //how can I validate if the photos are already taken on the two buttons?
    else if (requestCode == IMAGE_REQUEST && requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
            imageV.setImageResource(R.drawable.image_view_2do);
            imageV2.setImageResource(R.drawable.image_view2_2do);
        }
    }
 }
}

You can keep a 2 booleans that keep track of whether the images are loaded in the each imageView and update them in onActivityResult .您可以保留 2 个布尔值来跟踪图像是否已加载到每个 imageView 中并在onActivityResult更新它们。

Check isImage1Loaded and isImage2Loaded in the below code.在下面的代码中检查isImage1LoadedisImage2Loaded

ImageView imageV, imageV2;
Button btn1, btn2;
static final int IMAGE_REQUEST = 1;
static final int IMAGE_REQUEST_2 = 2;
boolean isImage1Loaded = false, isImage2Loaded = false;
private static final int PERMISSION_REQUEST = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

imageV = findViewById(R.id.image_view);
imageV2 = findViewById(R.id.image_view_2);
btn1= findViewById(R.id.button_1);
btn2= findViewById(R.id.button_2);

btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (camara.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(camara, IMAGE_REQUEST);
       }
    }
});

btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (camara.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(camara, IMAGE_REQUEST_2);
       }
    }
 });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == IMAGE_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {
        isImage1Loaded = true;
        imageV.setImageResource(R.drawable.image_view_2do);

    }
    else if (requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
            image2Loaded = true;
            imageV2.setImageResource(R.drawable.image_view2_2do);
        }
    }
    // This is where the code fails me 
    //how can I validate if the photos are already taken on the two buttons?
    else if (requestCode == IMAGE_REQUEST && requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
            imageV.setImageResource(R.drawable.image_view_2do);
            imageV2.setImageResource(R.drawable.image_view2_2do);
        }
    }
 }
} 

I've noticed that you're comparing the value of requestCode with IMAGE_REQUEST and next with IMAGE_REQUEST_2 .我注意到您将 requestCode 的值与IMAGE_REQUEST和 next 与IMAGE_REQUEST_2

After that in the section where your code breaks, you're comparing both in one line.在代码中断的部分之后,您将在一行中比较两者。 But I believe that this will never work since the requestCode has only one value, and you're comparing it with two different values [IMAGE_REQUEST, IMAGE_REQUEST_2]但我相信这永远不会奏效,因为 requestCode 只有一个值,并且您将它与两个不同的值进行比较 [IMAGE_REQUEST, IMAGE_REQUEST_2]

I suggest you to add the IMAGE_REQUEST_2 and the IMAGE_REQUEST to an array everytime you take a photo with that button.我建议您每次使用该按钮拍照时都将 IMAGE_REQUEST_2 和 IMAGE_REQUEST 添加到数组中。

And then comparing if both values exist in that array, if so you do what you want.然后比较该数组中是否存在两个值,如果存在,则执行所需的操作。

Like that:像那样:

ImageView imageV, imageV2;
Button btn1, btn2;
static final int IMAGE_REQUEST = 1;
static final int IMAGE_REQUEST_2 = 2;
private static final int PERMISSION_REQUEST = 2;

// creates a new list to store the photos taken
List<int> requestsMade = new ArrayList<int>();

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

imageV = findViewById(R.id.image_view);
imageV2 = findViewById(R.id.image_view_2);
btn1= findViewById(R.id.button_1);
btn2= findViewById(R.id.button_2);

btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (camara.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(camara, IMAGE_REQUEST);
       }
    }
});

btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       if (camara.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(camara, IMAGE_REQUEST_2);
       }
    }
 });

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == IMAGE_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {
      
        imageV.setImageResource(R.drawable.image_view_2do);
        // first photo taken
        requestsMade.add(IMAGE_REQUEST);

    }
    else if (requestCode == IMAGE_REQUEST_2) {
        if (resultCode == Activity.RESULT_OK) {
        
            imageV2.setImageResource(R.drawable.image_view2_2do);
            requestsMade.add(IMAGE_REQUEST_2);
            // second photo taken

        }
    }
    // now this compares if both are taken
    else if (requestsMade.contains(IMAGE_REQUEST) && requestsMade.contains(IMAGE_REQUEST_2)) {
        if (resultCode == Activity.RESULT_OK) {
            imageV.setImageResource(R.drawable.image_view_2do);
            imageV2.setImageResource(R.drawable.image_view2_2do);
        }
    }
 }
}

Maintain 2 boolean variables维护 2 个布尔变量

boolean isPictureTakenBtn1, isPictureTakenBtn2; boolean isPictureTakenBtn1, isPictureTakenBtn2;

Then in onActivityResult update the variables accordingly然后在 onActivityResult 中相应地更新变量

  onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri selectedImage = null;
    boolean isPictureTakenBtn1, isPictureTakenBtn2;

    if (requestCode == IMAGE_REQUEST) {
        if (resultCode == RESULT_OK) {                
            isPictureTakenBtn1 = true;
        } else if (resultCode == RESULT_CANCELED) {
            isPictureTakenBtn1 = false;
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
        } else {
            isPictureTakenBtn1 = false;
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
        }
    } else if (requestCode == IMAGE_REQUEST_2) {
        if (resultCode == RESULT_OK) {            
            isPictureTakenBtn2 = true;
        } else if (resultCode == RESULT_CANCELED) {
            isPictureTakenBtn2 = false;
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
        } else {
            isPictureTakenBtn2 = false;
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
        }
    }

    if (isPictureTakenBtn1) {
        //picture capture from btn1 available, do something
        /* if (data != null) {
                        selectedImage = data.getData();
                        InputStream in;
                        try {
                            in = getContentResolver().openInputStream(uri);
                            Bitmap selected_img = BitmapFactory.decodeStream(in);
                            imageV.setImageBitmap(selected_img);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                            Toast.makeText(this, "An error occured!", Toast.LENGTH_LONG).show();
                        }
                    }*/
    }
    if (isPictureTakenBtn2) {
        //picture capture from btn2 available, do something
        /* if (data != null) {
                        selectedImage = data.getData();
                        InputStream in;
                        try {
                            in = getContentResolver().openInputStream(uri);
                            Bitmap selected_img = BitmapFactory.decodeStream(in);
                            imageV2.setImageBitmap(selected_img);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                            Toast.makeText(this, "An error occured!", Toast.LENGTH_LONG).show();
                        }
                    }*/
    }
}

Now you can use these boolean values to check if the image was successfully captured and execute your remaining code logic as needed.现在您可以使用这些布尔值来检查图像是否已成功捕获并根据需要执行剩余的代码逻辑。

You can manage it through Boolean value like below example;您可以通过布尔值来管理它,如下例所示;

 ImageView imageV, imageV2; Button btn1, btn2; static final int IMAGE_REQUEST = 1; static final int IMAGE_REQUEST_2 = 2; private static final int PERMISSION_REQUEST = 2; boolean isFirstImageSet=false; boolean isSecondImageSet=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageV = findViewById(R.id.image_view); imageV2 = findViewById(R.id.image_view_2); btn1= findViewById(R.id.button_1); btn2= findViewById(R.id.button_2); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (camara.resolveActivity(getPackageManager()) != null) { startActivityForResult(camara, IMAGE_REQUEST); } } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (camara.resolveActivity(getPackageManager()) != null) { startActivityForResult(camara, IMAGE_REQUEST_2); } } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(isFirstImageSet && isSecondImageSet){ //Both image are set } else if(isFirstImageSet ){ //First image are set } else if(isSecondImageSet){ //First image are set } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == IMAGE_REQUEST) { if (resultCode == Activity.RESULT_OK) { imageV.setImageResource(R.drawable.image_view_2do); isFirstImageSet=true; } else if (requestCode == IMAGE_REQUEST_2) { if (resultCode == Activity.RESULT_OK) { imageV2.setImageResource(R.drawable.image_view2_2do); isSecondImageSet=true; } } // This is where the code fails me //how can I validate if the photos are already taken on the two buttons? else if (requestCode == IMAGE_REQUEST && requestCode == IMAGE_REQUEST_2) { if (resultCode == Activity.RESULT_OK) { imageV.setImageResource(R.drawable.image_view_2do); imageV2.setImageResource(R.drawable.image_view2_2do); } } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM