在 Android 中拍照后验证按钮
Validate buttons after taking a picture in Android
在 Android 中拍照后的按钮验证。
在我的 activity 中,我实现了两个带有默认图像的图像视图(imageview1 和 imageview2)和两个打开设备相机的按钮(button1 和 button2),当从按钮拍摄照片时,完成验证以更改图像查看图片(按钮 1 -> imageview1,按钮 2 -> imageview2)。
我想进行第三次验证,验证照片是否已通过两个按钮拍摄。
如何在两个按钮上拍摄照片时进行验证?
这是我的代码
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);
}
}
}
}
我注意到您正在将 requestCode 的值与 IMAGE_REQUEST
以及 next 与 IMAGE_REQUEST_2
进行比较。
在那之后,在您的代码中断的部分,您将在一行中比较两者。但我相信这永远不会起作用,因为 requestCode 只有一个值,而您正在将它与两个不同的值进行比较 [IMAGE_REQUEST, IMAGE_REQUEST_2]
我建议您每次使用该按钮拍照时都将 IMAGE_REQUEST_2 和 IMAGE_REQUEST 添加到数组中。
然后比较两个值是否都存在于该数组中,如果存在,你就做你想做的。
像那样:
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);
}
}
}
}
维护2个布尔变量
boolean isPictureTakenBtn1, isPictureTakenBtn2;
然后在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();
}
}*/
}
}
现在您可以使用这些布尔值来检查图像是否已成功捕获并根据需要执行剩余的代码逻辑。
在 Android 中拍照后的按钮验证。 在我的 activity 中,我实现了两个带有默认图像的图像视图(imageview1 和 imageview2)和两个打开设备相机的按钮(button1 和 button2),当从按钮拍摄照片时,完成验证以更改图像查看图片(按钮 1 -> imageview1,按钮 2 -> imageview2)。
我想进行第三次验证,验证照片是否已通过两个按钮拍摄。
如何在两个按钮上拍摄照片时进行验证?
这是我的代码
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);
}
}
}
}
我注意到您正在将 requestCode 的值与 IMAGE_REQUEST
以及 next 与 IMAGE_REQUEST_2
进行比较。
在那之后,在您的代码中断的部分,您将在一行中比较两者。但我相信这永远不会起作用,因为 requestCode 只有一个值,而您正在将它与两个不同的值进行比较 [IMAGE_REQUEST, IMAGE_REQUEST_2]
我建议您每次使用该按钮拍照时都将 IMAGE_REQUEST_2 和 IMAGE_REQUEST 添加到数组中。
然后比较两个值是否都存在于该数组中,如果存在,你就做你想做的。
像那样:
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);
}
}
}
}
维护2个布尔变量
boolean isPictureTakenBtn1, isPictureTakenBtn2;
然后在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();
}
}*/
}
}
现在您可以使用这些布尔值来检查图像是否已成功捕获并根据需要执行剩余的代码逻辑。