捕获的相机图像无法裁剪

时间:2017-05-16 13:43:44

标签: android image camera

我正试图从手机图库中获取图像或从相机中捕获图像..我使用了' me.villani.lorenzo.android:android-cropimage:1.1。+'用于裁剪图像。它适用于从手机图库中获取图像。虽然从相机捕获图像,它捕获图像,但它无法裁剪..从图库中选择图像工作正常..这是我包括我的代码,请看看,

public class Details extends AppCompatActivity {
    ImageView i1,i2;
    Bitmap bitmapPic;
    private static int REQUEST_PICTURE = 1;
    private final static int REQUEST_PERMISSION_REQ_CODE = 34;
    private static int REQUEST_CAMERA = 0, SELECT_FILE = 1, REQUEST_CROP_PICTURE = 2;
    private static int CROP_IMAGE_SIZE = 200;
    private static int CROP_IMAGE_HIGHLIGHT_COLOR = 0x6aa746F4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        android.support.v7.app.ActionBar ab = getSupportActionBar();
        if (ab != null) {
            ab.hide();
        }
        i1 = (ImageView)findViewById(R.id.prof1);
        i2 = (ImageView)findViewById(R.id.prof2);
        i1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectImageOption();
            }
        });
    }
    private void selectImageOption() {
        final CharSequence[] items = { "Capture Photo", "Choose from Gallery", "Cancel" };
        AlertDialog.Builder builder = new AlertDialog.Builder(Details.this);
        builder.setTitle("Add Photo!");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Capture Photo")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, REQUEST_CAMERA);
                } else if (items[item].equals("Choose from Gallery")) {
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                    intent.setType("image/*");
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent, SELECT_FILE);
                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        File croppedImageFile = new File(getFilesDir(), "Pic.jpg");
        Uri croppedImage = Uri.fromFile(croppedImageFile);
        if (requestCode == REQUEST_CROP_PICTURE && resultCode == RESULT_OK) {
            bitmapPic = BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath());
            if (bitmapPic != null) {
                i1.setImageBitmap(bitmapPic);
            } else {
                Toast.makeText(Details.this, "Image Error while Cropping", Toast.LENGTH_LONG).show();
            }
        } else if (resultCode == RESULT_OK && (requestCode == REQUEST_CAMERA || requestCode == SELECT_FILE)) {
            showImageCropView(data, croppedImage);
        }
    }
    private void showImageCropView(Intent data, Uri croppedImage) {
        CropImageIntentBuilder cropImage = new CropImageIntentBuilder(CROP_IMAGE_SIZE, CROP_IMAGE_SIZE, croppedImage);
        cropImage.setOutlineColor(CROP_IMAGE_HIGHLIGHT_COLOR);
        cropImage.setSourceImage(data.getData());
        cropImage.setCircleCrop(true);
        startActivityForResult(cropImage.getIntent(this), REQUEST_CROP_PICTURE);
    }
}

相机拍摄的图像无法裁剪!请给我更好的解决方案。!先谢谢

1 个答案:

答案 0 :(得分:1)

兄弟,看看这个。可以解决您的问题,您可以从相机/画廊裁剪图像。 link

  1. 设置
  2. <activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
      android:theme="@style/Base.Theme.AppCompat"/> <!-- optional (needed if default theme has no action bar) --> 
    
         

    到你的清单

    1. 调用startCropImageActivity(null);在onclick方法

    2. 这是方法:

    3. private void startCropImageActivity(Uri imageUri) {
              CropImage.activity(imageUri)
                      .setGuidelines(CropImageView.Guidelines.ON)
                      .setMultiTouchEnabled(true)
                      .setRequestedSize(320, 320, CropImageView.RequestSizeOptions.RESIZE_INSIDE)
                      .setMinCropWindowSize(0,0)
                      .setAspectRatio(1,1)
                      .setCropShape(CropImageView.CropShape.OVAL)
                      .start(this);
          }
      

      和onActivityResult

      @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
      
              if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
                  result = CropImage.getActivityResult(data);
                  if (resultCode == RESULT_OK) {
                      ((CircleImageView) findViewById(R.id.profileImage)).setImageURI(result.getUri());
                  } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                      Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
                  }
              }
          }
      

      我在这里使用CircleImageView作为圆形图像