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

Set two cropped image from camera separately in image view (Android Studio)

我尝试从相机拍摄两张图像并裁剪图像,然后分别在两个 Imageview 中显示裁剪图像。 我有 2 个 Button 打开相机,一个用于捕获第一张图像,然后裁剪以在 Imageview 中显示它,第二个做同样的事情。

我的代码在MainActivity

类中变量

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

createImageFile() 函数

    // 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;
}

onActivityResult 函数

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

startCropImageActivity 函数

    private void startCropImageActivity(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .setMultiTouchEnabled(true)
            .start(this);
}

Croppedimage 函数

  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);

}

____________________________________________________________________________

问题

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

有什么建议吗?

Library use for crop image

谢谢。

查看 GitHub 库上的问题,我发现 this one,这似乎与你的相似,你可以在开始裁剪时设置自定义请求代码 activity。

因此您可以使用 2 个不同的请求代码启动 activity 并检查在 onActivityResult

上使用了哪一个
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 语句