Java 在图像视图(Android Studio)中分别设置相机的两个裁剪图像

Java 在图像视图(Android Studio)中分别设置相机的两个裁剪图像,java,android,android-studio,android-intent,onactivityresult,Java,Android,Android Studio,Android Intent,Onactivityresult,我尝试从相机中拍摄两张图像,然后在两张图像视图中分别显示裁剪后的图像。 我有两个按钮打开相机,一个用于拍摄第一幅图像,然后裁剪以在图像视图中显示,第二个则执行相同的操作 main活动中的我的代码 计算中的变量 static int CAMERA_REQUEST_CODE = 228; static int CAMERA_REQUEST_CODE1 = 229; Uri pictureUri = null; ImageView iv, iv1; Button bt, bt1; onCreate

我尝试从相机中拍摄两张图像,然后在两张
图像视图中分别显示裁剪后的图像。
我有两个
按钮
打开相机,一个用于拍摄第一幅图像,然后裁剪以在
图像视图
中显示,第二个则执行相同的操作

main活动中的我的代码

计算中的变量

static int CAMERA_REQUEST_CODE = 228;
static int CAMERA_REQUEST_CODE1 = 229;
Uri pictureUri = null;

ImageView iv, iv1;
Button bt, bt1;
onCreate方法

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

    iv = findViewById(R.id.iv);
    bt = findViewById(R.id.bt);

    iv1 = findViewById(R.id.iv1);
    bt1 = findViewById(R.id.bt1);


    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            invokeCamera();
        }
    });

    bt1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            invokeCamera1();
        }
    });

}
invokeCamera()和invokeCamera1()函数

public void invokeCamera() {

    // get a file reference
    pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), createImageFile()); // Make Uri file example file://storage/emulated/0/Pictures/Civil_ID20180924_180619.jpg

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Go to camera

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, CAMERA_REQUEST_CODE);
}

public void invokeCamera1() {

    // get a file reference
    pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), createImageFile()); // Make Uri file example file://storage/emulated/0/Pictures/Civil_ID20180924_180619.jpg

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Go to camera

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, CAMERA_REQUEST_CODE1);
}
    // To create image file in pictures directory
public File createImageFile() {
    // the public picture director
    File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); // To get pictures directory from android system

    // timestamp makes unique name.
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String timestamp = sdf.format(new Date());

    // put together the directory and the timestamp to make a unique image location.
    File imageFile = new File(picturesDirectory, timestamp + ".jpg");

    return imageFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
 data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK)  // resultCode: -1
    {
        if(requestCode == CAMERA_REQUEST_CODE ) // requestCode: 288
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri);
            Toast.makeText(MainActivity.this, "Image 1 save", 
Toast.LENGTH_SHORT).show();
        }
        if(requestCode == CAMERA_REQUEST_CODE1)
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri);
            Toast.makeText(MainActivity.this, "Image 2 save", 
Toast.LENGTH_SHORT).show();
        }
    }

   if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

            if(resultCode == RESULT_OK)
            {
                Croppedimage(result, iv); // my problem !

                /*
                *  Here i want to use if or switch statement to can use iv1 
for second camera button! HOW?
                *  
                * example 
                * 
                * if(for first camera button)
                * {
                *   Croppedimage(result, iv);
                * }
                * 
                * if(for second camera button)
                * {
                *   Croppedimage(result, iv1);
                * }
                *
                * */

            }

        else if(resultCode == 
CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
        {
            // if there is any error show it
            Exception error = result.getError();
            Toast.makeText(this, "" + error, Toast.LENGTH_LONG).show();
        }
    }
}
    private void startCropImageActivity(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .setMultiTouchEnabled(true)
            .start(this);
}
  public void Croppedimage(CropImage.ActivityResult result,ImageView iv)
{
    Uri resultUri = null; // get image uri
    if (result != null) {
        resultUri = result.getUri();
    }

    //set image to image view
    iv.setImageURI(resultUri);

}
createImageFile()函数

public void invokeCamera() {

    // get a file reference
    pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), createImageFile()); // Make Uri file example file://storage/emulated/0/Pictures/Civil_ID20180924_180619.jpg

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Go to camera

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, CAMERA_REQUEST_CODE);
}

public void invokeCamera1() {

    // get a file reference
    pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), createImageFile()); // Make Uri file example file://storage/emulated/0/Pictures/Civil_ID20180924_180619.jpg

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Go to camera

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, CAMERA_REQUEST_CODE1);
}
    // To create image file in pictures directory
public File createImageFile() {
    // the public picture director
    File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); // To get pictures directory from android system

    // timestamp makes unique name.
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String timestamp = sdf.format(new Date());

    // put together the directory and the timestamp to make a unique image location.
    File imageFile = new File(picturesDirectory, timestamp + ".jpg");

    return imageFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
 data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK)  // resultCode: -1
    {
        if(requestCode == CAMERA_REQUEST_CODE ) // requestCode: 288
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri);
            Toast.makeText(MainActivity.this, "Image 1 save", 
Toast.LENGTH_SHORT).show();
        }
        if(requestCode == CAMERA_REQUEST_CODE1)
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri);
            Toast.makeText(MainActivity.this, "Image 2 save", 
Toast.LENGTH_SHORT).show();
        }
    }

   if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

            if(resultCode == RESULT_OK)
            {
                Croppedimage(result, iv); // my problem !

                /*
                *  Here i want to use if or switch statement to can use iv1 
for second camera button! HOW?
                *  
                * example 
                * 
                * if(for first camera button)
                * {
                *   Croppedimage(result, iv);
                * }
                * 
                * if(for second camera button)
                * {
                *   Croppedimage(result, iv1);
                * }
                *
                * */

            }

        else if(resultCode == 
CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
        {
            // if there is any error show it
            Exception error = result.getError();
            Toast.makeText(this, "" + error, Toast.LENGTH_LONG).show();
        }
    }
}
    private void startCropImageActivity(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .setMultiTouchEnabled(true)
            .start(this);
}
  public void Croppedimage(CropImage.ActivityResult result,ImageView iv)
{
    Uri resultUri = null; // get image uri
    if (result != null) {
        resultUri = result.getUri();
    }

    //set image to image view
    iv.setImageURI(resultUri);

}
onActivityResult函数

public void invokeCamera() {

    // get a file reference
    pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), createImageFile()); // Make Uri file example file://storage/emulated/0/Pictures/Civil_ID20180924_180619.jpg

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Go to camera

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, CAMERA_REQUEST_CODE);
}

public void invokeCamera1() {

    // get a file reference
    pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), createImageFile()); // Make Uri file example file://storage/emulated/0/Pictures/Civil_ID20180924_180619.jpg

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Go to camera

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, CAMERA_REQUEST_CODE1);
}
    // To create image file in pictures directory
public File createImageFile() {
    // the public picture director
    File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); // To get pictures directory from android system

    // timestamp makes unique name.
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String timestamp = sdf.format(new Date());

    // put together the directory and the timestamp to make a unique image location.
    File imageFile = new File(picturesDirectory, timestamp + ".jpg");

    return imageFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
 data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK)  // resultCode: -1
    {
        if(requestCode == CAMERA_REQUEST_CODE ) // requestCode: 288
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri);
            Toast.makeText(MainActivity.this, "Image 1 save", 
Toast.LENGTH_SHORT).show();
        }
        if(requestCode == CAMERA_REQUEST_CODE1)
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri);
            Toast.makeText(MainActivity.this, "Image 2 save", 
Toast.LENGTH_SHORT).show();
        }
    }

   if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

            if(resultCode == RESULT_OK)
            {
                Croppedimage(result, iv); // my problem !

                /*
                *  Here i want to use if or switch statement to can use iv1 
for second camera button! HOW?
                *  
                * example 
                * 
                * if(for first camera button)
                * {
                *   Croppedimage(result, iv);
                * }
                * 
                * if(for second camera button)
                * {
                *   Croppedimage(result, iv1);
                * }
                *
                * */

            }

        else if(resultCode == 
CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
        {
            // if there is any error show it
            Exception error = result.getError();
            Toast.makeText(this, "" + error, Toast.LENGTH_LONG).show();
        }
    }
}
    private void startCropImageActivity(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .setMultiTouchEnabled(true)
            .start(this);
}
  public void Croppedimage(CropImage.ActivityResult result,ImageView iv)
{
    Uri resultUri = null; // get image uri
    if (result != null) {
        resultUri = result.getUri();
    }

    //set image to image view
    iv.setImageURI(resultUri);

}
启动热带图像活动功能

public void invokeCamera() {

    // get a file reference
    pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), createImageFile()); // Make Uri file example file://storage/emulated/0/Pictures/Civil_ID20180924_180619.jpg

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Go to camera

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, CAMERA_REQUEST_CODE);
}

public void invokeCamera1() {

    // get a file reference
    pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), createImageFile()); // Make Uri file example file://storage/emulated/0/Pictures/Civil_ID20180924_180619.jpg

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Go to camera

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, CAMERA_REQUEST_CODE1);
}
    // To create image file in pictures directory
public File createImageFile() {
    // the public picture director
    File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); // To get pictures directory from android system

    // timestamp makes unique name.
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String timestamp = sdf.format(new Date());

    // put together the directory and the timestamp to make a unique image location.
    File imageFile = new File(picturesDirectory, timestamp + ".jpg");

    return imageFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
 data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK)  // resultCode: -1
    {
        if(requestCode == CAMERA_REQUEST_CODE ) // requestCode: 288
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri);
            Toast.makeText(MainActivity.this, "Image 1 save", 
Toast.LENGTH_SHORT).show();
        }
        if(requestCode == CAMERA_REQUEST_CODE1)
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri);
            Toast.makeText(MainActivity.this, "Image 2 save", 
Toast.LENGTH_SHORT).show();
        }
    }

   if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

            if(resultCode == RESULT_OK)
            {
                Croppedimage(result, iv); // my problem !

                /*
                *  Here i want to use if or switch statement to can use iv1 
for second camera button! HOW?
                *  
                * example 
                * 
                * if(for first camera button)
                * {
                *   Croppedimage(result, iv);
                * }
                * 
                * if(for second camera button)
                * {
                *   Croppedimage(result, iv1);
                * }
                *
                * */

            }

        else if(resultCode == 
CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
        {
            // if there is any error show it
            Exception error = result.getError();
            Toast.makeText(this, "" + error, Toast.LENGTH_LONG).show();
        }
    }
}
    private void startCropImageActivity(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .setMultiTouchEnabled(true)
            .start(this);
}
  public void Croppedimage(CropImage.ActivityResult result,ImageView iv)
{
    Uri resultUri = null; // get image uri
    if (result != null) {
        resultUri = result.getUri();
    }

    //set image to image view
    iv.setImageURI(resultUri);

}
裁剪图像功能

public void invokeCamera() {

    // get a file reference
    pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), createImageFile()); // Make Uri file example file://storage/emulated/0/Pictures/Civil_ID20180924_180619.jpg

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Go to camera

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, CAMERA_REQUEST_CODE);
}

public void invokeCamera1() {

    // get a file reference
    pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName(), createImageFile()); // Make Uri file example file://storage/emulated/0/Pictures/Civil_ID20180924_180619.jpg

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Go to camera

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, CAMERA_REQUEST_CODE1);
}
    // To create image file in pictures directory
public File createImageFile() {
    // the public picture director
    File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); // To get pictures directory from android system

    // timestamp makes unique name.
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String timestamp = sdf.format(new Date());

    // put together the directory and the timestamp to make a unique image location.
    File imageFile = new File(picturesDirectory, timestamp + ".jpg");

    return imageFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
 data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK)  // resultCode: -1
    {
        if(requestCode == CAMERA_REQUEST_CODE ) // requestCode: 288
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri);
            Toast.makeText(MainActivity.this, "Image 1 save", 
Toast.LENGTH_SHORT).show();
        }
        if(requestCode == CAMERA_REQUEST_CODE1)
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri);
            Toast.makeText(MainActivity.this, "Image 2 save", 
Toast.LENGTH_SHORT).show();
        }
    }

   if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

            if(resultCode == RESULT_OK)
            {
                Croppedimage(result, iv); // my problem !

                /*
                *  Here i want to use if or switch statement to can use iv1 
for second camera button! HOW?
                *  
                * example 
                * 
                * if(for first camera button)
                * {
                *   Croppedimage(result, iv);
                * }
                * 
                * if(for second camera button)
                * {
                *   Croppedimage(result, iv1);
                * }
                *
                * */

            }

        else if(resultCode == 
CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
        {
            // if there is any error show it
            Exception error = result.getError();
            Toast.makeText(this, "" + error, Toast.LENGTH_LONG).show();
        }
    }
}
    private void startCropImageActivity(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .setMultiTouchEnabled(true)
            .start(this);
}
  public void Croppedimage(CropImage.ActivityResult result,ImageView iv)
{
    Uri resultUri = null; // get image uri
    if (result != null) {
        resultUri = result.getUri();
    }

    //set image to image view
    iv.setImageURI(resultUri);

}
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu强

问题

在第一个
Imageview
中设置的第二个
按钮的裁剪图像。
需要在第二个摄像头的
按钮
onActivityResult
中找到到达
iv1
的方法

有什么建议吗


谢谢

查看我在GitHub respository上发现的问题(与您的问题类似),您可以在启动crop活动时设置自定义请求代码

因此,您可以使用两个不同的请求代码启动活动,并检查哪一个已在ActivityResult上使用

private static final RC_CROP = 100;
private static final RC_CROP1 = 200;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK)  // resultCode: -1
    {
        if (requestCode == CAMERA_REQUEST_CODE) // requestCode: 288
        {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri, RC_CROP);
            Toast.makeText(MainActivity.this, "Image 1 save",
                    Toast.LENGTH_SHORT).show();
        }
        if (requestCode == CAMERA_REQUEST_CODE1) {
            Uri picUri = pictureUri;
            startCropImageActivity(picUri, RC_CROP1);
            Toast.makeText(MainActivity.this, "Image 2 save",
                    Toast.LENGTH_SHORT).show();
        }

        if (requestCode == RC_CROP) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            //put image on first ImageView
        }

        if (requestCode == RC_CROP1) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            //put image on second ImageView
        }

    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
        // if there is any error show it
        Exception error = result.getError();
        Toast.makeText(this, "" + error, Toast.LENGTH_LONG).show();
    }
}


private void startCropImageActivity(Uri imageUri, int requestCode) {
    Intent vCropIntent = CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .setMultiTouchEnabled(true)
            .getIntent(this);

    startActivityForResult(vCropIntent, requestCode)
}
我还建议在检查requestCode时使用switch语句