在Android Studio中拍摄多张照片并进行裁剪

0 人关注

我想让它拍摄多张照片,裁剪每张图片,只保存裁剪过的图片。 但我的代码只裁剪最后一张照片,所以我只得到相同的图像。 我想知道问题出在哪里。下面是我的代码。

再一次,我想把裁剪后的图像保存为400x240像素的大小,但在测试时图像被保存为1050x630。我怎样才能解决这个问题?

我很抱歉,我的代码看不清楚。

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()), "");
    
android
crop
cherrypicker0413
cherrypicker0413
发布于 2022-08-16
1 个回答
TylerQITX
TylerQITX
发布于 2022-08-16
已采纳
0 人赞同

似乎你在这里让它连续拍了3张照片。

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

可以尝试。

int i = 1;
int n = 3;
btcamera.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        takePictureIntent();

在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);
        if (i < n) {