Home > Enterprise >  Take multiple photos and crop in Android Studio
Take multiple photos and crop in Android Studio

Time:08-16

I want to make that take multiple photos, crop each images and save only cropped images. But my code only crops the last taken image, so I just get the same images. I want to know what's the problem. Here's my code below.

Once more, I want to save cropped images as 400x240 pixel size, but at the test images were saved as 1050x630. How could I fix it?

I'm sorry that my code is not clear to see.

public Button btcamera;
public ImageView imageView;

private static final int REQUEST_TAKE_PHOTO = 1888;

public int PIC_CODE = 0;
public int i = 0;
public int n = 3; // how many do you want to take photo

Uri photoURI;

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

    imageView = findViewById(R.id.imageView);
    btcamera = findViewById(R.id.bt_camera);

    btcamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            for (i=0; i<n; i  ){

                takePictureIntent();
            }
        }
    });
}

@TargetApi(Build.VERSION_CODES.M)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {

        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {

            imageView.setImageURI(result.getUri());
            Toast.makeText(MainActivity.this, "Image Cropped Successfully", Toast.LENGTH_SHORT).show();

            saveImageToGallery();
        }
    }

    else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {

        if (photoURI != null) {
            Toast.makeText(this, "saved", Toast.LENGTH_SHORT).show();
            Log.d("hey", ""   photoURI);

            startCrop(photoURI);

            //saveImageToGallery();
        }
    }
}

@SuppressLint("QueryPermissionsNeeded")
private void takePictureIntent() {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.resolveActivity(this.getPackageManager());
    File photoFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "pickImageResult.jpeg");
    photoURI = FileProvider.getUriForFile(this, getApplicationContext().getPackageName()   ".fileprovider", photoFile);

    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}

private void startCrop (Uri imageuri){

    CropImage.activity(imageuri)
            .setAllowFlipping(false)
            .setAllowRotation(false)
            .setAllowCounterRotation(false)
            .setFixAspectRatio(true)
            .setMinCropResultSize(400, 240)
            .setMaxCropResultSize(400, 240)
            .setAspectRatio(5, 3)
            .start(MainActivity.this);
}

// 크롭된 이미지 저장
private void saveImageToGallery(){

    @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); // 연-월-일-시-분-초
    
    imageView.setDrawingCacheEnabled(true);
    Bitmap bitmap = imageView.getDrawingCache();
    MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, simpleDateFormat.format(new Date()), "");
}

CodePudding user response:

Seems you let it continuously take 3 photos here:

for (i=0; i<n; i  ) {
    takePictureIntent();
}

May try:

int i = 1;
int n = 3;

btcamera.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        takePictureIntent();
    }
});

In onActivityResult:

else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
    if (photoURI != null) {
        Toast.makeText(this, "saved", Toast.LENGTH_SHORT).show();
        Log.d("hey", ""   photoURI);
        startCrop(photoURI);
        i  ;
        if (i < n) {
            takePictureIntent();
        }
    }
}
  • Related